Spring--AOP实现的方法

1. 业务需求

现在有一个类,类中有一些方法,现在需要在执行每个方法前执行另一个方法,输出要执行的方法名称

  1. UserService
public interface UserService {
    void add();
    void delete();
    void update();
    void select();
}
  1. UserServiceImpl
public class UserServiceImpl implements UserService{
    public void add() {
        System.out.println("增加了一个用户");
    }

    public void delete() {
        System.out.println("删除了一个用户");
    }

    public void update() {
        System.out.println("更新了一个用户");
    }

    public void select() {
        System.out.println("查询了一个用户");
    }
}

2. AOP实现

2.1 使用AOP注意事项

  1. 必须引入Maven依赖
<dependency>
    <groupId>org.aspectj</groupId>
    <artifactId>aspectjweaver</artifactId>
    <version>1.9.4</version>
</dependency>
  1. applicationContext.xml文件中加入约束
<?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"
       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.xsd">

	<!--将要使用的类注册使Spring可以管理-->
    <bean id="userService" class="com.hust.service.UserServiceImpl"/>
    <bean id="log" class="com.hust.log.Log"/>

    <!--配置AOP-->
    <aop:config>
        <!--切入点,需要执行方法的位置,execution里面的内容为要执行的位置-->
        <aop:pointcut id="pointcut" expression="execution(* com.hust.service.UserServiceImpl.*(..))"/>
        <!--执行环绕-->
        <aop:advisor advice-ref="log" pointcut-ref="pointcut"/>
    </aop:config>
</beans>

2.2 实现方法一(Spring接口实现)

使用Spring自带的API接口实现

  • 写一个Log类,去实现MethodBeforeAdvice接口,使用里面的方法去实现
public class Log implements MethodBeforeAdvice {
    /*
    method:要执行的目标对象的方法
    args:对象参数
    target:目标对象
    * */
    public void before(Method method, Object[] args, Object target) throws Throwable {
        System.out.println(target.getClass().getName() + "类的" + method.getName() + "方法被执行了");
    }
}
  • 测试
public class MyTest {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        UserService userService = context.getBean("userService", UserService.class);
        userService.add();
    }
}
/*
com.hust.service.UserServiceImpl类的add方法被执行了
增加了一个用户
*/

2.3 实现方法二(自定义类实现)

自定义切入点类

  • 自定义切入点类
public class DiyPointcut {
    public void before(){
        System.out.println("=====方法执行前=====");
    }
    
    public void after(){
        System.out.println("=====方法执行后=====");
    }
}
  • 将类注入Spring管理
<?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"
       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.xsd">

    <bean id="userService" class="com.hust.service.UserServiceImpl"/>
    <bean id="diy" class="com.hust.diy.DiyPointcut"/>
    
    <aop:config>
        <!--自定义切面,ref为要引用的类-->
        <aop:aspect ref="diy">
            <!--切入点-->
            <aop:pointcut id="point" expression="execution(* com.hust.service.UserServiceImpl.*(..))"/>
            <!--通知-->
            <aop:before method="before" pointcut-ref="point"/>
            <aop:after method="after" pointcut-ref="point"/>
        </aop:aspect>
    </aop:config>
</beans>

2.4 实现方式三(注解实现)

  • 实现切面的类
//标注这个类是一个切面
@Aspect
public class AnnotationPointcut {

    @Before("execution(* com.hust.service.UserServiceImpl.*(..))")
    public void before(){
        System.out.println("-----方法执行前-----");
    }

    @After("execution(* com.hust.service.UserServiceImpl.*(..))")
    public void after(){
        System.out.println("-----方法执行后-----");
    }
}
  • 在xml文件中注册与添加支持
<?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"
       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.xsd">

    <bean id="userService" class="com.hust.service.UserServiceImpl"/>
    <bean id="anno" class="com.hust.diy.AnnotationPointcut"/>
    
    <aop:aspectj-autoproxy/>
</beans>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值