Spring AOP详解

1、AOP原理

动态代理

在不改变原有业务的情况下增加其他功能。

2、使用Spring实现AOP

2.1 准备

  • 新建maven项目,在maven项目中新建模块spring-03-study。
  • 配置pom.xml文件,添加依赖
<dependencies>
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjweaver</artifactId>
        <version>1.9.2</version>
    </dependency>
</dependencies>
  • 在java目录下新建com.wang.service包,包内创建接口UserService和接口实现类UserServiceImp
//接口
public interface UserService {
    public void add();
    public void delete();
    public void update();
    public void select();
}
//接口实现类
public class UserServiceImp 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.2 实现方式一:使用Spring原生的API

  • 在java目录下新建com.wang.Log包,包内新建日志增强类Log.java,使该类实现Spring自带的
public class Log implements MethodBeforeAdvice {
    /**
     *
     * @param method 要执行的目标对象的方法
     * @param objects 参数
     * @param o 目标对象
     * @throws Throwable
     */
    public void before(Method method, Object[] objects, Object o) throws Throwable {
        System.out.println(o.getClass().getName()+"的"+method.getName()+"被执行了");
    }
}
  • 在resources目录下新建applicationContext.xml,注册beans以及配置AOP

<?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来创建对象,在Spring这些都成为bean-->
    <bean id="userService" class="com.wang.service.UserServiceImp"/>
    <bean id="log" class="com.wang.Log.Log"/>

    <!--  方式一:使用Spring原生API接口  -->
    <!-- 配置AOP:需要导入AOP的约束-->
    <aop:config>
        <!--切入点-->
        <aop:pointcut id="pointcut" expression="execution(* com.wang.service.UserServiceImp.*(..))"/>
        <!-- 将log类切入到pointcut,在执行pointcut方法前会执行log类里的方法-->
        <aop:advisor advice-ref="log" pointcut-ref="pointcut"/>

    </aop:config>
</beans>
  • 新建测试类

public class MyTest {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        UserService userService = (UserService) context.getBean("userService");
        userService.add();
    }
}
  • 运行测试类,输出以下结果

com.wang.service.UserServiceImp的add被执行了
增加了一个用户

2.3 实现方式二:使用自定义类

  • 自定义切面,切面是一个类,类里面包含要在切点前后增加的功能(即在不改变原有业务的情况下增加一些其他的功能,例如日志)。
public class DiyPointCut {
    public void before(){
        System.out.println("=======方法执行前======");
    }
    public void after(){
        System.out.println("=======方法执行后======");
    }
}
  • 配置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来创建对象,在Spring这些都成为bean-->
    <bean id="userService" class="com.wang.service.UserServiceImp"/>

<!--    方式二:自定义类-->
    <bean id="diy" class="com.wang.diy.DiyPointCut"/>
    <aop:config>
        <!--自定义切面,切面里存放业务附加的功能-->
        <aop:aspect ref="diy">
            <!--切点-->
            <aop:pointcut id="point" expression="execution(* com.wang.service.UserServiceImp.*(..))"/>
            <!--通知-->
            <!--在切点前要做的事情-->
            <aop:before method="before" pointcut-ref="point"/>
            <!--在切点后要做的事情-->
            <aop:after method="after" pointcut-ref="point"/>
        </aop:aspect>
    </aop:config>

</beans>
  • 运行测试类(与2.2中一样),结果输出如下
=======方法执行前======
增加了一个用户
=======方法执行后======
  • 方法二比方法一的好处是可以把所有要增加的功能放在一个类里面(即切面里),而方法一中,每一个功能都要新建一个类,实现原生API接口。

2.3 实现方式三:使用注解

  • 自定义切面AnnotationPointCut类,使用注解标注该类是切面,而不用再在xml文件中配置
@Aspect //标注这个类是个切面
public class AnnotationPointCut {
    @Before("execution(* com.wang.service.UserServiceImp.*(..))")
    public void before(){
        System.out.println("=======方法执行前=======");
    }
}
  • 配置applicationContext.xml文件,主要是注册bean以及开启注解支持
<?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来创建对象,在Spring这些都成为bean-->
    <bean id="userService" class="com.wang.service.UserServiceImp"/>

    <!--方式三:使用注解-->
    <bean id="annotationPointCut" class="com.wang.diy.AnnotationPointCut"/>
    <!--开启注解支持-->
    <aop:aspectj-autoproxy/>
</beans>
  • 运行测试类(与2.2中一样),结果输出如下
=======方法执行前=======
增加了一个用户

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值