接上篇<<模拟Spring实现>>,本节来学习IOC。
在上篇的基础上,保留model,DAO,Service,我们加入Spring依赖,这里我用的Spring版本是2.6.5。spring.jar,commons-logging.jar。
1.beans.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-2.5.xsd"> <bean id="u" class="com.yuwl.dao.impl.UserDAOImpl"> </bean> <bean id="userService" class="com.yuwl.service.UserService"> <property name="userDAO" ref="u" /> </bean> </beans>
2.测试
import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.yuwl.model.User; //Dependency Injection //Inverse of Control public class UserServiceTest { @Test public void testAdd() throws Exception { ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml"); UserService service = (UserService)ctx.getBean("userService"); User u = new User(); u.setUsername("yuwl"); u.setPassword("123456"); service.add(u); } }
结果,执行成功:user saved!
说明:这里为什么使用ApplicationContext,而不用BeanFactory,根据官方文档,BeanFactory是基类,子类ApplicationContext比它做了更好的扩展,功能更强大,因此官方推荐使用ApplicationContext。
3.理解IOC
理解DI:
直接理解IOC,不太好说,先来说DI—Dependency Injection,依赖注入。
看配置文件beans.xml,userService依赖Spring容器注入userDAO,从而得到userDAO实例。
再看另一方面,userService也是通过Spring容器注入com.bjsxt.service.UserService,我们才能不用new而获得其实例,userDAO也是一样。
理解IOC:
依赖注入搞明白了,再来看IOC—Inverse of Control,控制反转,以前写程序都是我们自己new的service,自己new的dao,但是现在交给Spring容器来完成了,配下配置文件,简单又灵活就搞定了,是不是把这控制权交给了Spring,这就是控制反转。把控制权由我们人为控制交给Spring来控制。
4.注入类型
Spring提供的注入类型有三种:
a.setter(最常用)
b.构造方法(不常用)
c.接口或属性(基本不用)
我们一般用的最多的就是setter,其它两种可以不用掌握。
5.<bean中的scope属性
a.singleton单例
b.proptotype 每次创建新的对象
不写默认为singleton单例,一般配置Action的bean时,设置为proptotype。
6.自动装配
a.byName—根据bean name来自动选择
b.byType—根据bean class来自动选择
c.如果所有的bean都用同一种,可以使用beans的属性:default-autowire
byName:
<?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-2.5.xsd" > <!-- byName --> <bean name="userDAO" class="com.yuwl.dao.impl.UserDAOImpl"> <property name="daoId" value="1"></property> </bean> <bean name="userDAO2" class="com.yuwl.dao.impl.UserDAOImpl"> <property name="daoId" value="2"></property> </bean> <bean id="userService" class="com.yuwl.service.UserService" autowire="byName"> </bean> </beans>
byType:
<?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-2.5.xsd" > <!-- byType --> <bean name="userDAO" class="com.yuwl.dao.impl.UserDAOImpl"> <property name="daoId" value="1"></property> </bean> <!-- <bean name="userDAO2" class="com.yuwl.dao.impl.UserDAOImpl"> <property name="daoId" value="2"></property> </bean> --> <bean id="userService" class="com.yuwl.service.UserService" autowire="byType"> </bean> </beans>
7.Annotation第一步
修改xml文件,加入<context:annotation-config />
<?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:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"> <context:annotation-config /> <bean id="u" class="com.yuwl.dao.impl.UserDAOImpl"> </bean> <bean id="u2" class="com.yuwl.dao.impl.UserDAOImpl"> </bean> <bean id="userService" class="com.yuwl.service.UserService" > </bean> </beans>
8.@Autowired
a.默认按类型byType
b.如果想用byName,使用@Qulifier
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import com.yuwl.dao.UserDAO; import com.yuwl.model.User; public class UserService { private UserDAO userDAO; public void init() { System.out.println("init"); } public void add(User user) { userDAO.save(user); } public UserDAO getUserDAO() { return userDAO; } @Autowired public void setUserDAO(@Qualifier("u") UserDAO userDAO) { this.userDAO = userDAO; } public void destroy() { System.out.println("destroy"); } }
9.@Resource(重要)
a.加入:j2ee/common-annotations.jar
b.默认按名称byName,名称找不到,按类型byType
c.可以指定特定名称
d.推荐使用
import javax.annotation.Resource; import com.yuwl.dao.UserDAO; import com.yuwl.model.User; public class UserService { private UserDAO userDAO; public void init() { System.out.println("init"); } public void add(User user) { userDAO.save(user); } public UserDAO getUserDAO() { return userDAO; } @Resource public void setUserDAO( UserDAO userDAO) { this.userDAO = userDAO; } public void destroy() { System.out.println("destroy"); } }
10.@Component @Service @Controller @Repository
a.初始化的名字默认为类名首字母小写
b.可以指定初始化bean的名字
beans.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" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"> <context:annotation-config /> <context:component-scan base-package="com.yuwl"/> </beans>
Spring容器按包结构base-package="com.yuwl"来搜索要初始化的bean
UserDAOImpl:
import org.springframework.stereotype.Component; import com.yuwl.dao.UserDAO; import com.yuwl.model.User; @Component("u") public class UserDAOImpl implements UserDAO { public void save(User user) { //Hibernate or Jdbc add System.out.println("user saved!"); } }
UserService:
import javax.annotation.Resource; import org.springframework.stereotype.Component; import com.yuwl.dao.UserDAO; import com.yuwl.model.User; @Component("userService") public class UserService { private UserDAO userDAO; public void init() { System.out.println("init"); } public void add(User user) { userDAO.save(user); } public UserDAO getUserDAO() { return userDAO; } @Resource(name="u") public void setUserDAO( UserDAO userDAO) { this.userDAO = userDAO; } public void destroy() { System.out.println("destroy"); } }
Spring IOC最主要的核心内容就学到这,下面将深入AOP的学习。