AOP

AOP

  • 面向切面编程(不改变源代码的方式对功能进行增强)
  • 有接口的情况,使用JDK动态代理
  • 没有接口的情况,私用CGLIB动态代理

JDK动态代理

public interface Human {
    void speak();
    int add(int a,int b);
}

public class User implements Human{
    @Override
    public void speak() {
        System.out.println("人说话");
    }

    @Override
    public int add(int a, int b) {
        return a+b;
    }
}
public class factoryProxy {
    public static Object getProxyInstance(Object o){
        MyinvocationHander myinvocationHander = new MyinvocationHander();
        myinvocationHander.setO(o);
        return Proxy.newProxyInstance(o.getClass().getClassLoader(),o.getClass().getInterfaces(),myinvocationHander);
    }
}

public class MyinvocationHander implements InvocationHandler {
    private Object o;

    public void setO(Object o) {
        this.o = o;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println("执行" + method.getName() + "方法之前");
        Object reslut = method.invoke(o,args);
        System.out.println("执行" + method.getName() + "方法之后");

        return reslut;
    }
}
public class Test {

    public static void main(String[] args) {
        Human human = (Human) factoryProxy.getProxyInstance(new User());
        human.speak();
        System.out.println(human.add(1, 2));
    }

}

AOP术语

  • 连接点(类中可以被增强的方法都成为连接点)
  • 切入点(实际真正被增强的方法,成为切入点)
  • 通知或增强(实际增强的逻辑部分称为通知) (前置,后置,环绕,异常,最终)
  • 切面(是一个动作,把通知应用到切入点)

AOP (AspectJ注解)

  • 1.创建类,再类中定义方法
public class User {

    public void add(){
        System.out.println("add user");
    }
}
  • 2.创建增强类(不同方法代表不同通知类型)
// 增强类
public class UserPro {

    // 前置通知
    public void before(){
        System.out.println("before");
    }
}
  • 3.进行通知的配置
  • 3.1在Spring配置中开启注解扫描
<?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:context="http://www.springframework.org/schema/context"
       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/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
    <context:component-scan base-package="annAspectJ"></context:component-scan>
</beans>
  • 3.2使用注解创建User和UserPro对象
@Component
// 被增强类
public class User {
    public void add(){
        System.out.println("add user");
    }
}

@Component
// 增强类
public class UserPro {
    // 前置通知
    public void before(){
        System.out.println("before");
    }
}
  • 3.3在增强类上添加注解@Aspect
@Aspect
@Component
// 增强类
public class UserPro {
    // 前置通知
    public void before(){
        System.out.println("before");
    }
}
  • 3.4在Spring配置中开启生成代理对象
<?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:context="http://www.springframework.org/schema/context"
       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/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
    <context:component-scan base-package="annAspectJ"></context:component-scan>
    <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>
  • 4.配置不同类型的通知
// 设置优先级,值越小越优先
@Order(1)
@Aspect
@Component
// 增强类
public class UserPro {
    // 公共切入点
    @Pointcut(value = "execution(* annAspectJ.User.add())")
    public void pointcut(){
        before();
    }

    // 前置通知
    @Before(value = "pointcut()")
    public void before(){
        System.out.println("before");
    }

    // 后置通知
    @After(value = "execution(* annAspectJ.User.add())")
    public void after(){
        System.out.println("before");
    }

    // 环绕通知
    @Around(value = "execution(* annAspectJ.User.add())")
    public void around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        System.out.println("around before");
        proceedingJoinPoint.proceed();
        System.out.println("around before");
    }

    //异常通知
    @AfterThrowing(value = "execution(* annAspectJ.User.add())")
    public void throwss() {
        System.out.println("error");
    }
}

AOP (AspectJ配置)

<?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:context="http://www.springframework.org/schema/context"
       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/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 创建对象-->
    <bean id="book" class="xmlAspectJ.Book"></bean>
    <bean id="bookpro" class="xmlAspectJ.BookPro"></bean>
    <!--配置 aop 增强-->
    <aop:config> <!--切入点-->
        <aop:pointcut id="p" expression="execution(* xmlAspectJ.Book.speak(..))"/>
        <!--配置切面-->
        <aop:aspect ref="bookpro"> <!--增强作用在具体的方法上-->
            <aop:before method="before" pointcut-ref="p"/>
            </aop:aspect>
        </aop:config>
</beans>
public class Book {

    public void speak() {
        System.out.println("book speak");
    }
}

public class BookPro {

    public void before(){
        System.out.println("before");
    }

}
public class Test2 {
    public static void main(String[] args) {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("XMl/bean2.xml");
        Book user = applicationContext.getBean("book", Book.class);
        user.speak();
    }
}

完全注解AOP

@Configuration
@ComponentScan(basePackages = {"xxx"}) 
@EnableAspectJAutoProxy(proxyTargetClass = true)
public class SpringConfig(){} 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

临水而愚

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值