Spring-Bean依赖注入(引用数据类型和集合数据类型)

本文内容纲要:Spring-Bean依赖注入(引用数据类型和集合数据类型)

为什么使用spring依赖注入详见–》依赖注入分析

1.创建实体类User类

package com.hao.domain;

public class User {

private String name;

private String addr;

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public String getAddr() {

return addr;

}

public void setAddr(String addr) {

this.addr = addr;

}

@Override

public String toString() {

return "User{" +

"name='" + name + '\'' +

", addr='" + addr + '\'' +

'}';

}

}

2.UserDao接口及其实现类UserDaoImpl(接口代码省略)

public class UserDaoImpl implements UserDao {

private List<String> strList; //集合

private Map<String, User> userMap;//集和,集合中也包括User引用

private Properties properties;

//使用set方法注入

public void setStrList(List<String> strList) {

this.strList = strList;

}

public void setUserMap(Map<String, User> userMap) {

this.userMap = userMap;

}

public void setProperties(Properties properties) {

this.properties = properties;

}

@Override

public void save1() {

System.out.println(strList);

System.out.println(userMap);

System.out.println(properties);

}

}

3.UserService接口以及UserServiceImpl实现类(省略接口代码)

public class UserServiceImpl implements UserService {

private UserDao userDao;

public UserServiceImpl() {

}

public UserServiceImpl(UserDao userDao) {

this.userDao = userDao;

}

@Override

public void sava() {

userDao.save1();

}

}

3.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="userDao" class="com.hao.dao.impl.UserDaoImpl">

<property name="strList">

<list>

<value>aaa</value>

<value>bbb</value>

<value>ccc</value>

</list>

</property>

<property name="userMap">

<map>

<entry key="user1" value-ref="user1"></entry>

<entry key="user2" value-ref="user2"></entry>

</map>

</property>

<property name="properties">

<props>

<prop key="p1">ppp1</prop>

<prop key="p2">ppp2</prop>

<prop key="p3">ppp3</prop>

</props>

</property>

</bean>

//因为map集合里面包括User引用,而User类中还有其他属性,所以需要进行注入,然后通过id被上面配置的value-ref引入

<bean id="user1" class="com.hao.domain.User">

<property name="name" value="tom"/>

<property name="addr" value="beijing"/>

</bean>

<bean id="user2" class="com.hao.domain.User">

<property name="name" value="jin"/>

<property name="addr" value="tianjing"/>

</bean>

<bean id="userService" class="com.hao.service.impl.UserServiceImpl">

<constructor-arg name="userDao" ref="userDao"></constructor-arg>

</bean>

</beans>

5.测试类

public class UserController {

public static void main(String[] args) {

ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");

UserService service = (UserService) context.getBean("userService");

service.sava();

}

}

结果当然显而易见了

本文内容总结:Spring-Bean依赖注入(引用数据类型和集合数据类型)

原文链接:https://www.cnblogs.com/kevinsvim/p/14614961.html

以上是 Spring-Bean依赖注入(引用数据类型和集合数据类型) 的全部内容, 来源链接: utcz.com/z/296036.html

回到顶部