spring-aop思想(基础)

作用:运行期间对代码进行增强

优点:减少代码重复性,提高效率

底层实现:通过动态代理技术出代理对象进行增强,掉对象的方法 ,从而增强

两种技术

jdk:基于接口的动态代理技术

Target target = new Target();

Methods methods = new Methods();

targetimpl tar= (targetimpl) Proxy.newProxyInstance(target.getClass().getClassLoader(), target.getClass().getInterfaces(), new InvocationHandler() {

@Override

public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

methods.Befor();

Object invoke = method.invoke(target, args);

methods.After();

return invoke;

}

});

tar.save();

cglib:基于父类的动态代理技术

Have have = new Have();

Had had = new Had();

Enhancer enhancer = new Enhancer();

enhancer.setSuperclass(have.getClass());

enhancer.setCallback(new MethodInterceptor() {

@Override

public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {

had.Befor();

Object invoke = method.invoke(have, objects);

had.After();

return invoke;

}

});

Have o = (Have) enhancer.create();

o.save();

}

面向切面思想,运用二中代理技术

概念:切入点:配增强的方法

Advice通知/增强:增强业务的逻辑

Aspect切面:切点+通知

Weaving只如:将切面和织入结合的过程

关于Aop的依赖

dependency>

groupId>org.springframeworkgroupId>

artifactId>spring-contextartifactId>

version>5.0.5.RELEASEversion>

dependency>

dependency>

groupId>org.aspectjgroupId>

artifactId>aspectjweaverartifactId>

version>1.8.4version>

dependency>

案例

切面类

public class Meths {

public void Befor() {

System.out.println("Befor///");

}

public void After() {

System.out.println("After");

}

public Object around(ProceedingJoinPoint pdj) throws Throwable {

System.out.println("Befor///");

Object proceed = pdj.proceed();

System.out.println("After");

return proceed;

}

public void afterThrowing(){

System.out.println("异常抛出增强..........");

}

public void after(){

System.out.println("最终增强..........");

}

}

目标类:

public class Target implements targetimpl {

@Override

public void save() {

System.out.println("save Running.......");

}

}

xml配置:

bean id="have" class="weige1.Aop.Have">bean>

bean id="meths" class="weige1.Aop.Meths">bean>

aop:config>

aop:aspect ref="meths">

aop:pointcut id="pointcut" expression="execution(* weige1.Aop.Have.save())"/>

aop:before method="Befor" pointcut="execution(public void weige1.Aop.Have.save())"/>

aop:after-returning method="After" pointcut="execution(public void weige1.Aop.Have.save())">aop:after-returning>

aop:around method="around" pointcut="execution( * weige1.Aop.Have.save())"/>

aop:after-throwing method="afterThrowing" pointcut-ref="pointcut"/>

aop:after method="after" pointcut-ref="pointcut"/>

aop:after method="after" pointcut-ref="pointcut"/>

aop:aspect>

aop:config>

注解Aop代理

接口:

public interface targetimpl {

public void save();

}

目标类:

@Component("target")

public class Target implements targetimpl {

@Override

public void save() {

System.out.println("save Running.......");

}

}

切面类:

@Component("methods")

@Aspect

public class Methods {

@Before("execution( void weige1.Anno.Target.save())")

public void Befor(){

System.out.println("之前");

}

public void After(){

System.out.println("之后");

}

}

XML类配置:

context:component-scan base-package="weige1.Anno"/>

aop:aspectj-autoproxy/>

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

郎伟学架构

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

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

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

打赏作者

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

抵扣说明:

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

余额充值