Spring bean注入

1.构造方法注入

1.bean类
public class User {
    private  String name;
    private Integer age;
    private Cat cat;
    public User(String  name,Integer age,Cat cat){
        this.name=name;
        this.age=age;
        this.cat=cat;
    }

    @Override
    public String toString() {
        return "User"+"name"+name+"age"+age+"cat"+cat.getName();
    }
}

2.测试方法
  public void demo1(){
        ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");
        User user=(User)applicationContext.getBean("user");
        System.out.println(user);
    }

3.xml文件配置
   
    class="com.imooc.ioc.demo4.User">
        
        
        
    


 

2.set方法注入

1.bean类

public class Person {
    private String name;
    private Integer age;
    private Cat cat;

    public void setCat(Cat cat) {
        this.cat = cat;
    }

    public Cat getCat() {
        return cat;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public Integer getAge() {
        return age;
    }

    @Override
    public String toString() {
        return "name"+name+"age"+age+"cat"+cat.getName();
    }
}

2.测试方法
 public void demo2(){
        ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");
        Person person=(Person)applicationContext.getBean("person1");
        System.out.println(person);
    }
3.xml文件配置
    
    class="com.imooc.ioc.demo4.Person">
        
        
        
    

3.复杂类型的注入

数组  List   set Map  Properties

package com.imooc.ioc.demo5;

import java.util.*;

/*
*
*
* 复杂类型注入*/
public class CollectionBean {
    private String[] args;  //数组类型
    private List list;//list集合
    private Set set ;//set集合
    private Map map;
    private Properties properties;

    public List getList() {
        return list;
    }

    public Map getMap() {
        return map;
    }

    public Properties getProperties() {
        return properties;
    }

    public Set getSet() {
        return set;
    }

    public String[] getArgs() {
        return args;
    }

    public void setList(List list) {
        this.list = list;
    }

    public void setMap(Map map) {
        this.map = map;
    }

    public void setProperties(Properties properties) {
        this.properties = properties;
    }

    public void setSet(Set set) {
        this.set = set;
    }

    public void setArgs(String[] args) {
        this.args = args;
    }

    @Override
    public String toString() {
        return "arrs"+ Arrays.toString(args)+"list"+list+"map"+map+"properties"+properties+"set"+set;
    }
}


2.测试方法
public  void  demo1(){
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");
CollectionBean collectionBean=(CollectionBean)applicationContext.getBean("collectionBean");
System.out.println(collectionBean);
}
3.配置文件




aaa
bbb
ccc





111
333
222





ddd
eee
fff












zhangsan


 

 

你可能感兴趣的:(Spring bean注入)