spring之aop注解开发异常@AfterThrowing is not a valid AspectJ annotation问题

@aspect类

/**
 * @Date 2019/9/9 2:27
 * by mocar
 */
@Component
@Aspect
public class TransactionManager {
    @Autowired
    private ConnectionUtils connectionUtils;
    @Pointcut("execution(* com.itcast.service.impl.*.*(..))")
    private void pt1(){}

    @Before("pt1()")
    public void beginTransaction(){
        try {
            connectionUtils.getThreadConnection().setAutoCommit(false);//开启事务
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    @AfterReturning("pt1()")
    public void commit(){
        try {
            connectionUtils.getThreadConnection().commit();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    @AfterThrowing()
    public void rollback(){
        try {
            connectionUtils.getThreadConnection().rollback();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @After("pt1()")
    public void release(){
        try {
            //从dataSource获取的connection释放,但是线程中还绑定着这个无效connection连接,也需要释放线程绑定的连接
            connectionUtils.getThreadConnection().close();
            connectionUtils.removeThreadConnection();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    //@Around("pt1()")
    public Object around(ProceedingJoinPoint pjp){
        Object rsValues=null;
        Object[] args = pjp.getArgs();
        try {
            beginTransaction();
            rsValues=pjp.proceed(args);
            commit();
        } catch (Throwable e) {
            //throwable.printStackTrace();
            rollback();
            throw new RuntimeException(e);
        }finally {
            release();

        }
        return rsValues;
    }

}

测试:

spring整合junit

/**
 * @Date 2019/9/8 3:34
 * by mocar
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:bean.xml")
public class AccountServiceTest {
    
    @Autowired
    private  AccountService as=null;

    @Test
    public void testTarget(){

        as.transfer("aaa","bbb",100f);

    }
}

报错信息:

Caused by: 
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'accountDao' defined in file [D:\idea_project\spring\springaoptx_aonnotation\target\classes\com\itcast\dao\impl\AccountDaoImpl.class]: BeanPostProcessor before instantiation of bean failed; nested exception is 

java.lang.IllegalArgumentException: @org.aspectj.lang.annotation.AfterThrowing(throwing=, pointcut=, value=, argNames=) is not a valid AspectJ annotation

报的:is not a valid AspectJ annotation

查看了配置,没有问题啊。。。

只知道这四个注解执行顺序有问题

 

然后决定试试环绕通知手动编码生成动态代理

/**
 * @Date 2019/9/9 2:27
 * by mocar
 */
@Component
@Aspect
public class TransactionManager {
    @Autowired
    private ConnectionUtils connectionUtils;
    @Pointcut("execution(* com.itcast.service.impl.*.*(..))")
    private void pt1(){}

    //@Before("pt1()")
    public void beginTransaction(){
        try {
            connectionUtils.getThreadConnection().setAutoCommit(false);//开启事务
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    //@AfterReturning("pt1()")
    public void commit(){
        try {
            connectionUtils.getThreadConnection().commit();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    //@AfterThrowing()
    public void rollback(){
        try {
            connectionUtils.getThreadConnection().rollback();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //@After("pt1()")
    public void release(){
        try {
            //从dataSource获取的connection释放,但是线程中还绑定着这个无效connection连接,也需要释放线程绑定的连接
            connectionUtils.getThreadConnection().close();
            connectionUtils.removeThreadConnection();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Around("pt1()")
    public Object around(ProceedingJoinPoint pjp){
        Object rsValues=null;
        Object[] args = pjp.getArgs();
        try {
            beginTransaction();
            rsValues=pjp.proceed(args);
            commit();
        } catch (Throwable e) {
            //throwable.printStackTrace();
            rollback();
            throw new RuntimeException(e);
        }finally {
            release();

        }
        return rsValues;
    }

}

 

居然通过了!但是不知道 @AfterThrowing我哪里配置有问题

 

 

 

 

 

好的,我会根据您的要求回答您的问题。 首先,@AfterThrowingSpring AOP中的一种通知类型,用于在目标方法抛出异常后执行一些操作。要实现@AfterThrowing通知,我们需要创建一个类,并在该类中编写一个带有@AfterThrowing注释的方法。该方法应该接受一个Throwable类型的参数,以便我们可以获得抛出的异常。 下面是一个示例: ```java import org.aspectj.lang.annotation.AfterThrowing; import org.aspectj.lang.annotation.Aspect; @Aspect public class ExceptionLogger { @AfterThrowing(pointcut = "execution(* com.example.*.*(..))", throwing = "ex") public void logException(Exception ex) { // 在这里记录日志或执行其他操作 System.out.println("Exception caught: " + ex.getMessage()); } } ``` 在上面的代码中,我们定义了一个名为ExceptionLogger的类,并使用@Aspect注释将其标记为切面。然后,我们定义了一个名为logException的方法,并使用@AfterThrowing注释将其标记为异常通知。此方法使用execution pointcut表达式指定了要拦截的方法,该表达式将拦截com.example包中的所有方法。我们还指定了一个throwing参数,它将Throwable类型的异常对象传递给我们的方法。 为了验证这个通知是否正常工作,我们可以创建一个简单的Spring应用程序,并在ApplicationContext中注册ExceptionLogger类。然后,我们可以调用一个会抛出异常的方法,并观察是否记录了日志。 以下是一个示例应用程序的代码: ```java import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class App { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); MyService service = context.getBean("myService", MyService.class); try { service.doSomething(); } catch (Exception e) { // ignore } } } ``` 在上面的代码中,我们创建了一个名为MyService的类,其中包含一个会抛出异常的方法。然后,我们在Spring ApplicationContext中注册了ExceptionLogger类,并从中获取了MyService的实例。最后,我们调用了MyService的doSomething方法,并在catch块中捕获了抛出的异常。 如果一切正常,我们应该会在控制台上看到以下输出: ``` Exception caught: Something went wrong! ``` 这表明我们的异常通知已经拦截了抛出的异常,并执行了我们定义的操作。 希望这个示例可以帮助您理解如何实现@AfterThrowing异常通知并在Spring应用程序中使用它。
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值