spring的注入方式

给bean注入值

1、构造函数注入(类中需定义对应的构造器)
使用参数名:


argValue


或者:


·注入集合对象

 张三

  西红柿

·类似的还有





lisi

依赖注入

1、引用已定义的bean:


或者:

 

注:bean属性指明目标bean的ID。
2、使用内部bean :

 

整体代码演示如下

pom.xml坐标的配置:

com.lmm
    Spring_demo1
    1.0-SNAPSHOT
    
        
        
            org.springframework
            spring-context
            5.0.0.RELEASE
        
        
        
            org.springframework
            spring-test
            5.0.0.RELEASE
            test
        
        
        
            org.springframework
            spring-core
            5.0.0.RELEASE
        
        
        
            org.springframework
            spring-web
            5.0.0.RELEASE
        
        
        
            org.springframework
            spring-beans
            5.0.0.RELEASE
        
        
        
            commons-logging
            commons-logging
            1.2
        
        
        
            org.springframework
            org.springframework.context.support
            3.2.2.RELEASE
        
        
            junit
            junit
            4.12
            test
        
    

spring-bean.xml的配置(主要)


    
    
        
        
            高三泥
        
        
        
    
    
        
        
        
        
        
            
                美食
                电影
                睡觉
                听歌
            
        
        
        
            
                
                
                
            
        
    
    
        
        
        
        
        
        
        

        

        



    

实体类(两个)

public class User {
    private int id;
    private String username;
    private String sex;
    private Date birthday;
    private String address;
    private List hobbys;//爱好
    private Map phones;//手机号
    public User() {
    }

    public User(int id) {
        this.id = id;
    }

    public User(String username) {
        this.username = username;
    }

    public User(int id, String username) {
        this.id = id;
        this.username = username;
    }

    public User(String username, String sex) {
        this.username = username;
        this.sex = sex;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public List getHobbys() {
        return hobbys;
    }

    public void setHobbys(List hobbys) {
        this.hobbys = hobbys;
    }

    public Map getPhones() {
        return phones;
    }

    public void setPhones(Map phones) {
        this.phones = phones;
    }

    /**
     * 通过配置,让该方法在对象初始化的时候自动执行
     */
    public void init(){
        System.out.println("我被初始化了");
    }

    /**
     * 通过配置,让该方法在对象销毁的时候自动执行
     */
    public void destory(){
        System.out.println("我被销毁了");
    }

  /*  @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", username='" + username + '\'' +
                ", sex='" + sex + '\'' +
                ", birthday=" + birthday +
                ", address='" + address + '\'' +
                '}';
    }*/
}
public class Account {
    private int aid;
    private double money;
    private User user;

    public Account() {
    }

    public Account(int aid, double money, User user) {
        this.aid = aid;
        this.money = money;
        this.user = user;
    }

    public int getAid() {
        return aid;
    }

    public void setAid(int aid) {
        this.aid = aid;
    }

    public double getMoney() {
        return money;
    }

    public void setMoney(double money) {
        this.money = money;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }
}

测试类的实现:

public class UserTest {
    @Test
    public void testUser(){
        //获取配置文件spring-bean.xml
        ApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring-bean.xml");
        //获取User实例
        User user = (User)context.getBean("user");//对应的就是spring-bean.xml中对应的id
        user.setUsername("刘昊东");
        System.out.println(user.getUsername());

        User user1 = (User)context.getBean(User.class);
        System.out.println(user1.getUsername());
        System.out.println(user==user1);

    }
    @Test
    public void testUser2(){
        //获取配置文件spring-bean.xml
        ApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring-bean.xml");
        //获取User实例
        User user = (User)context.getBean("user");//对应的就是spring-bean.xml中对应的id

        System.out.println(user.getUsername());
        System.out.println(user.getId());



    }
    @Test
    public void testUser3(){
        //获取配置文件spring-bean.xml
        ApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring-bean.xml");
        //获取User实例
        User user = (User)context.getBean("user1");//对应的就是spring-bean.xml中对应的id

        System.out.println(user.getUsername());
        System.out.println(user.getSex());
    }
    @Test
    public void testUser4(){
        //获取配置文件spring-bean.xml
        ApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring-bean.xml");
        //获取User实例
        Account account = (Account) context.getBean("account");//对应的就是spring-bean.xml中对应的id

        System.out.println(account.getAid());
        System.out.println(account.getUser().getUsername());
        System.out.println(account.getUser().getAddress());
        //爱好
        List hobbys = account.getUser().getHobbys();
        for (String hobby : hobbys) {
            System.out.println(hobby);
        }
        //手机号
        Map phones = account.getUser().getPhones();
        Set key = phones.keySet();
        for (String s : key) {
            System.out.println(s+":"+phones.get(s));
        }


    }
}

你可能感兴趣的:(spring的注入方式)