Bean的依赖注入分析
案例举例service层
编写UserService接口
package com.taotao.service;
public interface UserService {
public void save();
}
编写接口实现类UserServiceImpl
package com.taotao.service.impl;
import com.taotao.dao.UserDao;
import com.taotao.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class UserServiceImpl implements UserService {
public void save() {
ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
UserDao userDao = (UserDao) app.getBean("userDao");
userDao.save();
}
}
模拟web层
编写UserController类
package com.taotao.demo;
import com.taotao.service.impl.UserServiceImpl;
public class UserController {
public static void main(String[] args) {
UserServiceImpl userService = new UserServiceImpl();
userService.save();
}
}
测试运行
使用Spring.xml完成上面的案例
编写UserController类
package com.taotao.demo;
import com.taotao.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class UserController {
public static void main(String[] args) {
ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
UserService userService = (UserService) app.getBean("userService");
userService.save();
}
}
编写Spring.xml
把userService配到容器中
<?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.taotao.dao.impl.UserDaoImpl"></bean>
<bean id="userService" class="com.taotao.service.impl.UserServiceImpl"></bean>
</beans>
测试运行
分析
- 目前UserService实例和UserDao实例都存在与Spring容器中,当前的做法是在容器外部获得UserService实例和UserDao实例,然后在程序中进行结合。
- 因为UserService和UserDao都在Spring容器中,而最终程序直接使用的是UserService,所以可以在Spring容器中,将UserDao设置到UserService内部。
Bean的依赖注入概念
依赖注入(Dependency Injection):他是Spring框架核心IOC的具体实现
在编写程序时,通过控制反转,把对象的创建交给了Spring,但是代码中不可能出现没有依赖的情况。IOC解耦只是降低他们的依赖关系,但不会消除。例如:业务层仍会调用持久层的方法。
那这种业务层和持久局的依赖关系,在使用Spring之后,就让Spring来维护了。简单的说,就是坐等框架把持久层对象传入业务层,而不用我们自己去获取。
如何将UserDao注入到UserService内部呢?
演示set方法注入
编写UserServiceImpl.java
package com.taotao.service.impl;
import com.taotao.dao.UserDao;
import com.taotao.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class UserServiceImpl implements UserService {
private UserDao userDao;
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
public void save() {
userDao.save();
}
}
编写spring.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.taotao.dao.impl.UserDaoImpl"></bean>
<bean id="userService" class="com.taotao.service.impl.UserServiceImpl">
<property name="userDao" ref="userDao"></property>
</bean>
</beans>
测试运行
set注入扩展
p命名空间注入
P命名空间注入本质也是set方法注入,但比起上述的set方法注入更加方便,主要体现在配置文件中,如下:首先,需要引入P命名空间:
xmlns:p="http://www.springframework.org/schema/p"
其次,需要修改注入方法
<bean id="userService" class="com.taotao.service.impl.UserServiceImpl" p:userDao-ref="userDao"/>
演示构造方法注入
先把刚才的set方法注释掉
生成构造器,编写Service
package com.taotao.service.impl;
import com.taotao.dao.UserDao;
import com.taotao.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class UserServiceImpl implements UserService {
private UserDao userDao;
public UserServiceImpl(UserDao userDao) {
this.userDao = userDao;
}
public UserServiceImpl() {
}
public void save() {
userDao.save();
}
}
编写Spring.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.taotao.dao.impl.UserDaoImpl"></bean>
<bean id="userService" class="com.taotao.service.impl.UserServiceImpl">
<constructor-arg name="userDao" ref="userDao"></constructor-arg>
</bean>
</beans>
运行测试
Bean的依赖注入的数据类型
上面的操作,都是注入的引用Bean,除了对象的引河以注入,普通数据类型,集合等都可以在容器中进行注入。
注入数据的三种数据类型
演示注入"普通数据类型"
编写UserDaoImpl.java
package com.taotao.dao.impl;
import com.taotao.dao.UserDao;
public class UserDaoImpl implements UserDao {
private String username;
private int age;
public void setUsername(String username) {
this.username = username;
}
public void setAge(int age) {
this.age = age;
}
public void save() {
System.out.println(username + "\n" + age );
System.out.println("save running...");
}
}
编写Spring.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.taotao.dao.impl.UserDaoImpl">
<property name="username" value="taotao"/>
<property name="age" value="18"/>
</bean>
<bean id="userService" class="com.taotao.service.impl.UserServiceImpl">
<constructor-arg name="userDao" ref="userDao"></constructor-arg>
</bean>
</beans>
测试运行
演示注入"集合类型"
编写UserDaoImpl.java
package com.taotao.dao.impl;
import com.taotao.dao.UserDao;
import com.taotao.domain.User;
import java.util.List;
import java.util.Map;
import java.util.Properties;
public class UserDaoImpl implements UserDao {
private String username;
private int age;
private List<String> strList;
private Map<String, User> userMap;
private Properties properties;
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;
}
public void setUsername(String username) {
this.username = username;
}
public void setAge(int age) {
this.age = age;
}
public void save() {
System.out.println(strList);
System.out.println(userMap);
System.out.println(properties);
System.out.println("save running...");
}
}
创建User.java
package com.taotao.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 + '\'' +
'}';
}
}
编写Spring.xml(注入List)
<?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.taotao.dao.impl.UserDaoImpl">
<property name="strList" >
<list>
<value>111</value>
<value>222</value>
<value>333</value>
</list>
</property>
</bean>
<bean id="userService" class="com.taotao.service.impl.UserServiceImpl">
<constructor-arg name="userDao" ref="userDao"></constructor-arg>
</bean>
</beans>
运行测试
Spring.xml(注入Map)
<?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.taotao.dao.impl.UserDaoImpl">
<property name="strList" >
<list>
<value>111</value>
<value>222</value>
<value>333</value>
</list>
</property>
<property name="userMap">
<map>
<entry key="u1" value-ref="user1"></entry>
<entry key="u2" value-ref="user2"></entry>
</map>
</property>
</bean>
<bean id="user1" class="com.taotao.domain.User">
<property name="name" value="tom"/>
<property name="addr" value="beijing"/>
</bean>
<bean id="user2" class="com.taotao.domain.User">
<property name="name" value="jack"/>
<property name="addr" value="shanghai"/>
</bean>
<bean id="userService" class="com.taotao.service.impl.UserServiceImpl">
<constructor-arg name="userDao" ref="userDao"></constructor-arg>
</bean>
</beans>
运行测试
Spring.xml(注入Properties)
<?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.taotao.dao.impl.UserDaoImpl">
<property name="strList" >
<list>
<value>111</value>
<value>222</value>
<value>333</value>
</list>
</property>
<property name="userMap">
<map>
<entry key="u1" value-ref="user1"></entry>
<entry key="u2" value-ref="user2"></entry>
</map>
</property>
<property name="properties">
<props>
<prop key="p1">pp1</prop>
<prop key="p2">pp2</prop>
<prop key="p3">pp3</prop>
</props>
</property>
</bean>
<bean id="user1" class="com.taotao.domain.User">
<property name="name" value="tom"/>
<property name="addr" value="beijing"/>
</bean>
<bean id="user2" class="com.taotao.domain.User">
<property name="name" value="jack"/>
<property name="addr" value="shanghai"/>
</bean>
<bean id="userService" class="com.taotao.service.impl.UserServiceImpl">
<constructor-arg name="userDao" ref="userDao"></constructor-arg>
</bean>
</beans>
运行测试
|