二、Spring属性注入
程序员文章站
2022-05-24 10:49:18
...
1.Set方法的属性注入
- 创建类
Student:
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;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", 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.itlike.demo4.Student" >
<property name="name" value="myxq"/>
<property name="age" value="18"/>
</bean>
</beans>
- 创建测试类
StudentTest:
public class StudentTest {
@Test
public void test(){
ApplicationContext applicationContext =
new ClassPathXmlApplicationContext("applicationContext.xml");
Student student = (Student) applicationContext.getBean("student");
System.out.println(student); //Student{name='myxq', age=18}
}
}
2.构造方法的属性注入
- 修改类
Student:
public class Student {
public String name;
public Integer age;
//构造方法的赋值
public Student(String name, Integer age) {
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", 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.itlike.demo4.Student" >
<constructor-arg name="name" value="girlfriend" />
<constructor-arg name="age" value="21" />
</bean>
</beans>
- 创建测试类
StudentTest:
public class StudentTest {
@Test
public void test(){
ApplicationContext applicationContext =
new ClassPathXmlApplicationContext("applicationContext.xml");
Student student = (Student) applicationContext.getBean("student");
System.out.println(student); //Student{name='girlfriend', age=21}
}
}
3.对象类型的注入
- 新增类
Dog:
public class Dog {
public String name;
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Dog{" +
"name='" + name + '\'' +
'}';
}
}
- 修改类
Student:
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;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
", dog=" + dog +
'}';
}
}
- 修改配置文件
<bean id="dog" class="com.itlike.demo4.Dog">
<property name="name" value="wc"/>
</bean>
<bean id="student" class="com.itlike.demo4.Student" >
<constructor-arg name="name" value="girlfriend" />
<constructor-arg name="age" value="21" />
<property name="dog" ref="dog"/>
</bean>
- 运行测试类:
代码不变
System.out.println(student); //Student{name='girlfriend', age=21, dog=Dog{name='wc'}}
4.P名称空间的注入
- 配置文件添加名称空间:
xmlns:p="http://www.springframework.org/schema/p"
- 改写配置文件
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">
<!-- 使用p:name注入-->
<bean id="dog" class="com.itlike.demo4.Dog" p:name="wc" />
<!--使用p:dog-ref注入引用类型,只适合property标签-->
<bean id="student" class="com.itlike.demo4.Student" p:dog-ref="dog">
<constructor-arg name="name" value="girlfriend" />
<constructor-arg name="age" value="21" />
</bean>
</beans>
- 运行测试类:
System.out.println(student); //Student{name='girlfriend', age=21, dog=Dog{name='wc'}}
5.spEL表达式的注入
- 配置文件:
(为了方便,把Student中的构造方法注入改为Set注入)
<bean id="dog" class="com.itlike.demo4.Dog" p:name="wc" />
<bean id="student" class="com.itlike.demo4.Student">
<!--不使用p:name注入,而是使用sqEL表达式注入,(Web中的EL获取值)-->
<property name="name" value="#{'mygirl'}"/>
<property name="age" value="#{21}"/>
<property name="dog" value="#{dog}"/>
</bean>
- 运行测试类:
System.out.println(student); //Student{name='mygirl', age=21, dog=Dog{name='wc'}}
甚至,sqEL表达式还可以获取引用类的属性值赋给Student的某个属性:
-
Student
添加属性:
public String dogName;
public void setDogName(String dogName) {
this.dogName = dogName;
}
- 配置文件:
<property name="dogName" value="#{dog.name}"/>
- 重新覆写
Student
的toString
方法 - 运行测试类:
System.out.println(student); //Student{name='mygirl', age=21, dog=Dog{name='wc'}, dogName='wc'}
6.集合类型属性注入
- 数组
- 创建数组属性和Setter方法
public String[] attr;
public void setAttr(String[] attr) {
this.attr = attr;
}
- 配置文件:
<property name="attr">
<list>
<value>zs</value>
<value>ls</value>
<value>ww</value>
</list>
</property>
- 测试方法:
System.out.println(Arrays.toString(student.attr)); //[zs, ls, ww]
- List集合
- 创建集合和set
public List mylist;
public void setMylist(List mylist) {
this.mylist = mylist;
}
- 配置文件:
都一样的
<property name="mylist">
<list>
<value>111</value>
<value>222</value>
<value>333</value>
</list>
</property>
- 测试方法:
System.out.println(student.mylist); //[111, 222, 333]
- Set集合
- 创建集合和set
public Set myset;
public void setMyset(Set myset) {
this.myset = myset;
}
- 配置文件:
<property name="myset">
<set>
<value>aaa</value>
<value>bbb</value>
<value>ccc</value>
<value>bbb</value>
</set>
</property>
- 测试类:
System.out.println(student.myset); //[aaa, bbb, ccc]
Set集合的特点是:去重!!!
- Map集合
- 创建集合和set
public Map myMap;
public void setMyMap(Map myMap) {
this.myMap = myMap;
}
- 配置文件:
<property name="myMap">
<map>
<entry key="key1" value="value1"/>
<entry key="key2" value="value2"/>
<entry key="key3" value="value3"/>
</map>
</property>
- 测试类:
System.out.println(student.myMap); //{key1=value1, key2=value2, key3=value3}
上一篇: spring普通属性注入
下一篇: spring 注入static属性