Spring boot事务的使用以及缺少jar报错解决

0、使用

属性expression=“execution(service….*(…))”
1、execution(): 表达式主体 (必须加上execution)。

2、第一个*号:表示返回值类型,*号表示所有的类型。

3、包名:表示需要拦截的包名,后面的两个句点表示当前包和当前包的所有子包,cn.smd.service.impl包、子孙包下所有类的方法。

4、第二个*号:表示类名,*号表示所有的类。

5、*(…):最后这个星号表示方法名,*号表示所有的方法,后面括弧里面表示方法的参数,两个句点表示任何参数。

书写的注意事项:execution(* cn.smd.service.impl..(…))

<?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:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx"
	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-3.1.xsd
     http://www.springframework.org/schema/context  http://www.springframework.org/schema/context/spring-context-3.1.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
    
	<!-- 配置事务管理器 -->
	<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="insert*" propagation="REQUIRED" timeout="180"
				rollback-for="java.lang.Exception" />
			<tx:method name="add*" propagation="REQUIRED" timeout="180"
				rollback-for="java.lang.Exception" />
			<tx:method name="update*" propagation="REQUIRED" timeout="180"
				rollback-for="java.lang.Exception" />
			<tx:method name="delete*" propagation="REQUIRED" timeout="180"
				rollback-for="java.lang.Exception" />
		</tx:attributes>
	</tx:advice>
	<!-- 第三步:配置切入点,让通知关联切入点,即事务控制业务层的方法 -->
	<aop:config proxy-target-class="true">
		<!-- 切入点 -->
		<aop:pointcut id="businessMethod"
				expression="execution(* com.test.service.*.*(..))" />
		<!-- 切面 -->
		<aop:advisor pointcut-ref="businessMethod" advice-ref="txAdvice" />
	</aop:config>
</beans>

1、报错解决

Lookup method resolution failed; nested exception is java.lang.IllegalStateException: Failed to XXX

原因

此异常原因是因为classA中使用了项目没有导入的类,从而导致类加载失败。一般来说如果使用了没有依赖的类应该会报ClassNotFindException的错误

解决

<dependency>
    <groupId>org.aspectj</groupId>
    <artifactId>aspectjweaver</artifactId>
    <version>1.8.14</version>
</dependency>

猜你喜欢

转载自blog.csdn.net/weixin_42119415/article/details/107306870