第一种,写死代码:
public interface Greeting{void sayHello(String name);
}
public class GreetingImpl implements Greeting{
@Overide
public void sayHello(String name){
before();
System.out.println("Hello" + name);
after();
}
public void before(){
System.out.println("Before");
}
public void after(){
System.out.println("After");
}
}
第二种,静态代理
public class GreetingProxy implements Greeting{
private GreetingImpl greetingImpl;
public GreetingProxy(GreetingImpl greetingImpl){
this.greetingImpl = greetingImpl;
}
@Overide
public void sayHello(String name){
before();
System.out.println("Hello" + name);
after();
}
public void before(){
System.out.println("Before");
}
public void after(){
System.out.println("After");
}
}
public class Client {
public static void main(String [] args){
Greeting greetingProxy = new GreetingProxy(new GreetingImpl());
greetingProxy.sayHello("Jack");
}
}
第三种,JDK动态代理
public class JDKDynamicProxy implements InvacationHandler{
private Object target;
public JDKDynamicProxy(){
this.target = target;
}
@SupperessWarings("unchecked")
public <T> T getProxy(){
return (T) Proxy.newProxyInstance(target.getClass().getClassLoader(),targer.getClass().getInterfaces(),this);
}
@Overide
public Object invoke(Object proxy,Method method,Object[]args)throws Throwable{
before();
Object result = method.invoke(target,args);
after();
return result;
}
public void before(){
System.out.println("Before");
}
public void after(){
System.out.println("After");
}
}
public class Client{
public static void main(String[]args){
Greeting greeting = new JDKDynamicProxy(new GreetingImple()).getProxy();
greeting.sayHallo("Jack");
}
}
第四种,CGILib动态代理
public class CGLibDynamicProxy implments MethodInterceptor{
private static CGLibDynamicProxy intance = new CGILibDynamicProxy();
private CGLibDaynamic{}
public static CGLibDynamicProxy getInstance(){
return instance;
}
@SupperessWarings("unchecked")
public <T> T getProxy(Class<T> cls){
return (T) Enhancer.create(cls,this);
}
@Override
public Object intercept(Object target,Method method,Object[]args,MethodProxy)thows Throwable{
before();
Object result = proxy.invokeSuper(target,args);
after();
}
public void before(){
System.out.println("Before");
}
public void after(){
System.out.println("After");
}
public class Client{
public static void main(String[]args){
Greeting greeting = CGLibDynamicProxy.getInstance().getProxy(GreetingImpl.class);
greeting.sayHello("Jack");
}
}
}
Spring Aop
前置增强:
public class GreetingBeforeAdvice implements MethodBeforeAdvice{
@Overide
public void before(Method method,Object[]args,Object target) throws Throwable{
System.out.println("Before");
}
}
后置增强
public class GreetingAfterAdvice implements AfterReturngAdvice{
@Overide
public void afterReturning(Object result,Method method,Object[]args,Object target)throws Throwable{
System.out.println("After");
}
}
环绕增强
public class GreetingAroundAdvice implements MethodInterceptor{
@Overide
public Object invoke(MethodInvocation invocation)throws Throwable{
before();
Object result = invocation.proceed();
after();
return result;
}
public void before(){
System.out.println("before");
}
public void after(){
System.out.println("after");
}
}
public class Client{
public static void main(String[]args){
ProxyFactory proxyFactory = new ProxyFactory();
proxyFactory.setTarget(new GreetingImpl());
proxyFactory.addAdvice(new GreetingBeforeAdvice());
proxyFactory.addAdvice(new GreetingAfterAdvice());
Greeting greeting = (Greeting)proxyFactory.getProxy();
greeting.sayHello("Jack");
}
}
spring 配置文件编写
<!--开启注解扫描-->
<context:component-scan base-package="aop.demo">
<!--配置代理-->
<bean id="greetingProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="interfaces" value="aop.Greeting"/>
<property name="target" ref="greetingImpl"/>
<!--拦截器名称,也就是增强类名称,spring bean 的 id-->
<property name="interceptorNames">
<list>
<value>greetingAroundAdvice</value>
</list>
</property>
</bean>
public class Client{
public static void main(String[]args){
ApplicationContext context = new ClassPathXmlApplicationContext("aop/demo/spring.xml");
Greeting greeting = (Greeting)context.getBean("greetingProxy");
greeting.sayHello("Jack");//调用代理的方法
}
}
抛出增强
实现ThrowsAdvice接口即可获得目标的反射信息
上面都是对方法进行增强
对类进行增强:
public interface Apology{
void saySorry(String name);
}
@Componenet
public class GreetingIntroAdvice extends DelegatingIntroductionInterceptor implements Apology{
@Overide
public Object invoke(MethodInvocation invocation)throws Throwable{
return super.invoke(invocation);
}
@Overide
public void saySorry(){
System.out.println("Sorry!" + name);
}
}
<context:component-scan base-package="aop.demo">
<bean id="greetingProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="iterfaces" value="aop.demo.Apology"/>
<property name="target" ref="greetingImpl"/>
<property name="interceptorNames" value="greetingIntroAdvice"/>
<property name="proxyTargetClass" value="true"/>
</bean>
public class Client{
public static void main(String[]args){
ApplicationContext context = new ClassPathXmlApplicationContext("aop/demo/spring.xml");
GreetingImpl greetingImpl = (GreetingImpl)context.getBean("greetingProxy");
greetingImpl.sayHello("Jack");
Apology apology = (Apology)greetingImpl;
apology.saySorry("Jack");
}
}
Spring aop 切面
基于正则表达式的拦截条件,切面封装了增强与切点
对某个方法不拦截
<context:component-scan base-package="aop.demo"/>
<!--配置一个切面-->
<bean id="greetingAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
<property name="advice" ref="greetingAroundAdvice"/>
<property name="pattern" value="aop.demo.GreetingImpl.good.*"/>
</bean>
<bean id="greetingProxy" class="org.springframework.aop.frameWork.ProxyFactoryBean">
<property name="target" ref="greetingImpl"/>
<property name="interceptorNames" value="greetingAdvisor"/>
<property name="proxyTargetClass" value="true"/>
</bean>
//DefaultPointcutAdvisor 默认切面
//NameMatchMethodPointcutAdvisor 根据方法名称进行匹配的切面
//StaticMethodMatcherPointcutAdvisor 用于匹配静态方法的切面
Spring Aop 自动代理
<bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
<property name="beanNames" value = "*Impl"/>
<property name="interceptorNames" value="greetingAroundAdvice"/>
<property name="optimize" value="true"/><!--类有接口就JDK代理,没有接口只有类就CGLib代理-->
</bean>
SpringAOP 自动代理 (扫描切面配置)
<bean id="greetingAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
<property name="pattern" value = "aop.demo.GreetingImpl.good.*"/>
<property name="advice" ref="greetingAroundAdvice"/>
</bean>
<bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator">
<property name="optimize" value="true"/>
</bean>
Spring + AspectJ 基于注解
@Aspect
@Around("execution(* aop.demo.GreetingImpl.*(..)*)")
around(ProceedingJoinPoint pjp)
<context:component-scan base-package="aop.demo"/>
<aop:aspectj-autoproxy proxy-target-class="true"/>
其他注解:
@annotation()
@Before
@After
@Around
@AfterThrowing
@DecleareParents(value="aop.demo.GreetingImpl",defaultImpl=ApologyImpl.class) 引入增强
Spring + AspectJ 基于配置
<bean>
<bean id="greetingImpl" class="aop.demo.GreetingImpl"/>
<bean id="greetingAspect" class="aop.demo.GreetingAspect"/>
<aop:config>
<aop:aspect ref="greetingAspect">
<aop:around method="around" pointcut="execution(* aop.demo.GreetingImpl.*(..))"/>
</aop:aspect>
</aop:config>
</beans>