spring(六)AOP

本文详细介绍了Spring AOP的三种实现方式:使用API、自定义类和注解。通过配置切入点和通知,实现方法执行前后的日志记录。在测试中展示了如何通过Spring容器获取代理后的接口实例,实现切面增强功能。
摘要由CSDN通过智能技术生成

springAOP

这是spring的另一个主要的核心功能,用来提供声明式事务:允许用户自定义切面。基本原理是动态代理,代理的是接口不是一个具体的类。

1.基本概念

切面(aspect):就是一个用来拓展功能的类。
通知(advice):拓展功能的类中的方法。
目标(target):被通知对象
代理(proxy):向目标对象应用通知之后创建的对象。
切入点(pointcut):切面通知执行地点的定义。
连接点(jointpoint):与切入点匹配的执行点

2.使用AOP需要导入的包

<dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjweaver</artifactId>
        <version>1.9.4</version>
</dependency>

3.AOP实现方式一(springAPI方式)

3.1service层接口以及其实现类

接口:

public interface UserService {
    public void add();
    public void delete();
    public void update();
    public void select();
}

实现类:

public class UserServiceImpl implements UserService {
    @Override
    public void add() {
        System.out.println("增加");
    }
    @Override
    public void delete() {
        System.out.println("删除");
    }
    @Override
    public void update() {
        System.out.println("修改");
    }
    @Override
    public void select() {
        System.out.println("查询");
    }
}
3.2编写日志增强类

后置类:

public class AfterLog implements AfterReturningAdvice {
    @Override
    public void afterReturning(Object returnValue, Method method, Object[] objects, Object o1) throws Throwable {
        System.out.println("执行了" + method.getName() + "返回结果为:" + returnValue);
    }
}

前置类:

public class BeforeLog implements MethodBeforeAdvice {
    //method:要执行的目标对象的方法
    //args:参数
    //target:目标对象
    @Override
    public void before(Method method, Object[] args, Object target) throws Throwable {
        System.out.println(target.getClass().getName() + "的" + method.getName() + "被执行了");
    }
}
3.3编写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-->
    <bean id="userService" class="com.joker.service.UserServiceImpl"></bean>
    <bean id="beforelog" class="com.joker.log.BeforeLog"></bean>
    <bean id="afterlog" class="com.joker.log.AfterLog"></bean>

    <!--    使用原生api接口-->
    <!--    配置aop:需要导入aop的约束-->
<aop:config>
    <!--        切入点:expression表示在哪理执行,这是一个表达式-->
    <aop:pointcut id="pointcut" expression="execution(* com.joker.service.UserServiceImpl.*(..))"/>
    <!--        执行环绕增加-->
    <aop:advisor advice-ref="beforelog" pointcut-ref="pointcut"></aop:advisor>
    <aop:advisor advice-ref="afterlog" pointcut-ref="pointcut"></aop:advisor>
</aop:config>
</beans>
3.4测试类
public class MyTest {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");


        //动态代理的是接口所以这里必须是接口,不能象以前一样改为实现类
        UserService userService = (UserService) context.getBean("userService");
        userService.add();
    }
}

至此我们已经完成aop。在测试中,使用getbean方法的时候传入的是一个接口而不是一个具体的类。

4.AOP实现方式二(自定义类实现)

4.1定义自定义类(切面)

实现类和前面一样

public class DiyPointCut {
    public void before() {
        System.out.println("自定义before");
    }
    public void after() {
        System.out.println("自定义after");
    }
}
4.2配置文件
<?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-->
    <bean id="userService" class="com.joker.service.UserServiceImpl"></bean>
    <bean id="beforelog" class="com.joker.log.BeforeLog"></bean>
    <bean id="afterlog" class="com.joker.log.AfterLog"></bean>
<!--    方式二:自定义类(没有第一种方法强大)-->
        <bean id="diy" class="com.joker.diy.DiyPointCut"></bean>
        <aop:config>
        <!--            定义切面-->
            <aop:aspect ref="diy">
    <!--            下面是切入点-->
    <!--            expression表达式-->
                <aop:pointcut id="point" expression="execution(* com.joker.service.UserServiceImpl.*(..))"></aop:pointcut>
                <aop:before method="before" pointcut-ref="point"></aop:before>
                <aop:after method="after" pointcut-ref="point"></aop:after>
            </aop:aspect>
        </aop:config>
</beans>
4.3测试类
public class MyTest {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        //动态代理的是接口所以这里必须是接口,不能象以前一样改为实现类
        UserService userService = (UserService) context.getBean("userService");
        userService.add();
    }
}

5.AOP实现方式三(注解实现)

5.1编写自定义类
@Aspect//标注这个类是一个切面
public class AnnotationPointCut {
    @Before("execution(* com.joker.service.UserServiceImpl.*(..))")
    public void before() {
        System.out.println("注解执行前");
    }

    @After("execution(* com.joker.service.UserServiceImpl.*(..))")//参数用来定义切入点
    public void after() {
        System.out.println("注解执行后");
    }

    @Around("execution(* com.joker.service.UserServiceImpl.*(..))")
    //在环绕增强中,我们可以给定一个参数,代表我们要获取处理切入的点
    public void around(ProceedingJoinPoint jp) throws Throwable {
        System.out.println("环绕钱");
        //执行方法
        Object proceed = jp.proceed();
        System.out.println("环绕后");
    }
}
5.2配置文件
<?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-->
    <!--    注册bean-->
    <bean id="userService" class="com.joker.service.UserServiceImpl"></bean>
    <bean id="annotationPointCut" class="com.joker.diy.AnnotationPointCut"></bean>
    <!--    开启注解支持-->
    <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>
5.3测试类
public class MyTest {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        //动态代理的是接口所以这里必须是接口,不能象以前一样改为实现类
        UserService userService = (UserService) context.getBean("userService");
        userService.add();
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值