Spring container using the most simple gesture code

If only to test a simple to use about Spring's IOC container, or look at the source code of the Spring container to achieve, then build the project Spring time, does not require complex xml configuration. It provides a way to configure Java annotations after Spring3.0 to start Spring container. Here is the simplest way to start Spring container:

1. The dependencies can only spring-context:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.springexample</groupId>
    <artifactId>spring-ioc</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.1.8.RELEASE</version>
        </dependency>
    </dependencies>

</project>

2. Start a test class:

import com.spring.ioc.SpringConfig;
import com.spring.ioc.UserService;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class Application {

    public static void main(String[] args) {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class);
        UserService userService = context.getBean(UserService.class);
        System.out.println(userService.getSysUser());
    }
}

 

3. a configuration class (@Configuration annotated classes equivalent to an xml file) for scanning and registration Bean, @ ComponentScan comment for Bean (ie scanning @ Service, @ Repository annotation such as Bean) under which the package to be scanned :

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@ComponentScan("com.spring")
@Configuration
public class SpringConfig {

}

 

4. The simulation of a traffic class, based on the annotation adding @Service:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserService {

    @Autowired
    private UserDao userDao;

    public SysUser getSysUser() {
        return userDao.getSysUser();
    }
}

 

5. Analog Dao service, plus classes on @Repository annotations provide data access services:

import org.springframework.stereotype.Repository;

@Repository
public class UserDao {
    public SysUser getSysUser() {
        SysUser sysUser = new SysUser();
        sysUser.setUserId(2);
        sysUser.setUserName("Tom");
        return sysUser;
    }
}

 

6. An entity class for encapsulating service data:

public class SysUser {

    private Integer userId;
    private String userName;

    public Integer getUserId() {
        return userId;
    }

    public void setUserId(Integer userId) {
        this.userId = userId;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    @Override
    public String toString() {
        return "SysUser{" +
                "userId=" + userId +
                ", userName='" + userName + '\'' +
                '}';
    }
}

 

6. The method executes the main class Application test in view the results:

SysUser{userId=2, userName='Tom'}

 

Guess you like

Origin www.cnblogs.com/jun1019/p/11404346.html