一、引用数据类型
set方式注入
在UserServiceImpl中添加setUserDao方法
public class UserServiceImpl implements UserService {
private UserDao userDao;
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
@Override
public void save() {
userDao.save();
}
}
配置Spring容器调用set方法进行注入
<bean id="userDao" class="com.txt.dao.impl.UserDaoImpl"/>
<bean id="userService" class="com.txt.service.impl.UserServiceImpl">
<property name="userDao" ref="userDao"/>
</bean>
//说明property name="userDao" ,name里的参数是将setUserDao(...)的UserDao的首字母小写
set之p方式注入
本质一样
引入P命名空间
xmlns:p="http://www.springframework.org/schema/p"
修改注入方式
<bean id="userService" class="com.txt.service.impl.UserServiceImpl" p:userDao-
ref="userDao"/>
构造方式注入
创建有参构造
public UserServiceImpl(UserDao bean) {
this.bean = bean;
}
配置Spring容器调用有参构造时进行注入
<bean id="userDao" class="com.txt.dao.impl.UserDaoImpl"/>
<bean id="userService" class="com.txt.service.impl.UserServiceImpl">
<constructor-arg name="bean" ref="userDao"></constructor-arg>
</bean>
二、普通数据类型
和引用同理,也有set和构造方法注入,区别如下
代码如下(set示例):
//传的值变为value
<bean id="userDao" class="com.txt.dao.impl.UserDaoImpl">
<property name="company" value="txt"></property>
<property name="age" value="15"></property>
</bean>
三、集合数据类型
和引用同理,也有set和构造方法注入
1.List
代码如下(set示例):
//要注入的类
private List<String> strList;
public void setStrList(List<String> strList) {
this.strList = strList;
}
<bean id="userDao" class="com.txt.dao.impl.UserDaoImpl">
<property name="strList">
<list>
<value>aaa</value>
<value>bbb</value>
<value>ccc</value>
</list>
</property>
</bean>
2.List <对象>
代码如下(set示例):
public class UserDaoImpl implements UserDao {
private List<User> userList;
public void setUserList(List<User> userList) {
this.userList = userList;
}
public void save() {
System.out.println(userList);
System.out.println("UserDao save method running....");
}
}
<bean id="u1" class="com.txt.domain.User"/>
<bean id="u2" class="com.txt.domain.User"/>
<bean id="userDao" class="com.txt.dao.impl.UserDaoImpl">
<property name="userList">
<list>
<bean class="com.txt.domain.User"/>
<bean class="com.txt.domain.User"/>
<ref bean="u1"/>
<ref bean="u2"/>
</list>
</property>
</bean>
3.Map<String,User>
public class UserDaoImpl implements UserDao {
private Map<String,User> userMap;
public void setUserMap(Map<String, User> userMap) {
this.userMap = userMap; }
public void save() {
System.out.println(userMap);
System.out.println("UserDao save method running....");
}
}
<bean id="u1" class="com.txt.domain.User"/>
<bean id="u2" class="com.txt.domain.User"/>
<bean id="userDao" class="com.txt.dao.impl.UserDaoImpl">
<property name="userMap">
<map>
<entry key="user1" value-ref="u1"/>
<entry key="user2" value-ref="u2"/>
</map>
</property>
</bean>
4.Properties
public class UserDaoImpl implements UserDao {
private Properties properties;
public void setProperties(Properties properties) {
this.properties = properties;
}
public void save() {
System.out.println(properties);
System.out.println("UserDao save method running....");
}
}
<bean id="userDao" class="com.itheima.dao.impl.UserDaoImpl">
<property name="properties">
<props>
<prop key="p1">aaa</prop>
<prop key="p2">bbb</prop>
<prop key="p3">ccc</prop>
</props>
</property>
</bean>
四、引入其他配置文件
<import resource="applicationContext-xxx.xml"/>