java 代码
- package com.test;
- public interface StudentService {
- public void addStudent(String name);
- }
- package com.test;
- public class StudentServiceImpl implements StudentService{
- @Override
- public void addStudent(String name) {
- System.out.println("##增加一个姓名是" + name + "学生!");
- }
- }
- package com.test;
- import java.lang.reflect.Method;
- import org.springframework.aop.MethodBeforeAdvice;
- //调用前增强
- public class BeforeAdvice implements MethodBeforeAdvice {
- @Override
- public void before(Method arg0, Object[] arg1, Object arg2) throws Throwable {
- // TODO Auto-generated method stub
- System.out.println("##before -- 方法调入前的操作!");
- }
- }
- package com.test;
- import java.lang.reflect.Method;
- import org.springframework.aop.AfterReturningAdvice;
- //结束之后
- public class AfterAdvice implements AfterReturningAdvice {
- @Override
- public void afterReturning(Object arg0, Method arg1, Object[] arg2,
- Object arg3) throws Throwable {
- // TODO Auto-generated method stub
- System.out.println("##after -- 方法返回之后的操作");
- }
- }
- package com.test;
- import org.aopalliance.intercept.MethodInterceptor;
- import org.aopalliance.intercept.MethodInvocation;
- //环绕增强
- public class CompareInterceptor implements MethodInterceptor{
- @Override
- public Object invoke(MethodInvocation invocation) throws Throwable {
- // TODO Auto-generated method stub
- Object result = null;
- String name = invocation.getArguments()[0].toString();
- if(!name.equals("admin")) result = invocation.proceed();
- else{
- System.out.println("admin不是学生,不能加入!");
- }
- return result;
- }
- }
- package com.test;
- import org.springframework.aop.framework.ProxyFactoryBean;
- public class AopTest {
- /**
- * @param args
- */
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- StudentService service = new StudentServiceImpl();
- ProxyFactoryBean proxy = new ProxyFactoryBean();
- try {
- proxy.setProxyInterfaces(new Class[]{com.test.StudentService.class});
- } catch (ClassNotFoundException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- proxy.setProxyTargetClass(false);
- proxy.setTarget(service);
- proxy.addAdvice(new BeforeAdvice());
- proxy.addAdvice(new AfterAdvice());
- proxy.addAdvice(new CompareInterceptor());
- StudentService s = (StudentService)proxy.getObject();
- s.addStudent("test");
- System.out.println("----------------------------");
- s.addStudent("admin");
- }
- }
##before -- 方法调入前的操作!
##增加一个姓名是test学生!
##after -- 方法返回之后的操作
----------------------------
##before -- 方法调入前的操作!
admin不是学生,不能加入!
##after -- 方法返回之后的操作
http://blogs.warwick.ac.uk/colinyates/entry/performance_of_spring/
利用 Spring 和 EHCache 缓存结果(翻译)
http://www.blogjava.net/zhenyu/archive/2006/07/05/56849.html