依赖注入时,最常用的是setter注入和构造函数注入。
本篇介绍了setter注入时,如何注入常量、对象、数组、List、Map、Set、Null和Properties,以及p命名空间注入;也介绍了构造函数注入和c命名空间注入。
setter注入
- com.jepcc.demo.model.FullName
package com.jepcc.demo.model;
public class FullName {
private String firstName;
private String lastName;
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
}
- com.jepcc.demo.model.Student
package com.jepcc.demo.model;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
public class Student {
private String id;
private FullName fullName;
private String[] skills;
private List<String> hobbies;
private Map<String,Integer> scores;
private Set<String> artists;
private String company;
private Properties info;
public void setId(String id) {
this.id = id;
}
public void setFullName(FullName fullName) {
this.fullName = fullName;
}
public void setSkills(String[] skills) {
this.skills = skills;
}
public void setHobbies(List<String> hobbies) {
this.hobbies = hobbies;
}
public void setScores(Map<String, Integer> scores) {
this.scores = scores;
}
public void setArtists(Set<String> artists) {
this.artists = artists;
}
public void setCompany(String company) {
this.company = company;
}
public void setInfo(Properties info) {
this.info = info;
}
public void show(){
System.out.print("id="+id+
",fullName="+fullName.getFirstName()+" "+fullName.getLastName()+"" +
",skills=");
for(String skill: skills){
System.out.print(skill + " ");
}
System.out.println();
System.out.println("hobbies="+hobbies);
System.out.println("scores="+scores);
System.out.println("artists="+artists);
System.out.println("company="+company);
System.out.println("info="+info);
}
}
- com.jepcc.demo.test.Test
package com.jepcc.demo.test;
import com.jepcc.demo.model.Student;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("config.xml");
Student stu = (Student) context.getBean("student");
stu.show();
}
}
- config.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="fullname" class="com.jepcc.demo.model.FullName">
<property name="firstName" value="Steven"></property>
<property name="lastName" value="Jobs"></property>
</bean>
<bean id="student" class="com.jepcc.demo.model.Student">
<property name="id" value="374887"></property>
<property name="fullName" ref="fullname"></property>
<property name="skills">
<array>
<value>HTML</value>
<value>CSS</value>
<value>JavaScript</value>
<value>Java</value>
</array>
</property>
<property name="hobbies">
<list>
<value>羽毛球</value>
<value>游泳</value>
<value>跑步</value>
</list>
</property>
<property name="scores">
<map>
<entry key="数据结构与算法" value="90"></entry>
<entry key="数据库原理" value="88"></entry>
<entry>
<key><value>操作系统</value></key>
<value>95</value>
</entry>
<entry>
<key><value>编译原理</value></key>
<value>85</value>
</entry>
</map>
</property>
<property name="artists">
<set>
<value>周杰伦</value>
<value>陈小春</value>
<value>胡歌</value>
</set>
</property>
<property name="company"><null></null></property>
<property name="info">
<props>
<prop key="address">深圳市</prop>
<prop key="email">test@exam.com</prop>
</props>
</property>
</bean>
</beans>
运行Test.main(),控制台打印如下信息:
常量注入
<bean id="student" class="com.jepcc.demo.model.Student">
<property name="id" value="374887"></property>
</bean>
对象注入
<bean id="fullname" class="com.jepcc.demo.model.FullName">
<property name="firstName" value="Steven"></property>
<property name="lastName" value="Jobs"></property>
</bean>
<bean id="student" class="com.jepcc.demo.model.Student">
<property name="fullName" ref="fullname"></property>
</bean>
数组注入
<bean id="student" class="com.jepcc.demo.model.Student">
<property name="skills">
<array>
<value>HTML</value>
<value>CSS</value>
<value>JavaScript</value>
<value>Java</value>
</array>
</property>
</bean>
List注入
<bean id="student" class="com.jepcc.demo.model.Student">
<property name="hobbies">
<list>
<value>羽毛球</value>
<value>游泳</value>
<value>跑步</value>
</list>
</property>
</bean>
Map注入
<bean id="student" class="com.jepcc.demo.model.Student">
<property name="scores">
<map>
<entry key="数据结构与算法" value="90"></entry>
<entry key="数据库原理" value="88"></entry>
<entry>
<key><value>操作系统</value></key>
<value>95</value>
</entry>
<entry>
<key><value>编译原理</value></key>
<value>85</value>
</entry>
</map>
</property>
</bean>
Set注入
<bean id="student" class="com.jepcc.demo.model.Student">
<property name="artists">
<set>
<value>周杰伦</value>
<value>陈小春</value>
<value>胡歌</value>
</set>
</property>
</bean>
Null注入
<bean id="student" class="com.jepcc.demo.model.Student">
<property name="company"><null></null></property>
</bean>
Properties注入
<bean id="student" class="com.jepcc.demo.model.Student">
<property name="info">
<props>
<prop key="address">深圳市</prop>
<prop key="email">test@exam.com</prop>
</props>
</property>
</bean>
p命名空间注入
- 新建类
User
,com.jepcc.demo.model.User
package com.jepcc.demo.model;
public class User {
private String name;
private int age;
public void setName(String name) {
this.name = name;
}
public void setAge(int age) {
this.age = age;
}
public void show(){
System.out.println("name="+name+",age="+age);
}
}
- 修改测试类
Test
,com.jepcc.demo.test.Test
package com.jepcc.demo.test;
import com.jepcc.demo.model.User;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("config.xml");
User user = (User) context.getBean("user");
user.show();
}
}
- config.xml
注意,头文件中新增了xmlns:p="http://www.springframework.org/schema/p"
,且属性是通过p:name
或p:age
设置的。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="user" class="com.jepcc.demo.model.User" p:name="李小明" p:age="20">
</bean>
</beans>
运行Test.main(),控制台打印出如下信息:
构造函数注入
- com.jepcc.demo.model.User
package com.jepcc.demo.model;
public class User {
private String name;
private int age;
public User(String name, int age) {
this.name = name;
this.age = age;
}
public void show(){
System.out.println("name="+name+",age="+age);
}
}
- com.jepcc.demo.test.Test
package com.jepcc.demo.test;
import com.jepcc.demo.model.User;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("config.xml");
User user = (User) context.getBean("user");
user.show();
}
}
- config.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="user" class="com.jepcc.demo.model.User">
<constructor-arg name="name" value="李小明"></constructor-arg>
<constructor-arg name="age" value="20"></constructor-arg>
</bean>
</beans>
c命名空间注入
对config.xml稍作修改,注意,头文件增加xmlns:c="http://www.springframework.org/schema/c"
,且属性是通过c:name
或c:age
设置的。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:c="http://www.springframework.org/schema/c"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="user" class="com.jepcc.demo.model.User" c:name="Nicholas" c:age="18">
</bean>
</beans>
bean的作用域
其中, singleton
,单例,即整个容器中只有一个对象实例,scope
的默认值是singleton
;prototype
,原型,每次获取bean时都会产生一个新的对象实例。