Spring的属性注入-注入对象,复杂的集合类型属性

时间:2022-07-23 16:59:14

在上一篇博客上,用字符串类型的属性简单的介绍了下Spring的属性注入,这片文章会介绍更高级的属性注入.

属性注入-注入对象类型

场景介绍:我们现在要做一个新增用户的操作.
在不使用Spring类开发时,我们传统的开发应该是UserService通过UserDao来操作数据库实现对User的新增,代码如下.
传统的开发:

package cn.com.yves.spring;

public class UserDao {

public void addUser() {
System.out.println("add user success!");
}

}
public class UserService {

private UserDao dao = null;

/* 提供UserDao的构造方法 或 set方法进行属性的赋值 */
public UserService(UserDao dao) {
this.dao = dao;
}

public void setDao(UserDao dao) {
this.dao = dao;
}

/* 新增操作 */
public void addUser() {
this.dao.addUser();
}

}

使用Spring开发
在UserService中的属性dao就是一个对象类型.这里就需要用到注入对象类型的属性.要用到属性ref.ref 属性可以引用另一个 bean 的 id 或 name.

<bean id="userService" class="cn.com.yves.spring.UserService">
<!-- 注入对象类型的时候使用ref属性 -->
<property name="dao" ref="userDao"></property>
</bean>

name属性值dao为类UserService中的属性,ref的值:<bean id="userDao" class="cn.com.yves.spring.UserDao"></bean>的id值.

具体代码如下:

package cn.com.yves.spring;

public class UserDao {
public void addUser() {
System.out.println("add user success!");
}
}
package cn.com.yves.spring;

public class UserService {

private UserDao dao = null;
public void setDao(UserDao dao) {
this.dao = dao;
}

public void addUser() {
System.out.println("do add user service.");
this.dao.addUser();
}

}
<?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"
>


<!-- 这里是userDao配置在前,还是userService配置在前是不分配置顺序的.-->
<bean id="userService" class="cn.com.yves.spring.UserService">
<!-- 注入对象类型的时候使用ref属性 -->
<property name="dao" ref="userDao"></property>
</bean>
<bean id="userDao" class="cn.com.yves.spring.UserDao"></bean>

</beans>

测试方法:

    @Test
public void test() {

// 1 加载spring配置文件,根据配置创建对象
ApplicationContext context = new ClassPathXmlApplicationContext("Application.xml");

// 2 配置文件中的id获取创建的对象
UserService service = (UserService) context.getBean("userService");
service.addUser();

}

结果:

do add user service.
add user success!

spring-属性注入(对象类型)

属性注入-注入复杂类型属性

这里以简单的String类型为例子,如果子类型时对象类型,可以参考注入对象的ref属性来注入.

User.java

package cn.com.yves.spring;

import java.util.List;
import java.util.Map;
import java.util.Properties;

public class User {

private String[] arrays;
private List<String> lists;
private Map<String, String> maps;
private Properties props; // 最终实现的map

public String[] getArrays() {
return arrays;
}

public void setArrays(String[] arrays) {
this.arrays = arrays;
}

public List<String> getLists() {
return lists;
}

public void setLists(List<String> lists) {
this.lists = lists;
}

public Map<String, String> getMaps() {
return maps;
}

public void setMaps(Map<String, String> maps) {
this.maps = maps;
}

public Properties getProps() {
return props;
}

public void setProps(Properties props) {
this.props = props;
}

public void showInfo() {
System.out.println("message:" + this.arrays + "\t" + this.lists + "\t" + this.maps + "\t" + this.props);
}

}

Application.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="cn.com.yves.spring.User">

<!-- 数组 -->
<property name="arrays">
<list>
<value>array1</value>
<value>array2</value>
<value>array3</value>
</list>
</property>

<!-- list -->
<property name="lists">
<list>
<value>list1</value>
<value>list2</value>
<value>list3</value>
</list>
</property>

<!-- map -->
<property name="maps">
<map>
<entry key="aa" value="lucy"></entry>
<entry key="bb" value="jack"></entry>
<entry key="cc" value="yves"></entry>
</map>
</property>

<!-- properties -->
<property name="props">
<props>
<prop key="driverclass">com.mysql.jdbc.Driver</prop>
<prop key="username">root</prop>
</props>
</property>
</bean>

</beans>

测试代码:

 @Test
public void test() {

// 1 加载spring配置文件,根据配置创建对象
ApplicationContext context = new ClassPathXmlApplicationContext("Application.xml");

// 2 配置文件中的id获取创建的对象
User user = (User) context.getBean("user");
user.showInfo();
}

结果:

message:[Ljava.lang.String;@77ec78b9    [list1, list2, list3]   {aa=lucy, bb=jack, cc=yves} {driverclass=com.mysql.jdbc.Driver, username=root}

spring-属性注入(复杂类型)