Annotation方式配置AOP

package com.xk.spring.kp04_aop.aop.s02_annotation;

public interface IStudentService {
    public void save(Student stu);
    public void update(Student stu);
}
package com.xk.spring.kp04_aop.aop.s02_annotation;

import org.springframework.stereotype.Service;

@Service
public class StudentServiceImpl implements IStudentService {
    @Override
    public void save(Student stu) {
        System.out.println("调用Dao的save方法....");
    }
    @Override
    public void update(Student stu) {
        System.out.println("调用Dao的update方法...");
        /*@SuppressWarnings("unused")
        int i = 10/0;*/
    }
}
package com.xk.spring.kp04_aop.aop.s02_annotation;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

//自定义切面
@Aspect
@Component //一般写在工具类上面
public class TransactionManager {
    @Pointcut("execution(* com.xk.spring.kp04_aop.aop.s02_annotation.*Service.*(..))")
    public void stuService(){}//此方法名为execution表达式的别名,即id="";
    /**
     * 事物开始
     */
    @Before("stuService()")//
    public void begin(){
        System.out.println("TransactionManager.begin()");
    }
    /**
     * 事物提交
     */
    @AfterReturning("stuService()")
    public void commit(){
        System.out.println("TransactionManager.commit()");
    }
    /**
     * 事物回滚
     */
    @AfterThrowing(value = "stuService()",throwing = "e")
    public void rollback(Throwable e){
        System.out.println("TransactionManager.rollback()");
        System.out.println("rollback()");
    }
    
    /**
     * 事物结束
     */
    @After("stuService()")
    public void finished(){
        System.out.println("TransactionManager.close()");
    }
    @Around("stuService()")
    public void all(ProceedingJoinPoint point){
        try {
            begin();
            point.proceed();//此处有连接,必须写,不然执行到此将不再向下执行
            commit();
        } catch (Throwable e) {
            rollback(e);
        }finally{
            finished();
        }
        
    }
}
package com.xk.spring.kp04_aop.aop.s02_annotation;

public class Student {
    private String name;
    private Integer age;

    public Student() {

    }

    public Student(String name, Integer age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student [name=" + name + ", age=" + age + "]";
    }
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context.xsd
           http://www.springframework.org/schema/aop
           http://www.springframework.org/schema/aop/spring-aop.xsd">
     <!-- 自动生成代理 -->
    <aop:aspectj-autoproxy />
    <!-- 指定IOC扫描的包 -->
    <context:component-scan 
                base-package="com.xk.spring.kp04_aop.aop.s02_annotation"/>
</beans>
package com.xk.spring.kp04_aop.aop.s02_annotation;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
public class AOPAnnoationTest {
    @Autowired
    IStudentService seriver;

    @Test
    public void testAOPXML() throws Exception {
        seriver.save(new Student("张三", 18));
        seriver.update(new Student("张4", 18));
    }
}
原文地址:https://www.cnblogs.com/huike/p/6636986.html