Spring随笔-ProxyFactoryBean
<!-- 使用JDK自带的动态代理,被代理的类必须要实现一个接口 --><bean id="loginInterceptor" /></property></bean><!-- 使用CGLIB实现动态代理,被代理的类不需要实现接口。直接操作.class文件 --><bean id="loginTarget2" value="true"></property><property name="target"><ref bean="loginTarget2" /></property><property name="interceptorNames"><list><value>loginInterceptor2</value></list></property></bean>
import org.aopalliance.intercept.MethodInterceptor;import org.aopalliance.intercept.MethodInvocation;public class CGLIBLoginInterceptor implements MethodInterceptor {public Object invoke(MethodInvocation arg0) throws Throwable {String name = (String)arg0.getArguments()[0]; if (name.equals("flash")) { System.out.println("这才是真正的用户! "); return arg0.proceed(); } else { System.out.println("非法的用户~~~ "); return null; }}}
public class LoginManager2 {public void login(String name) {System.out.println("欢迎 " + name + "登陆!"); }}
?