Home > Java > javaTutorial > Quick Start with Java Spring

Quick Start with Java Spring

高洛峰
Release: 2017-01-23 11:09:14
Original
1617 people have browsed it

1. What is Spring?

Spring is an open source framework.

Spring was born to simplify enterprise-level application development. Using Spring, you can implement simple JavaBeans that were previously only possible with EJB. function.

Spring is an IOC (DI) and AOP container framework.

2. Detailed description of Spring

Lightweight: Spring is non-intrusive - objects in applications developed based on Spring can not rely on Spring's API

Dependency injection: (DI-Dependency injection, IOC)

Aspect-oriented programming: (AOP-aspect oriented programming)

Container: Spring is a container because it contains and manages the life cycle of application objects

Framework: Spring implements the use of simple component configurations to form a complex application. In Spring, XML and java annotations can be used to combine these objects.

One-stop: based on IOC and AOP It can integrate open source frameworks and excellent third-party class libraries for various enterprise applications (in fact, Spring itself also provides Spring MVC for the presentation layer and Spring JDBC for the persistence layer)

3. Build a Spring environment

1. Install Spring Tool Suite by eclipse

SPRING TOOL SUITE is an Eclipse plug-in, which can be used to more conveniently develop Spring-based applications on the Eclipse platform.

2. Add packages

Add the following jar packages to the classpath of the project:

Java Spring快速入门

3. Spring configuration file: a A typical Spring project requires the creation of one or more bean configuration files, which are used to configure beans in the Spring IOC container. Bean configuration files can be placed in the classpath or in other directories.

4. Create a Spring project and write HelloWorld:

package com.atguigu.spring.beans;
public class HelloWorld {
  private String name;
  public void setName(String name) {
    System.out.println("setName...");
    this.name = name;
  }
  public void hello(){
    System.out.println("Hello " + name);
  }
  public HelloWorld() {
    System.out.println("HelloWorld's construct...");
  }
}
Copy after login
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:util="http://www.springframework.org/schema/util"
  xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans             
http://www.springframework.org/schema/beans/spring-beans.xsd        
 http://www.springframework.org/schema/util 
 http://www.springframework.org/schema/util/spring-util-4.0.xsd">
  <bean id="helloworld" class="com.atguigu.spring.beans.HelloWorld">
    <property name="name" value="spring"></property>
  </bean>
</beans>
Copy after login
package com.atguigu.spring.beans;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
  public static void main(String[] args) {
    /*
    HelloWorld helloWorld = new HelloWorld();
    helloWorld.setName("spring");
    helloWorld.hello();
    */
    //1.创建容器
    ApplicationContext ctx = new   ClassPathXmlApplicationContext("appliactionContext.xml");
    //2.从容器中获取bean
    HelloWorld hello = (HelloWorld) ctx.getBean("helloworld");
    //3.调用bean的方法
    hello.hello();
  }
}
Copy after login

5. Test results

Java Spring快速入门

The above is the entire content of this article. I hope that the content of this article can bring some help to everyone's study or work. I also hope to support the PHP Chinese website!

For more articles related to Java Spring quick start, please pay attention to the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template