http://www.iteye.com/topic/295348
1 没有注解之前
public class UserManagerImpl implements UserManager { private UserDao userDao; public void setUserDao(UserDao userDao) { this.userDao = userDao; } ... }
<bean id="userManagerImpl" class="com.kedacom.spring.annotation.service.UserManagerImpl"> <property name="userDao" ref="userDao" /> </bean> <bean id="userDao" class="com.kedacom.spring.annotation.persistence.UserDaoImpl"> <property name="sessionFactory" ref="mySessionFactory" /> </bean>
2 注解诞生之后
public class UserManagerImpl implements UserManager { @Autowired private UserDao userDao; ... }
public class UserManagerImpl implements UserManager { private UserDao userDao; @Autowired public void setUserDao(UserDao userDao) { this.userDao = userDao; } ... }
<bean id="userManagerImpl" class="com.kedacom.spring.annotation.service.UserManagerImpl" /> <bean id="userDao" class="com.kedacom.spring.annotation.persistence.UserDaoImpl"> <property name="sessionFactory" ref="mySessionFactory" /> </bean>
要使@Autowired能够工作,还需要在配置文件中加入以下代码
<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" />
可以看到确实是减少了代码和配置文件中的配置。
<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-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <context:annotation-config/> </beans>
2 @PostConstruct和PreDestroy:通过注解指定初始化和销毁方法定义
用于注入EntityManagerFactory和EntityManager
@Autowired public void setUserDao(@Qualifier("userDao") UserDao userDao) { this.userDao = userDao; }
@Autowired(required = false) public void setUserDao(UserDao userDao) { this.userDao = userDao; }
public class UserDaoImpl extends HibernateDaoSupport implements UserDao { private SessionFactory mySessionFacotry; @Resource public void setMySessionFacotry(SessionFactory sessionFacotry) { this.mySessionFacotry = sessionFacotry; } @PostConstruct public void injectSessionFactory() { super.setSessionFactory(mySessionFacotry); } ... }
<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 /> </beans>
@Component public class UserDaoImpl extends HibernateDaoSupport implements UserDao { ... }
<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:component-scan base-package="com.kedacom.ksoa" /> </beans>
<context:component-scan base-package="com.casheen.spring.annotation"> <context:exclude-filter type="regex" expression="com\.casheen\.spring\.annotation\.web\..*" /> </context:component-scan>
@Scope("session") @Component() public class UserSessionBean implements Serializable { ... }
为了加深印象,又增加了一个完整的例子。
例子来源
http://blog.csdn.net/pk490525/article/details/8096326
spring零配置(Annotation)学习笔记
本地例子:
AnnotationTest
本地有细小的改变
project用到的jar包
先上bean-config.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-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <context:annotation-config/> <!-- action暂未用注解 --> <bean id="myAction" class="com.demo.annotation.action.MyAction" scope="prototype" /> <!-- 注解测试 --> <context:component-scan base-package="com.demo.annotation" /> </beans>
/** * * Annotation * */ public interface TestService { /** * 注解测试 * @return */ public String getTestAnnotation(); }
import org.springframework.stereotype.Service; import com.demo.annotation.dao.TestDao; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; /** * * 注解测试 * */ @Service("testService") public class TestServiceImp implements TestService { /** * 自动装配 */ @Autowired @Qualifier("testDao") //@Resource(name="testDao"), 等价于<property ………… ref="testDao" /> private TestDao testDao; public TestDao getTestDao() { return testDao; } public void setTestDao(TestDao testDao) { this.testDao = testDao; } @Override public String getTestAnnotation() { // TODO Auto-generated method stub return testDao.getTestDaoAnnotation(); } }
/** * 测试注解 * */ public interface TestDao { /** * 得到dao层注解 * @return */ public String getTestDaoAnnotation(); }
import org.springframework.stereotype.Repository; /** * 测试Annotation * */ @Repository("testDao") public class TestDaoImpl implements TestDao { @Override public String getTestDaoAnnotation() { // TODO Auto-generated method stub return "This is testDao Annotation..."; } }
import javax.annotation.Resource; import com.demo.annotation.service.TestService; public class MyAction{ @Resource(name="testService") private TestService testService; public String testAnnotation(){ if(null == testService){ System.out.println("Service is null!!"); } String result = testService.getTestAnnotation(); System.out.println(result); return "success"; } public TestService getTestService() { return testService; } public void setTestService(TestService testService) { this.testService = testService; } }
import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.demo.annotation.action.MyAction; public class TestAnnotation { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("bean-config.xml"); MyAction action = (MyAction)context.getBean("myAction"); action.testAnnotation(); } }
七 总结
Annotation的好处:
简化了xml文件
坏处:
1 spring一直宣称是无侵入的,annotation是否是侵入了?
2 很多类都是pojo的对象,引入annotation后还是pojo么?
3 增加了复杂度
spring官方的态度是两种让你混合着用。
最后:
推荐