Spring中最核心的部分就是IoC容器了,也就是我们经常所说的控制反转,它主要是通过xml文件的形式将相互依赖的对象进行解耦合,也就是对象之间不必去主动查找,而是通过配置的方式进行注入,当其中一个对象发生改变的时候,我们可以不用去修改源代码,而是直接去更改对象所在的容器即可。
下面我们通过Demo简单介绍如何应用spring:
一、拷贝Spring相关的包到classpath路径下:
spring-framework-2.0\dist\下面的spring.jar核心包
spring-framework-2.0\lib\log4j\log4j-1.2.14.jar记录日志所需要的jar包
spring-framework-2.0\lib\jakarta-commons\commons-logging.jar这个记录日志是抽象的
关于这个日志记录的包,spring是这样处理的:如果在src的路径下存在log4j就会使用log4j,如果不存在就会使用commons-logging.jar包里的记录日志的包文件创建自己的库,引入相应的包。
二、提供了spring配置文件applicationContext.xml:
三、提供log4j.properties配置文件
四、编写应用程序:
编写Dao层的代码:
package com.bjpowernode.spring.dao;
public class UserDao4MySqlImpl implements UserDao {
@Override
public void addUser(String username, String password) {
System.out.println("UserDao4MySqlImpl.addUser");
}
}
Dao层主要是和数据库打交道。
编写UserManager的实现代码:
package com.bjpowernode.spring.manager;
import com.bjpowernode.spring.dao.UserDao;
/**
* 在这个类里面没有了具体的实现,面向的就是抽象的UserDao接口
* @author xiao
*
*/
public class UserManagerImpl implements UserManager {
private UserDao userDao;//UserManager依赖与UserDao,所以定义UserDao为成员变量
/**
* 这个是setter方法用于将我们的依赖对象注入到IoC容器中
* @param userDao
*/
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
public void addUser(String username, String password) {
userDao.addUser(username, password);
}
}
在这个实现类中我们需要一个UserDao的成员变量,然后通过setter方法将这个对象注入到IoC容器中。所以在每一个实现类中都会有对象的setter方法,用于对象的注入。
然后就是客户端的代码了,我们需要在客户端把UserManager拿出来,就需要创建一个BeanFactory工厂:
package com.bjpowernode.spring.client;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.bjpowernode.spring.dao.UserDao4MySqlImpl;
import com.bjpowernode.spring.manager.UserManager;
import com.bjpowernode.spring.manager.UserManagerImpl;
public class Client {
public static void main(String[] args) {
BeanFactory factory=new ClassPathXmlApplicationContext("applicationContext.xml");
UserManager userManager=(UserManager) factory.getBean("userManager");
userManager.addUser("张三", "123");
}
}
这样就完成了从IOC容器中取得注入对象的过程。
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">
<bean id="userDao4MySql" class="com.bjpowernode.spring.dao.UserDao4MySqlImpl"/>
<bean id="userDao4Oracle" class="com.bjpowernode.spring.dao.UserDao4OracleImpl"/>
<bean id="userManager" class="com.bjpowernode.spring.manager.UserManagerImpl">
<!-- 下面使用setter方法进行依赖对象的注入到IoC容器中 -->
<property name="userDao" ref="userDao4MySql"/>
</bean>
</beans>
Spring的IoC容器其实就是具有依赖注入功能的容器,负责实例化、定位、配置应用程序中的对象及建立这些对象间的依赖。应用程序无需直接在代码中new相关的对象,可以由Spring中的BeanFactory取出容器中的对象。IoC的使用原理也就是java中最普遍的“反射”机制,通过xml文件中的字符串来确定对象的生成到底选择哪个,这也就是控制反转。如果我们需要更换容器中对象,只需要修改配置文件即可。
有了IOC容器我们使得对象之间的依赖关系变成了对象和容器之间的依赖关系,由容器去管理这些对象,完成了对象之间的解耦。