1)List、Set、Map、Properties集合注入
实体类
public class User {
private List<String> hobit;
private Set<String> subject;
private Map<String,Double> score;
private Properties db;
public User() {
}
public User(List<String> hobit, Set<String> subject, Map<String, Double> score, Properties db) {
this.hobit = hobit;
this.subject = subject;
this.score = score;
this.db = db;
}
public List<String> getHobit() {
return hobit;
}
public void setHobit(List<String> hobit) {
this.hobit = hobit;
}
public Set<String> getSubject() {
return subject;
}
public void setSubject(Set<String> subject) {
this.subject = subject;
}
public Map<String, Double> getScore() {
return score;
}
public void setScore(Map<String, Double> score) {
this.score = score;
}
public Properties getDb() {
return db;
}
public void setDb(Properties db) {
this.db = db;
}
@Override
public String toString() {
return "User{" +
"hobit=" + hobit +
", subject=" + subject +
", score=" + score +
", db=" + db +
'}';
}
}
数据库配置文件(db.properties)
driverClassName = com.mysql.jdbc.Driver
url = jdbc:mysql://localhost:3306/eesy
username = root
password = root
配置文件(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"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-4.0.xsd">
<!-- 加载数据库信息 -->
<util:properties id="db" location="classpath:db.properties"></util:properties>
<bean id="user" class="entity.User">
<property name="hobit">
<list>
<value>电影</value>
<value>音乐</value>
<value>阅读</value>
<value>阅读</value>
</list>
</property>
<property name="subject">
<set>
<value>Chinese</value>
<value>Math</value>
<value>English</value>
<value>English</value>
</set>
</property>
<property name="score">
<map>
<entry key="Chinese" value="80"></entry>
<entry key="Math" value="85"></entry>
<entry key="Math" value="86"></entry>
<entry key="English" value="90"></entry>
</map>
</property>
<property name="db" ref="db"></property>
</bean>
</beans>
测试类
public class TestDI {
@Test
public void test() {
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
User user = ac.getBean("user", User.class);
System.out.println(user);
}
}
测试结果
User{hobit=[电影, 音乐, 阅读, 阅读], subject=[Chinese, Math, English], score={Chinese=80.0, Math=86.0, English=90.0}, db={password=root, url=jdbc:mysql://localhost:3306/eesy, driverClassName=com.mysql.jdbc.Driver, username=root}}
2)引用的方式注入集合
实体类
public class User {
private List<String> hobit;
private Set<String> subject;
private Map<String,Double> score;
private Properties db;
public User() {
}
public User(List<String> hobit, Set<String> subject, Map<String, Double> score, Properties db) {
this.hobit = hobit;
this.subject = subject;
this.score = score;
this.db = db;
}
public List<String> getHobit() {
return hobit;
}
public void setHobit(List<String> hobit) {
this.hobit = hobit;
}
public Set<String> getSubject() {
return subject;
}
public void setSubject(Set<String> subject) {
this.subject = subject;
}
public Map<String, Double> getScore() {
return score;
}
public void setScore(Map<String, Double> score) {
this.score = score;
}
public Properties getDb() {
return db;
}
public void setDb(Properties db) {
this.db = db;
}
@Override
public String toString() {
return "User{" +
"hobit=" + hobit +
", subject=" + subject +
", score=" + score +
", db=" + db +
'}';
}
}
数据库配置文件(db.properties)
driverClassName = com.mysql.jdbc.Driver
url = jdbc:mysql://localhost:3306/eesy
username = root
password = root
配置文件(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"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-4.0.xsd">
<util:list id="hobitBean">
<value>电影</value>
<value>音乐</value>
<value>阅读</value>
<value>阅读</value>
</util:list>
<util:set id="subjectBean">
<value>Chinese</value>
<value>Math</value>
<value>English</value>
<value>English</value>
</util:set>
<util:map id="scoreBean">
<entry key="Chinese" value="80"></entry>
<entry key="Math" value="85"></entry>
<entry key="Math" value="86"></entry>
<entry key="English" value="90"></entry>
</util:map>
<util:properties id="dbBean">
<prop key="username">scott</prop>
<prop key="password">tiger</prop>
</util:properties>
<!-- 加载数据库信息 -->
<util:properties id="dbBean" location="classpath:db.properties"></util:properties>
<!-- 注册User类 -->
<bean id="user" class="entity.User">
<property name="hobit" ref="hobitBean"></property>
<property name="subject" ref="subjectBean"></property>
<property name="score" ref="scoreBean"></property>
<property name="db" ref="dbBean"></property>
</bean>
</beans>
测试类
public class TestDI {
@Test
public void test() {
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
User user = ac.getBean("user", User.class);
System.out.println(user);
}
}
测试结果
User{hobit=[电影, 音乐, 阅读, 阅读], subject=[Chinese, Math, English], score={Chinese=80.0, Math=86.0, English=90.0}, db={password=root, url=jdbc:mysql://localhost:3306/eesy, driverClassName=com.mysql.jdbc.Driver, username=root}}
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。