SSM整合的一些细节

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/>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值