Spring面向切面编程(AOP)实现

百度百科:在软件业,AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译方式和运行期间动态代理实现程序功能的统一维护的一种技术。



导入AOP依赖

<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
<dependency>
    <groupId>org.aspectj</groupId>
    <artifactId>aspectjweaver</artifactId>
    <version>1.9.5</version>
</dependency>




方式一:使用Spring接口

  1. UserService
package com.spring.service;

public interface UserService {
    public void add();
    public void delete();

}
  1. UserServiceImpl
public class UserServiceImpl implements UserService {

    @Override
    public void add() {
        System.out.println("增加用户");
    }

    @Override
    public void delete() {
        System.out.println("删除用户");
    }

}
  1. Log类

假如说这个时候希望在在接口的实现中再增添某些功能,但又不希望删除修改原来的代码,所以这里后来的功能就要以一个切入点的形式进行横向切入

import org.springframework.aop.MethodBeforeAdvice;

import java.lang.reflect.Method;

public class Log implements MethodBeforeAdvice {

    //method要执行的方法,objects参数,target目标对象
    @Override
    public void before(Method method, Object[] objects, Object o) throws Throwable {
        System.out.println(o.getClass().getName()+"的"+method.getName()+"方法被执行");
    }
}
  1. AfterLog
 import org.springframework.aop.AfterReturningAdvice;

import java.lang.reflect.Method;

public class AfterLog implements AfterReturningAdvice {
    @Override
    public void afterReturning(Object o, Method method, Object[] objects, Object o1) throws Throwable {
        System.out.println("执行"+method.getName()+"方法,返回结果为"+o);
    }
}
  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
        https://www.springframework.org/schema/beans/spring-beans.xsd
         http://www.springframework.org/schema/aop
         https://www.springframework.org/schema/aop/spring-aop.xsd">



    <bean id="userService" class="com.spring.service.UserServiceImpl"/>
    <bean id="log" class="com.spring.log.Log"/>
    <bean id="afterlog" class="com.spring.log.AfterLog"/>


    <!--配置aop,需要提前导入约束-->
    <aop:config>
        <!--切入点 execution(执行位置)-->
        <aop:pointcut id="pointcut" expression="execution(* com.spring.service.UserServiceImpl.*(..))"/>

        <!--执行环绕增强-->
        <aop:advisor advice-ref="log" pointcut-ref="pointcut"/>
        <aop:advisor advice-ref="afterlog" pointcut-ref="pointcut"/>

    </aop:config>

</beans>
  1. 测试类
import com.spring.service.UserService;
import com.spring.service.UserServiceImpl;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MyTest {

    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        UserService userService = context.getBean("userService", UserService.class);
        userService.add();
    }
}




方式二:自定义类

在原来的基础上,新建一个类(切面定义)。

  1. DiyPointCut
public class DiyPointCut {
    public void before(){
        System.out.println("方法执行前");
    }

    public void after(){
        System.out.println("方法执行后");
    }

}
  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
        https://www.springframework.org/schema/beans/spring-beans.xsd
         http://www.springframework.org/schema/aop
         https://www.springframework.org/schema/aop/spring-aop.xsd">



    <bean id="userService" class="com.spring.service.UserServiceImpl"/>
    <bean id="log" class="com.spring.log.Log"/>
    <bean id="afterlog" class="com.spring.log.AfterLog"/>

    <!--方式二-->
    <bean id="diy" class="com.spring.util.DiyPointCut"></bean>
    <aop:config>
        <!--定义切面,ref引用的类-->
        <aop:aspect id="" ref="diy">
            <!--切入点-->
            <aop:pointcut id="pointcut" expression="execution(* com.spring.service.UserServiceImpl.*(..))"/>
            <!--通知-->
            <aop:before method="before" pointcut-ref="pointcut"/>
            <aop:after method="after" pointcut-ref="pointcut"/>
        </aop:aspect>
    </aop:config>

</beans> 
  1. 测试类不变,执行
    在这里插入图片描述




方式三:注解方式实现

新建一个切面类,在类上方声明@Aspect注解,在IDEA中新建文件的时候就会看到有一个Aspect类,这里选择了第二种使用@Aspect的方式。

  1. AnnoationPointCut
    在这里插入图片描述
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;

@Aspect
public class AnnoationPointCut {

    @Before("execution(* com.spring.service.UserServiceImpl.*(..))")
    public void befor(){
        System.out.println("方法执行前");
    }
    
    @After("execution(* com.spring.service.UserServiceImpl.*(..))")
    public void after(){
        System.out.println("方法执行后");
    }
}

  1. AnnoationContext.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
        https://www.springframework.org/schema/beans/spring-beans.xsd
         http://www.springframework.org/schema/aop
         https://www.springframework.org/schema/aop/spring-aop.xsd">


	 <bean id="userService" class="com.spring.service.UserServiceImpl"/>
    
    <bean id="annoationPointCut" class="com.spring.util.AnnoationPointCut"/>
    <!--注解支持-->
    <aop:aspectj-autoproxy/>

</beans>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值