<context:component-scan base-package="com.txazo" />
Spring自动扫描组件,配置了它,就无需配置<context:annotation-config />。
<context:component-scan base-package="com.txazo">
<context:include-filter type="regex"
expression=".dao.impl.*" />
<context:include-filter type="regex"
expression=".service.impl.*" />
<context:include-filter type="regex"
expression=".action.*" />
</context:component-scan>
<context:annotation-config />
向Spring容器注册AutowiredAnnotationBeanPostProcessor、CommonAnnotationBeanPostProcessor、PersistenceAnnotationBeanPostProcessor 、RequiredAnnotationBeanPostProcessor这4个BeanPostProcessor,然后Spring就可以识别相应的注解。
<aop:aspectj-autoproxy />
AOP切面配置,Spring自动为配置@aspectJ切面的bean创建代理。
<bean
class="org.springframework.dao.annotation.
PersistenceExceptionTranslationPostProcessor" />
Spring对任何带有@Repository注解的对象自动激活其数据访问异常转换。
<tx:annotation-driven transaction-manager="transactionManager" />
Spring的事务管理器,可以使用@Transactional注解配置声明式事务。
Spring结合Hibernate基于注解的配置如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/
spring-context-3.0.xsd">
<!-- Spring Component Scan -->
<context:component-scan base-package="com.txazo" />
<!-- Spring AOP Aspectj Autoproxy -->
<aop:aspectj-autoproxy />
<!-- Spring DataAccessException Annotated With @Repository -->
<bean
class="org.springframework.dao.annotation.
PersistenceExceptionTranslationPostProcessor" />
<!-- SessionFactory -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.
AnnotationSessionFactoryBean"
p:configLocation="classpath:hibernate.cfg.xml" />
<!-- HibernateTemplate -->
<bean id="hibernateTemplate"
class="org.springframework.orm.hibernate3.HibernateTemplate"
p:sessionFactory-ref="sessionFactory" />
<!-- TransactionManager -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager"
p:sessionFactory-ref="sessionFactory" />
<!-- Declarative Transaction Management -->
<tx:annotation-driven transaction-manager="transactionManager" />
</beans>