今天我又重新复习了一下之前的知识,发现知识的遗忘速度之快让人措手不及,多做多练习你以为就能记住,搞笑了,知识点之丰富哪能面面聚到,。。。我好像在为自己开脱,好了闲话少说,在老生长谈一下这些知识吧!
先来个注解版的切面配置,这里的Aspects是切面,Aspects中的方法就是通知,CutPoint就是增强的目标,CutPoint中的方法未与通知织入的时候叫连接点,织入后叫切入点(切点)。当我们运行目标代码的方法时被切面的中的表达式(execution(...))发现就进行方法增强。
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
@Component
@Aspect
public class Aspects {
@Pointcut("execution (* com.zsc.aop..*(..))")
private void cut() {
}
@Before("cut()")
public void befores() {
System.out.println("我在前面");
}
@After("cut()")
public void afters() {
System.out.println("我在后面");
}
@Around("cut()")
public void arounds(ProceedingJoinPoint point) {
try {
point.proceed();
} catch (Throwable e) {
e.printStackTrace();
}
System.out.println("我在环绕");
}
}
@Component
public class CutPoint {
public void point() {
System.out.println("我是切点");
}
}
public class Tests {
public static void main(String[] args) {
ApplicationContext context=new ClassPathXmlApplicationContext("application.xml");
// CutPoint point = (CutPoint) context.getBean("cut");
CutPoint point = context.getBean(CutPoint.class);
point.point();
}
}
XML版Aop
<?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"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">
<context:component-scan base-package="com.zsc"></context:component-scan>
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
<!-- <bean id="aspects" class="com.zsc.aop.AMLAspects"></bean>//这是切面
<bean id="cut" class="com.zsc.aop.CutPoint"></bean>//这是要增强的切点
<aop:config>
<aop:pointcut expression="execution(* com.zsc.aop..*(..))"//这是配置找到需要增强的方法
id="cutpoin"/>
<aop:aspect id="as" ref="aspects"//引入切面 >
<aop:before method="befores"//切面中的方法 pointcut-ref="cutpoin"//引入切点/>
<aop:around method="arounds" pointcut-ref="cutpoin"/>
<aop:after method="afters" pointcut-ref="cutpoin"/>
</aop:aspect>
</aop:config> -->
</beans>
spring 中的Aop就是基于动态代理的,这里我们粘贴一些代码参考,禁忌时间过长遗忘。
jdk版动态代理目标代码一定要实现自定义的接口
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
public class DynamicProxy implements InvocationHandler {
private Object targer;
public Object bind(Object targer) {
this.targer = targer;
return Proxy.newProxyInstance(targer.getClass().getClassLoader(), targer.getClass().getInterfaces(), this);
}
@Override
public Object invoke(Object object, Method method, Object[] args) throws Throwable {
System.out.println("执行前处理");
method.invoke(targer, args);
System.out.println("执行后处理");
return null;
}
}
public interface Targer {
void add();
void delete();
}
package com.zsc.jdk;
public class TargerImpl implements Targer {
@Override
public void add() {
System.out.println("start void add()");
}
@Override
public void delete() {
System.out.println("start void delete()");
}
}
package com.zsc.jdk;
public class Test {
public static void main(String[] args) {
TargerImpl targer = new TargerImpl();
DynamicProxy dynamicProxy = new DynamicProxy();
Targer proxy = (Targer) dynamicProxy.bind(targer);
proxy.add();
proxy.delete();
}
}
cglib动态代理 不需要实现接口
package com.zsc.cglib;
import java.lang.reflect.Method;
import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;
public class DynamicProxy implements MethodInterceptor {
private Object targer;
public Object getInstance(Object targer) {
this.targer=targer;
Enhancer enhancer=new Enhancer();
enhancer.setSuperclass(targer.getClass());
enhancer.setCallback(this);
return enhancer.create();
}
@Override
public Object intercept(Object arg0, Method arg1, Object[] arg2, MethodProxy arg3) throws Throwable {
System.out.println("执行前处理");
arg3.invoke(targer, arg2);
System.out.println("执行后处理");
return null;
}
}
package com.zsc.cglib;
public class TargerImpl {
public void add() {
System.out.println("start void add()");
}
public void delete() {
System.out.println("start void delete()");
}
}
package com.zsc.cglib;
public class Test {
public static void main(String[] args) {
TargerImpl impl=new TargerImpl();
DynamicProxy dynamicProxy=new DynamicProxy();
TargerImpl targerImpl= (TargerImpl) dynamicProxy.getInstance(impl);
targerImpl.add();
targerImpl.delete();
}
}
希望自己复习的时候的能够想起来,加油老哥!