Examples of small spring (java configuration)

History of spring

  • xml spring1.X configuration used
  • Notes Spring2.X use
  • Spring3.X, Spring4.X use java configuration

The java spring configuration

spring of java configuration is through @configuration and @Bean achieved two notes

1.configuration role in the class, the equivalent of a xml configuration asked that

2.Bean acting on the method, which corresponds xml configuration <bean>

Examples

Use java configuration and function to achieve the spring IOC

step

1. pom introduced maven dependent

 

2. Create bean objects

package cn.itcast.spring;

public class User {

    private String userName;
    private String passwd;
    private Integer age;
    public String getUserName() {
        return userName;
    }
    public void setUserName(String userName) {
        this.userName = userName;
    }
    public String getPasswd() {
        return passwd;
    }
    public void setPasswd(String passwd) {
        this.passwd = passwd;
    }
    public Integer getAge() {
        return age;
    }
    public void setAge(Integer age) {
        this.age = age;
    }
    
    
}
View Code

3. Edit DAO layer used to simulate the interaction with the database layer

package cn.itcast.spring;

import java.util.ArrayList;
import java.util.List;

public class UserDAO {
    
    
    public List<User> queryUser()
    {
        List<User> userList = new ArrayList<User>();
        for (int  i = 0; i < 3; i++)
        {
            User user = new User();
            
            user.setAge(i + 2);
            user.setPasswd("passwd" + i);
            user.setUserName("username" + i);
            userList.add(user);
        }
        return userList;
    }

}
View Code

4. Service layer for the preparation of the business logic bean object

 

Guess you like

Origin www.cnblogs.com/yanfeiguo1011/p/11456452.html