IOC依赖反转:
就是本来由应用程序管理的对象之间的依赖关系,现在交给了容器管理。控制权的转移,带来的好好处是降低了业务对象之间的依赖程度
~如何管理对象
这就需要配置文件,Spring IOC通过配置读取配置文件中的配置元素,通过元素对应中的各个对象进行实例化装配,一般使用及i与xml配置文件进行配置元素数据,而且是完全解耦的,可以使用其他可能的方式进行配置元素。比如注解,基于java文件的,基于属性的配置都可以。
IOC容器的代表是org.springframwork.bean包中的Beanfacotry接口, Beanfacotry接口提供管理IOC容器最基本功能,而org.springframwork.context包下的ApplicationContext接口扩展了Beanfactory,还提供了与Spring AOP的继承,国际化处理等。简单说,Beanfacotry提供了IOC容器基本功能,而ApplicationContext则增加了支持企业功能。ApplicationContext完全继承Beanfacotry。
容器实现
xmlBeanFacotry: Beanfacotry实现,提供基本ioc容器功能,可以从classpath或文件系统获取资源
(1)
File file = newFile("classpath.xml");
Resource resource= newFileSystemResoruce(file);
Beanfacotryfacotry= new XmlBeanFacotroy(resource);
(2)
Resource resource = newClassPathResource("classpath.xml");
BeanFactory beanFactory = newXmlBeanFactory(resource);
ClassPathXmlApplicationContext :ApplicationContext实现,从classpath获取资源文件
Beanfacotry facotry = new ClassPathXmlApplicationContext("classpath.xml");
ApplicationContext 获取bean方法
•Object getBean(String name) 根据名称返回一个Bean,客户端需要自己进行类型转换;
• TgetBean(String name, Class<T> requiredType) 根据名称和指定的类型返回一个Bean,客户端无需自己进行类型转换,如果类型转换失败,容器抛出异常;
• TgetBean(Class<T> requiredType) 根据指定的类型返回一个Bean,客户端无需自己进行类型转换,如果没有或有多于一个Bean存在容器将抛出异常;
实例
我们创建一个用户的接口 UserDao
package com.bjpower.node.spring.dao;
public interface IUserDao {
public void addUser(String username, String password);
}
两种数据库操作的实现
mysql
package com.bjpower.node.spring.dao;
public class UserDaoMysql implements IUserDao{
@Override
public void addUser(String username, String password) {
// TODO Auto-generated method stub
System.out.println("usermySql");
}
}
oracle
package com.bjpower.node.spring.dao;
public class UserDaoOracle implements IUserDao {
@Override
public void addUser(String username, String password) {
// TODO Auto-generated method stub
System.out.println("userDalOral.adduser");
}
}
业务层的管理类接口
package com.bjpower.node.spring.manager;
public interface UserManager {
public void addUser (String username, String password);
}
业务类的实现
//通常我们是工厂反射调用接口,但这里我们使用IOC的容器,set方法注入
package com.bjpower.node.spring.manager;
import com.bjpower.node.spring.dao.IUserDao;
import com.bjpower.node.spring.dao.UserDaoOracle;
public class UserManagerImp implements UserManager {
public void addUser(String username, String password) {
// 由我们的应用程序负责服务 定位
IUserDao userDao = new UserDaoOracle();
userDao.addUser(username, password);
}
private IUserDao userDao;//注入的userDao,需要配置文件注入
public void setUserDao(IUserDao userDao) {
this.userDao = userDao;
}
///set方法的默认配置
public UserManagerImp() {
}
}
配置文件,让IoC容器知道要管理哪些对象。
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
<!-- ========================= BUSINESS OBJECT DEFINITIONS ======================== -->
<bean id="userDaoMysql" class="com.bjpower.node.spring.dao.UserDaoMysql"/>
<bean id="userDalOracle" class="com.bjpower.node.spring.dao.UserDaoOracle"/>
<bean id="userManager" class="com.bjpower.node.spring.manager.UserManagerImp">
<!-- 描述set方法 -->
<property name="userDao" ref="userDalOracle"></property>
</bean>
</beans>
客户端调用
package com.bjpower.node.spring.client;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Client {
public static void main(String[] args) {
//请求io容器
BeanFactory factory = new ClassPathXmlApplicationContext("applicationContext.xml");
UserManager userManager=(UserManager) factory.getBean("userManager");
userManager.addUser("hanhan", "passowrd");
}
}
小结:
通过ioc容器的注入,客户端代码完全面向接口编程,无需知道实现类,可以通过修改配置文件来更换接口实现,客户端代码不需要任何修改。是不是低耦合。