AOP
什么是AOP:

在Spring中使用AOP:

使用Spring实现AOP需要导入一个包:
<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
<dependency>
    <groupId>org.aspectj</groupId>
    <artifactId>aspectjweaver</artifactId>
    <version>1.9.6</version>
</dependency>
1.方式一:使用Spring的API接口

UserSerice接口:
package com.kakafa.service;
public interface UserService {
    public void add();
    public void delete();
    public void update();
    public void select();
}
UserServiceImpl:
package com.kakafa.service;
public class UserServiceImpl {
    public void add(){
        System.out.println("增加!");
    }
    public void delete(){
        System.out.println("删除!");
    }
    public void update(){
        System.out.println("修改!");
    }
    public void select(){
        System.out.println("选择!");
    }
}
Log:
package com.kakafa.log;
import org.springframework.aop.MethodBeforeAdvice;
import java.lang.reflect.Method;
public class Log 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()+"的方法被执行了");
    }
}
AfterLog:
package com.kakafa.log;
import org.springframework.aop.AfterReturningAdvice;
import java.lang.reflect.Method;
public class AfterLog implements AfterReturningAdvice {
    //returnValue:返回值
    @Override
    public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
        System.out.println(target.getClass().getName()+"这个类的"+method.getName()+"的方法被执行了,并且返回值为"+returnValue);
    }
}
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-->
    <bean id="userServiceImpl" class="com.kakafa.service.UserServiceImpl"/>
    <bean id="log" class="com.kakafa.log.Log"/>
    <bean id="afterLog" class="com.kakafa.log.AfterLog"/>
    <!--配置AOP,需要导入app的约束-->
    <aop:config>
        <!--切入点:指定在哪个地方执行;expression:表达式,execution(要执行的位置)-->
        <aop:pointcut id="pc1" expression="execution(* com.kakafa.service.UserServiceImpl.*(..))"/>
        <!--执行环绕增加-->
        <aop:advisor advice-ref="log" pointcut-ref="pc1"/>
        <aop:advisor advice-ref="afterLog" pointcut-ref="pc1"/>
    </aop:config>
</beans>

test:
import com.kakafa.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");
        UserServiceImpl usl = context.getBean("userServiceImpl", UserServiceImpl.class);
        usl.add();
    }
}

2.方式二:自定义类来实现AOP

DiyPointcut:
package com.kakafa.diy;
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
       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="userServiceImpl" class="com.kakafa.service.UserServiceImpl"/>
    <bean id="diy" class="com.kakafa.diy.DiyPointcut"/>
    <aop:config>
        <aop:aspect ref="diy">
            <!--切入点-->
            <aop:pointcut id="pc2" expression="execution(* com.kakafa.service.UserServiceImpl.*(..))"/>
            <!--通知-->
            <aop:before method="before" pointcut-ref="pc2"/>
            <aop:after method="after" pointcut-ref="pc2"/>
        </aop:aspect>
    </aop:config>
</beans>

3.使用注解实现AOP

AnnotationPointcut:
package com.kakafa.diy;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
//@Aspect标注这是一个切面
@Aspect
public class AnnotationPointcut {
    @Before("execution(* com.kakafa.service.UserServiceImpl.*(..))")
    public void before(){
        System.out.println("============(注解)方法执行前===========");
    }
    @After("execution(* com.kakafa.service.UserServiceImpl.*(..))")
    public void after(){
        System.out.println("============(注解)方法执行后===========");
    }
    //在环绕增强中,我们可以给定一个参数,代表我们要获取处理切入的点
    @Around("execution(* com.kakafa.service.UserServiceImpl.*(..))")
    public void around(ProceedingJoinPoint jp) throws Throwable {
        System.out.println("环绕前");
        //执行方法
        Object proceed = jp.proceed();
        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
       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="userServiceImpl" class="com.kakafa.service.UserServiceImpl"/>
    <bean id="annotationPointcut" class="com.kakafa.diy.AnnotationPointcut"/>
    <!--开启注解支持-->
    <aop:aspectj-autoproxy/>
</beans>

 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号