AOP实现

12、AOP配置实现

AOP是啥?

​ AOP是面向切面编程

AOP解决了什么问题?

​ AOP解决了代码的重用问题

AOP的目标?

​ AOP的目标是在方法的前面或者后面切入一段重复的代码

execution表达式

执行( com.ch.dao.impl.UserDaoImpl.*(..))*

符号 含义
执行() 表达式的主体;
第一个” *“符号 表示返回值的类型任意;
com.ch.dao.impl AOP所切的服务的包名
UserDaoImpl 表示当前包写的UserDaoImpl类
第二个” *“ 表示方法名,*即所有方法。此处可以写死某个方法
(..) 括号表示参数,两个点表示任何参数类型

切入点表达式的主体格式为 :execution(修饰符 返回值 包.类.方法名(参数) throws异常)  修饰符可省略,throws一般省略不写

execution( * con.baibai.service..(..)); 返回值为所有类型, 指定路径为 com.baibai.service包下所有的类的所有的方法带任何参数

方式一:原生接口

1、引入依赖:Spring-webmvc、spring-aspect(或者aspectjweaver)

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.ch</groupId>
    <artifactId>spring-study</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <!-- 引入Spring依赖 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.2.5.RELEASE</version>
        </dependency>
        <!-- Junit测试依赖包 -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
        <!-- 引入lombok -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.12</version>
        </dependency>
        <!-- 使用aop所必须的一个依赖包,否则会报错 -->
<!--        <dependency>-->
<!--            <groupId>org.aspectj</groupId>-->
<!--            <artifactId>aspectjweaver</artifactId>-->
<!--            <version>1.8.9</version>-->
<!--        </dependency>-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aspects</artifactId>
            <version>4.3.25.RELEASE</version>
        </dependency>
    </dependencies>
</project>

2、编写UserDao、UserImpl

public interface UserDao {

    //增
    int add();

    //删
    int delete();

    //查
    List<User> select();

    //改
    int update();

}
public class UserDaoImpl implements UserDao {
    @Override
    public int add() {
        System.out.println("增");
        return 0;
    }

    @Override
    public int delete() {
        System.out.println("删");
        return 0;
    }

    @Override
    public List<User> select() {
        System.out.println("查");
        return null;
    }

    @Override
    public int update() {
        System.out.println("改");
        return 0;
    }
}

3、编写所要插入的代码BeforeLog、AfterLog

//在方法之前添加日志
public class BeforeLog implements MethodBeforeAdvice {

    /**
     *
     * @param method    执行被代理对象的方法
     * @param objects   方法参数
     * @param o         被代理对象
     * @throws Throwable
     */
    @Override
    public void before(Method method, Object[] objects, Object o) throws Throwable {
        /**
         * o.getClass().getName():被代理对象的名字[类名]
         * method.getName():执行了被代理对象中的名字[方法名]
         */
        System.out.println(o.getClass().getName()+"的"+method.getName()+"方法被执行了");
    }
}
public class AfterLog implements AfterReturningAdvice {
    @Override
    public void afterReturning(Object o, Method method, Object[] objects, Object o1) throws Throwable {
        System.out.println(o1.getClass().getName()+"的"+method.getName()+"方法执行了,结果为:"+o.toString());
    }
}

4、编写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">

    <!-- 将所需类注入到IOC容器 -->
    <bean id="userDaoImpl" class="com.ch.dao.impl.UserDaoImpl"></bean>
    <bean id="afterLog" class="com.ch.log.AfterLog"></bean>
    <bean id="beforeLog" class="com.ch.log.BeforeLog"></bean>

    <!-- 使用java原生接口实现 -->
    <aop:config>
        <!-- 配置切入点 -->
        <aop:pointcut id="pointcut" expression="execution(* com.ch.dao.impl.UserDaoImpl.*(..))"/>
        <!-- 配置环绕方式 -->
        <aop:advisor advice-ref="beforeLog" pointcut-ref="pointcut"/>
        <aop:advisor advice-ref="afterLog" pointcut-ref="pointcut"/>
    </aop:config>

</beans>

5、编写测试类

@Test
public void aopTest(){
    ApplicationContext context = new ClassPathXmlApplicationContext("beans2.xml");
    /**
     * 重点1、
     * Caused by: java.lang.ClassNotFoundException: org.aspectj.weaver.reflect.ReflectionWorld$ReflectionWorldException
     * 错误原因:使用aop缺包
     *
     * 重点2、
     * UserDaoImpl useDaoImpl = (UserDaoImpl) context.getBean("userDaoImpl");【报错】
     * 因为我们动态代理代理的是接口,所以不能使用实现类,不然就报错
     */

    UserDao useDaoImpl = (UserDao) context.getBean("userDaoImpl");
    System.out.println(useDaoImpl.delete());
}

方式二:自定义类

1、引入依赖:Spring-webmvc、spring-aspect(或者aspectjweaver)[同上]

2、编写UserDao、UserImpl[同上]

3、编写自定义类

/**
 * 自定义日志之类
 */
public class MyLog {

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

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

4、编写xml配置文件

<!-- 将类注入到IOC容器中 -->
<bean id="userDaoImpl" class="com.ch.dao.impl.UserDaoImpl"/>

<bean id="myLog" class="com.ch.log.MyLog"/>

<!-- aop配置 -->
<aop:config>
    <!-- 配置切面 -->
    <aop:aspect ref="myLog">
        <!-- 配置切入点 -->
        <aop:pointcut id="pointcut" expression="execution(* com.ch.dao.impl.UserDaoImpl.*(..))"/>
        <!-- 配置通知 -->
        <aop:before method="before" pointcut-ref="pointcut"/>
        <aop:after method="after" pointcut-ref="pointcut"/>
    </aop:aspect>
</aop:config>

5、编写测试类[同上]

13、AOP注解实现

1、编写自定义的切面类

/**
 * 使用注解方式实现AOP
 */
@Aspect //注解声明是一个切面
public class MyLog1 {

    @Before("execution(* com.ch.dao.impl.UserDaoImpl.*(..))")
    public void before(){
        System.out.println("===========方法执行前===========");
    }

    @After("execution(* com.ch.dao.impl.UserDaoImpl.*(..))")
    public void after(){
        System.out.println("===========方法执行后===========");
    }

    /**
     * 报错:org.springframework.aop.AopInvocationException: Null return value from advice does not match primitive return type for: public abstract int com.ch.dao.UserDao.add()
     * 如果使用了void返回,则会如上错
     * 错误原因:spring-aop,编写Aspect后,出现这个错误,这个错误就是返回类型不匹配。
     * @param jp
     * @throws Throwable
     *
     * 运行结果:
     * ===========环绕前===========
     * ===========方法执行前===========
     * 增
     * ===========环绕后===========
     * ===========方法执行后===========
     * 0
     */
    @Around("execution(* com.ch.dao.impl.UserDaoImpl.*(..))")
    public Object around(ProceedingJoinPoint jp) throws Throwable {
        System.out.println("===========环绕前===========");
        //执行方法
        Object proceed = jp.proceed();
        System.out.println("===========环绕后===========");
        return proceed;
    }
}

2、编写xml配置文件

<!-- 将所需类注入到IOC容器 -->
<bean id="userDaoImpl" class="com.ch.dao.impl.UserDaoImpl"></bean>

<!-- 将注解式的类添加到IOC容器中 -->
<bean id="myLog1" class="com.ch.log.MyLog1"/>

<!-- 开启AOP注解的支持 -->
<aop:aspectj-autoproxy/>

3、测试【同上】

原文地址:https://www.cnblogs.com/IT_CH/p/13510155.html