SSM整合
配置文件中的若干细节
开启包扫描注解
service层必须在applicationContext.xml中开启包扫描,不然事务不生效
< context: component-scan base-package = " com.wyq.test.service" />
controller层必须在springMVC中开启宝扫描,不然配置无效,并且在springMVC中可以覆盖applicationContext.xml的配置,导致事务无效,所以在该配置文件中仅配controller层包扫描即可。
< context: component-scan base-package = " com.wyq.test.controller" />
mybatis与spring整合
包扫描必须配在applicationContext中,在mybatis-config.xml配置无效
< bean class = " org.mybatis.spring.mapper.MapperScannerConfigurer" >
< property name = " basePackage" value = " com.wyq.test.mapper" > </ property>
</ bean>
@Service最好写在接口上
spring默认使用jdk的动态代理,如果,把@Service写在类上,配置切入点时,会报错,原因找不到该Bean
Caused by: org. springframework. beans. factory. BeanCreationException: Error creating bean with name 'org.springframework.aop.support.DefaultBeanFactoryPointcutAdvisor#0' : Initialization of bean failed; nested exception is org. springframework. beans. factory. BeanDefinitionStoreException: Invalid bean name 'txAdvice' in bean reference for bean property 'adviceBeanName'
事务整合
< bean id = " transactionManager" class = " org.springframework.jdbc.datasource.DataSourceTransactionManager" >
< property name = " dataSource" ref = " dataSource" />
</ bean>
< tx: advice id = " txAdvice" transaction-manager = " transactionManager" >
< tx: attributes>
< tx: method name = " get*" read-only = " true" />
< tx: method name = " list*" read-only = " true" />
< tx: method name = " query*" read-only = " true" />
< tx: method name = " *" propagation = " REQUIRED" />
</ tx: attributes>
</ tx: advice>
< aop: config>
< aop: pointcut expression = " execution(* com.wyq.test.service.*Service.*(..))" id = " txPoint" />
< aop: advisor advice-ref = " txAdvice" pointcut-ref = " txPoint" />
</ aop: config>
< bean id = " transactionManager" class = " org.springframework.jdbc.datasource.DataSourceTransactionManager" >
< property name = " dataSource" ref = " dataSource" />
</ bean>
< aop: aspectj-autoproxy/>
< tx: annotation-driven/>