spring中 属性注入的四种方式

 

1、set方法注入

配置:

   
    <bean name="user" class="cn.itcast.bean.User">
        
            
        <property name="name" value="tom">property>
        <property name="age" value="18">property>
              
        <property name="car" ref="car">property>
    bean>

    
    <bean name="car" class="cn.itcast.bean.Car">
        <property name="name" value="兰博基尼">property>
        <property name="color" value="黄色">property>
    bean>
//测试demo
public class Demo {
    
    @Test
    public void test(){
        //1、创建容器对象
        ApplicationContext ac =new ClassPathXmlApplicationContext("cn/itcast/injection/applicationContext.xml");
        //2、像容器要User对象
        User user = (User)ac.getBean("user");
        //3、打印user对象
        System.out.println(user.toString());
    }
    
}

结果:

spring中 属性注入的四种方式_第1张图片

 

2、构造方法注入

配置:


    <bean name="user1" class="cn.itcast.bean.User">
        <constructor-arg name="name" value="jerry">constructor-arg>
        <constructor-arg name="car" ref="car">constructor-arg>
    bean>

实体:

package cn.itcast.bean;

public class User {
    private String name;
    private Integer age;
    
    private Car car;
    
    
    public User(String name, Car car) {
        System.out.println("User(String name, Car car)");
        this.name = name;
        this.car = car;
    }
    
    public User(Integer name, Car car) {
        System.out.println("User(Integer name, Car car)");
        this.name = name+"";
        this.car = car;
    }

    public User(){
        System.out.println("user对象空参构造");
    }
    
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Integer getAge() {
        return age;
    }
    public void setAge(Integer age) {
        this.age = age;
    }

    public Car getCar() {
        return car;
    }

    public void setCar(Car car) {
        this.car = car;
    }

    @Override
    public String toString() {
        return "User [name=" + name + ", age=" + age + ", car=" + car.toString() + "]";
    }
    
}

demo

public class Demo {
    
    @Test
    public void test(){
        //1、创建容器对象
        ApplicationContext ac =new ClassPathXmlApplicationContext("cn/itcast/injection/applicationContext.xml");
        //2、像容器要User对象
        User user = (User)ac.getBean("user1");
        //3、打印user对象
        System.out.println(user.toString());
    }
    
}

结果

spring中 属性注入的四种方式_第2张图片

由 name+index + type 可以任意指定一个构造参数


    <bean name="user1" class="cn.itcast.bean.User">
        
        
        
        <constructor-arg name="name" index="0" type="java.lang.Integer" value="jerry">constructor-arg>
        <constructor-arg name="car" ref="car">constructor-arg>
    bean>

 

3、p名称空间注入

配置


    
    <bean name="user3" class="cn.itcast.bean.User" p:name="jack" p:age="20" p:car-ref="car">
    bean>

demo

public class Demo {
    
    @Test
    public void test(){
        //1、创建容器对象
        ApplicationContext ac =new ClassPathXmlApplicationContext("cn/itcast/injection/applicationContext.xml");
        //2、像容器要User对象
        User user = (User)ac.getBean("user3");
        //3、打印user对象
        System.out.println(user.toString());
    }
    
}

结果:

spring中 属性注入的四种方式_第3张图片

 

4、spel注入

配置:

xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns="http://www.springframework.org/schema/beans" 
xmlns:p="http://www.springframework.org/schema/p" 
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd ">
    
     
    <bean name="user" class="cn.itcast.bean.User">
        
        <property name="name" value="tom">property>
        <property name="age" value="18">property>
        <property name="car" ref="car">property>
    bean>

    
    <bean name="car" class="cn.itcast.bean.Car">
        <property name="name" value="兰博基尼">property>
        <property name="color" value="黄色">property>
    bean>
    
    
    <bean name="user1" class="cn.itcast.bean.User">
        
        
        
        <constructor-arg name="name" index="0" type="java.lang.Integer" value="18">constructor-arg>
        <constructor-arg name="car" ref="car">constructor-arg>
    bean>
    
    
    
    <bean name="user3" class="cn.itcast.bean.User" p:name="jack" p:age="20" p:car-ref="car">
    bean>
    
    
    
    <bean name="user4" class="cn.itcast.bean.User">
        <property name="name" value="#{user.name}">property>
        <property name="age" value="#{12}">property>
        <property name="car" ref="car">property>
    bean>
beans>

demo

public class Demo {
    
    @Test
    public void test(){
        //1、创建容器对象
        ApplicationContext ac =new ClassPathXmlApplicationContext("cn/itcast/injection/applicationContext.xml");
        //2、像容器要User对象
        User user = (User)ac.getBean("user4");
        //3、打印user对象
        System.out.println(user.toString());
    }
    
}

 

结果

spring中 属性注入的四种方式_第4张图片

你可能感兴趣的:(spring中 属性注入的四种方式)