普通代理的实现:
Buy.java}
Test.java
package com.proxyExample;
import java.lang.reflect.Proxy;
public class Test {
public static void main(String args[]) {
Buy bb = new BuyImpl();
BuyProxy pro = new BuyProxy(bb);
Buy go = (Buy) Proxy.newProxyInstance(bb.getClass().getClassLoader(),
bb.getClass().getInterfaces(), pro);
go.buyThing("买烤鸡,烤鸭,红烧牛排");
}
}
AOP代理的实现:
AOP在Spring中占有很重要的地位,做了一个例子是利用AOP来做一个登陆的身份验证,希望能对大家有所帮助,使用了AOP可以在不破坏你的代码的前提下帮你完成验证功能。
转一点AOP的知识:
1、Spring只支持方法拦截,也就是说,只能在方法的前后进行拦截,而不能在属性前后进行拦截。
2、Spring支持四种拦截类型:目标方法调用前(before),目标方法调用后(after),目标方法调用前后(around),以及目标方法抛出异常(throw)。
3、前置拦截的类必须实现MethodBeforeAdvice接口,实现其中的before方法。
4、后置拦截的类必须实现AfterReturningAdvice接口,实现其中的afterReturning方法。
5、前后拦截的类必须实现MethodInterceptor接口,实现其中的invoke方法。前后拦截是唯一可以控制目标方法是否被真正调用的拦截类型,也可以控制返回对象。而前置拦截或后置拦截不能控制,它们不能印象目标方法的调用和返回。
Student.java
package com.aop;
public interface Student {
public void addStudent(String name);
}
StudentImpl.java
package com.aop;
public class StudentImpl implements Student {
public void addStudent(String name) {
System.out.println("欢迎"+name+"来到spring");
}
}
BeforeAdvice.java
package com.aop;
import java.lang.reflect.Method;
import org.springframework.aop.MethodBeforeAdvice;
public class BeforeAdvice implements MethodBeforeAdvice {
public void before(Method arg0, Object[] arg1, Object arg2) throws Throwable {
System.out.println(" 这是BeforeAdvice类的before方法. ");
}
}
AfterAdvice.java
package com.aop;
import java.lang.reflect.Method;
import org.springframework.aop.AfterReturningAdvice;
public class AfterAdvice implements AfterReturningAdvice {
public void afterReturning(Object arg0, Method arg1, Object[] arg2, Object arg3) throws Throwable {
System.out.println("这是AfterAdvice类的afterReturning方法.");
}
}
CompareInterceptor.java
package com.aop;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
public class CompareInterceptor implements MethodInterceptor {
public Object invoke(MethodInvocation arg0) throws Throwable {
Object result = null;
String stu_name = arg0.getArguments()[0].toString();
if (stu_name.equals("")||stu_name==null) {
result = arg0.proceed();
} else {
System.out.println("此学生是" + stu_name);
}
return result;
}
}
Test.java
package com.aop;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
public class Test {
public static void main(String[] args) {
ApplicationContext ctx = new FileSystemXmlApplicationContext("/src/applicationContext.xml");
Student s = (Student) ctx.getBean("student");
s.addStudent("kava");
}
}
ApplicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<bean id="beforeAdvice" class="com.aop.BeforeAdvice"></bean>
<bean id="afterAdvice" class="com.aop.AfterAdvice"></bean>
<bean id="compareInterceptor" class="com.aop.CompareInterceptor"></bean>
<bean id="studenttarget" class="com.aop.StudentImpl"></bean>
<bean id="student"
class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="proxyInterfaces">
<value>com.aop.Student</value>
</property>
<property name="interceptorNames">
<list>
<value>beforeAdvice</value>
<value>afterAdvice</value>
<value>compareInterceptor</value>
</list>
</property>
<property name="target">
<ref bean="studenttarget" />
</property>
</bean>
</beans>
对CompareInterceptor.java进行处理可以增加控制逻辑,实现权限控制
package com.aop;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
public class CompareInterceptor implements MethodInterceptor {
public Object invoke(MethodInvocation arg0) throws Throwable {
Object result = null;
String stu_name = arg0.getArguments()[0].toString();
if (stu_name.equals("")||stu_name==null) {
return mapping.findForward("login");
} else {
System.out.println("此学生是" + stu_name);
return result = arg0.proceed();
}
}
}
可参考例子:
http://lighter.javaeye.com/blog/42673
http://kukuqiu.javaeye.com/blog/163199