作用:运行期间对代码进行增强
优点:减少代码重复性,提高效率
底层实现:通过动态代理技术出代理对象进行增强,掉对象的方法 ,从而增强
两种技术
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/>