Spring属性注入

构造方法的方式的属性注入

public class Student {
    public String name;
    public Integer age;

    /* 构造方法 */
    public Student(String name, Integer age) {
        this.name = name;
        this.age = age;
    }
}

applicationContext.xml 配置:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="student" class="com.xzh.demo4.Student">
        <constructor-arg name="name" value="IT888"/>
        <constructor-arg name="age" value="18"/>
    </bean>

</beans>

测试:

@Test
public  void test(){
	ApplicationContext applicationContext = 
		new ClassPathXmlApplicationContext("applicationContext.xml");

	Student student = (Student)applicationContext.getBean("student");
	System.out.println(student.name);
	System.out.println(student.age);
}

Set方法的属性注入

需要提供set方法:

public class Student {
    public String name;
    public Integer age;

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

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

applicationContext.xml 配置:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="student" class="com.xzh.demo4.Student">
        <property name="name" value="IT666"/>
        <property name="age" value="20"/>
    </bean>

</beans>

测试:

@Test
public  void test(){
	ApplicationContext applicationContext = 
		new ClassPathXmlApplicationContext("applicationContext.xml");

	Student student = (Student)applicationContext.getBean("student");
	System.out.println(student.name);
	System.out.println(student.age);
}

Set方法设置对象类型的属性

public class Dog {

    public String name;
    public Integer age;
    public String color;

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

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

    public void setColor(String color) {
        this.color = color;
    }

    @Override
    public String toString() {
        return "Dog{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", color='" + color + '\'' +
                '}';
    }
}


public class Student {
    public String name;
    public Integer age;
    public Dog dog;

    /* 构造方法 */
    public Student(String name, Integer age) {
        this.name = name;
        this.age = age;
    }

    public void setDog(Dog dog) {
        this.dog = dog;
    }
}

applicationContext.xml 中配置:

<bean id="dog" class="com.xzh.demo4.Dog">
	<property name="name" value="旺财"/>
	<property name="age" value="3"/>
	<property name="color" value="白色"/>
</bean>
<bean id="student" class="com.xzh.demo4.Student">
	<constructor-arg name="name" value="IT888"/>
	<constructor-arg name="age" value="18"/>
	<property name="dog" ref="dog"/>
</bean>

测试:

@Test
public  void test(){
	ApplicationContext applicationContext = new
			ClassPathXmlApplicationContext("applicationContext.xml");

	Student student = (Student)applicationContext.getBean("student");
	System.out.println(student.name);
	System.out.println(student.age);
	System.out.println(student.dog);
}

P名称空间的属性注入

使用p名称空间,就不需要写那那么多的property。使用时,要添加名称空间

xmlns:p="http://www.springframework.org/schema/p"

Dog类、Student类和测试类 同 Set方法设置对象类型的属性 一样。

applicationContext.xml 中配置:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="dog" class="com.xzh.demo4.Dog" p:name="旺财" p:age="3" p:color="黑色"/>
    <bean id="student" class="com.xzh.demo4.Student" p:dog-ref="dog">
        <constructor-arg name="name" value="IT888"/>
        <constructor-arg name="age" value="18"/>
    </bean>

</beans>

spEL表达式的属性注入

public class Dog {

    public String name;
    public Integer age;
    public String color;

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

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

    public void setColor(String color) {
        this.color = color;
    }

    @Override
    public String toString() {
        return "Dog{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", color='" + color + '\'' +
                '}';
    }
}


public class Student {
    public String name;
    public Integer age;
    public Dog dog;
    public String myDogName;

    /* 构造方法 */
    public Student(String name, Integer age) {
        this.name = name;
        this.age = age;
    }

    public void setDog(Dog dog) {
        this.dog = dog;
    }

    public void setMyDogName(String myDogName) {
        this.myDogName = myDogName;
    }
}

applicationContext.xml 中配置:

<bean id="dog" class="com.xzh.demo4.Dog">
	<property name="name" value="#{'旺财'}"/>
	<property name="age" value="#{2}"/>
	<property name="color" value="#{'红色'}"/>
</bean>
<bean id="student" class="com.xzh.demo4.Student">
	<constructor-arg name="name" value="IT888"/>
	<constructor-arg name="age" value="18"/>
	<property name="dog" value="#{dog}"/>
	<property name="myDogName" value="#{dog.name}"/>
</bean>

测试:

@Test
public  void test(){
	ApplicationContext applicationContext = new
			ClassPathXmlApplicationContext("applicationContext.xml");

	Student student = (Student)applicationContext.getBean("student");
	System.out.println(student.name);
	System.out.println(student.age);
	System.out.println(student.dog);
	System.out.println(student.myDogName);
}

集合类型属性注入

数组

public class Student {
    public String name;
    public Integer age;
    public String[] attr;

    /* 构造方法 */
    public Student(String name, Integer age) {
        this.name = name;
        this.age = age;
    }

    public void setAttr(String[] attr) {
        this.attr = attr;
    }
}

applicationContext.xml 中配置:

<bean id="student" class="com.xzh.demo4.Student">
	<constructor-arg name="name" value="IT888"/>
	<constructor-arg name="age" value="18"/>
	<!--数组-->
	<property name="attr">
		<list>
			<value>zs</value>
			<value>ls</value>
			<value>ww</value>
		</list>
	</property>
</bean>

测试:

@Test
public  void test(){
	ApplicationContext applicationContext = new
			ClassPathXmlApplicationContext("applicationContext.xml");

	Student student = (Student)applicationContext.getBean("student");
	System.out.println(student.name);
	System.out.println(student.age);
	System.out.println(Arrays.toString(student.attr));
}

List集合

public class Student {
    public String name;
    public Integer age;
    public List myList;

    /* 构造方法 */
    public Student(String name, Integer age) {
        this.name = name;
        this.age = age;
    }

    public void setMyList(List myList) {
        this.myList = myList;
    }
}

applicationContext.xml 中配置:

<bean id="student" class="com.xzh.demo4.Student">
	<constructor-arg name="name" value="IT888"/>
	<constructor-arg name="age" value="18"/>
	<!--List-->
	<property name="myList">
		<list>
			<value>123</value>
			<value>456</value>
			<value>789</value>
		</list>
	</property>
</bean>

测试:

@Test
public  void test(){
	ApplicationContext applicationContext = new
			ClassPathXmlApplicationContext("applicationContext.xml");

	Student student = (Student)applicationContext.getBean("student");
	System.out.println(student.name);
	System.out.println(student.age);
	System.out.println(student.myList);
}

Set集合

public class Student {
    public String name;
    public Integer age;
    public Set mySet;

    /* 构造方法 */
    public Student(String name, Integer age) {
        this.name = name;
        this.age = age;
    }

    public void setMySet(Set mySet) {
        this.mySet = mySet;
    }
}

applicationContext.xml 中配置:

<bean id="student" class="com.xzh.demo4.Student">
	<constructor-arg name="name" value="IT888"/>
	<constructor-arg name="age" value="18"/>
	<!--Set-->
	<property name="mySet">
		<set>
			<value>123</value>
			<value>456</value>
			<value>789</value>
		</set>
	</property>
</bean>

测试:

@Test
public  void test(){
	ApplicationContext applicationContext = new
			ClassPathXmlApplicationContext("applicationContext.xml");

	Student student = (Student)applicationContext.getBean("student");
	System.out.println(student.name);
	System.out.println(student.age);
	System.out.println(student.mySet);
}

Map集合

public class Student {
    public String name;
    public Integer age;
    public Map myMap;

    /* 构造方法 */
    public Student(String name, Integer age) {
        this.name = name;
        this.age = age;
    }

    public void setMyMap(Map myMap) {
        this.myMap = myMap;
    }
}

applicationContext.xml 中配置:

<bean id="student" class="com.xzh.demo4.Student">
	<constructor-arg name="name" value="IT888"/>
	<constructor-arg name="age" value="18"/>
	<!--Map-->
	<property name="myMap">
		<map>
			<entry key="key1" value="value1"/>
			<entry key="key2" value="value2"/>
			<entry key="key3" value="value3"/>
		</map>
	</property>
</bean>

测试:

@Test
public  void test(){
	ApplicationContext applicationContext = new
			ClassPathXmlApplicationContext("applicationContext.xml");

	Student student = (Student)applicationContext.getBean("student");
	System.out.println(student.name);
	System.out.println(student.age);
	System.out.println(student.myMap);
}
posted @ 2019-05-09 09:50  Lomen~  阅读(616)  评论(0编辑  收藏  举报