Spring框架annotation实现IOC介绍
Spring学习笔记(三)
续Spring 学习笔记(二)之后,对Spring框架的annotation实现方法进行整理
本文目录
@Autowire
1 @Autowire+@Qualifier tips
2 myeclipse content小技巧
@Resource
1@Resource(name,type)
2与@Autowire的区别 按名字和按类型
@Component
1@Resource+@Component
2@Resource(NAME)+@Component(NAME)
@scope
@PostConstruct and @PreDestroy
annotation实现的Spring XML配置
1 查询文档http://docs.spring.io/spring/docs/4.0.0.BUILD-SNAPSHOT/spring-framework-reference/htmlsingle/
<?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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <context:annotation-config/> </beans>
注意
1 http://www.springframework.org/schema/context/spring-context.xsd 找到所在的文件位置并添加到中xml catalog
2 写上 <context:annotation-config/> 表示启用annotation配置
3 介绍@Resource与@Autowire(通常使用@Resource)
1 @Resource与@Autowire的区别在于,Resource先是按照名字查找,然后再按照类型查找,
Autowire按照类型查找
2 通常我们只使用@Resource就可以了。
3 @Resource的语法
@Resource(name="userDaoImpl") 如果不指定name则按照set的参数的名字来查找,一般情况下建议指定NAME的值,便于对多种实现进行明确定位
UserServiceImpl
package com.serviceImpl; import javax.annotation.Resource; import com.dao.UserDao; import com.entity.User; public class UserServiceImpl { private UserDao userDao; public void add(User user) { userDao.save(user); } public void update(User user) { userDao.update(user); } public void init() {// 初始方法 System.out.println("init"); } public void destroy() {// 销毁方法 System.out.println("destory"); } public UserDao getUserDao() { return userDao; } @Resource(name="userDaoImpl") public void setUserDao(UserDao userDao) { this.userDao = userDao; } }
再来看配置文件
<?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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <context:annotation-config/> <bean name="userDaoImpl" class="com.daoImpl.UserDaoImpl"/> <bean name="userDaoImpl2" class="com.daoImpl.UserDaoImpl2"/> <bean name="userServiceImpl" class="com.serviceImpl.UserServiceImpl"> <!-- <property name="userDao" ref="userDaoImpl"></property> 使用了@Resource与@Autowire之后这一行就可以不用写了--> </bean> </beans>
测试类
package com.serviceImpl.test; import org.junit.Before; import org.junit.Test; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.entity.User; import com.serviceImpl.UserServiceImpl; public class UserServiceImplTest { User user; @Before public void setUp() throws Exception { user = new User(); user.setName("testName"); user.setRemark("testRemark"); } @Test public void testAdd() { ClassPathXmlApplicationContext app = new ClassPathXmlApplicationContext("beans.xml"); UserServiceImpl UserServiceImpl = (UserServiceImpl)app.getBean("userServiceImpl"); UserServiceImpl.add(user);//调用方法 UserServiceImpl.update(user);//调用方法 } }
执行结果
testName-->testRemark save --调用UserDaoImpl!
testName-->testRemark update --调用UserDaoImpl!
TIPS:如果按@不出现提示怎么办?
在Myeclipse 中的JAVA editor中配置一下即可
4 @Component介绍
1 Component是组件化的注解对整个类进行初始化时使用
2 @Component的语法
Component注释位于类的上方,Component(NAME)用于指定名字,不指定则用类名(第一个首字母小写)
package com.daoImpl; import org.springframework.stereotype.Component; import com.dao.UserDao; import com.entity.User; @Component public class UserDaoImpl implements UserDao{ public void save(User user) { System.out.println(user.getName()+"-->"+user.getRemark()+" save --调用UserDaoImpl!"); } public void update(User user) { System.out.println(user.getName()+"-->"+user.getRemark()+" update --调用UserDaoImpl!"); } }
@Component需要在XML里配置搜索带有@Component注解路径
<?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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <context:annotation-config/> <context:component-scan base-package="com.daoImpl"></context:component-scan> <!-- 配置component路径,如果配置根目录会很方便,不过初始化速度会有点影响--> <bean name="userDaoImpl2" class="com.daoImpl.UserDaoImpl2"/> <bean name="userServiceImpl" class="com.serviceImpl.UserServiceImpl"> <!-- <property name="userDao" ref="userDaoImpl"></property> 使用了@Resource与@Autowire之后这一行就可以不用写了--> </bean> </beans>
3 让我们把所有的类全部配上,这样XML里会少很多内容
package com.serviceImpl; import javax.annotation.Resource; import org.springframework.stereotype.Component; import com.dao.UserDao; import com.entity.User; @Component public class UserServiceImpl { private UserDao userDao; public void add(User user) { userDao.save(user); } public void update(User user) { userDao.update(user); } public void init() {// 初始方法 System.out.println("init"); } public void destroy() {// 销毁方法 System.out.println("destory"); } public UserDao getUserDao() { return userDao; } @Resource(name="userDaoImpl") public void setUserDao(UserDao userDao) { this.userDao = userDao; } }
package com.daoImpl; import org.springframework.stereotype.Component; import com.dao.UserDao; import com.entity.User; @Component public class UserDaoImpl implements UserDao{ public void save(User user) { System.out.println(user.getName()+"-->"+user.getRemark()+" save --调用UserDaoImpl!"); } public void update(User user) { System.out.println(user.getName()+"-->"+user.getRemark()+" update --调用UserDaoImpl!"); } }
package com.daoImpl; import org.springframework.stereotype.Component; import com.dao.UserDao; import com.entity.User; @Component public class UserDaoImpl2 implements UserDao{ public void save(User user) { System.out.println(user.getName()+"-->"+user.getRemark()+" save --调用UserDaoImpl2!"); } public void update(User user) { System.out.println(user.getName()+"-->"+user.getRemark()+" update --调用UserDaoImpl2!"); } }
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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <context:annotation-config/> <context:component-scan base-package="com.daoImpl"></context:component-scan> <!-- 配置component路径,如果配置根目录会很方便,不过初始化速度会有点影响--> <context:component-scan base-package="com.serviceImpl"></context:component-scan> <!-- <bean name="userDaoImpl2" class="com.daoImpl.UserDaoImpl2"/> 使用了component配置这个不需要了 --> <!-- <bean name="userServiceImpl" class="com.serviceImpl.UserServiceImpl"> 使用了component配置这个不需要了 --> <!-- <property name="userDao" ref="userDaoImpl"></property> 使用了@Resource与@Autowire之后这一行就可以不用写了--> <!-- </bean>--> </beans>
测试类
package com.serviceImpl.test; import org.junit.Before; import org.junit.Test; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.entity.User; import com.serviceImpl.UserServiceImpl; public class UserServiceImplTest { User user; @Before public void setUp() throws Exception { user = new User(); user.setName("testName"); user.setRemark("testRemark"); } @Test public void testAdd() { ClassPathXmlApplicationContext app = new ClassPathXmlApplicationContext("beans.xml"); UserServiceImpl UserServiceImpl = (UserServiceImpl)app.getBean("userServiceImpl"); UserServiceImpl.add(user);//调用方法 UserServiceImpl.update(user);//调用方法 } }
执行结果
testName-->testRemark save --调用UserDaoImpl!
testName-->testRemark update --调用UserDaoImpl!
总结@Component与@Resource
1 Component是定义初始化的类,相当于<bean />里的配置,且必须在XML里配置初始化component的搜索范围
2 @Resource是定义<bean></bean>里的<proptity >里的配置
3 两者结合的时候,开发速度能够明显加快,毕竟代码量少了很多
4 @scope
1 在类的上面配置
package com.serviceImpl; import javax.annotation.Resource; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Component; import com.dao.UserDao; import com.entity.User; @Scope("singleton") @Component public class UserServiceImpl { private UserDao userDao; public void add(User user) { userDao.save(user); } public void update(User user) { userDao.update(user); } public void init() {// 初始方法 System.out.println("init"); } public void destroy() {// 销毁方法 System.out.println("destory"); } public UserDao getUserDao() { return userDao; } @Resource(name="userDaoImpl") public void setUserDao(UserDao userDao) { this.userDao = userDao; } }
执行效果不再演示
总结@scope
1 相当于在bean的类上面配置了scope,执行结果可参考笔记(一)
5@PostConstruct and @PreDestroy
package com.serviceImpl; import javax.annotation.PostConstruct; import javax.annotation.PreDestroy; import javax.annotation.Resource; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Component; import com.dao.UserDao; import com.entity.User; @Scope("singleton") @Component public class UserServiceImpl { private UserDao userDao; public void add(User user) { userDao.save(user); } public void update(User user) { userDao.update(user); } @PostConstruct public void init() {// 初始方法 System.out.println("init"); } @PreDestroy public void destroy() {// 销毁方法 System.out.println("destory"); } public UserDao getUserDao() { return userDao; } @Resource(name="userDaoImpl") public void setUserDao(UserDao userDao) { this.userDao = userDao; } }
执行结果
init
testName-->testRemark save --调用UserDaoImpl!
testName-->testRemark update --调用UserDaoImpl!
destory
总结@PostConstruct and @PreDestroy
1 相当于在bean的类上面配置了init-method,和destroy-method,在annotation里面配置则是在bean类的方法上配置即可
通过本文可以了解
@Autowire
1 @Autowire+@Qualifier tips
2 myeclipse content小技巧
@Resource
1@Resource(name,type)
2与@Autowire的区别 按名字和按类型
@Component
1@Resource+@Component
2@Resource(NAME)+@Component(NAME)
@scope
@PostConstruct and @PreDestroy