注:本例中Client.java、UserManager.java、UserManagerImpl.java、applicationContext.xml 4个类的代码和上一篇日志一致
spring对AOP的支持
Aspect默认情况下不用实现接口,但对于目标对象(UserManagerImpl.java),在默认情况下必须实现接口
如果没有实现接口必须引入CGLIB库
我们可以通过Advice中添加一个JoinPoint参数,这个值会由spring自动传入,从JoinPoint中可以取得
参数值、方法名等等
SecutityHandler.java代码如下:
package com.wdh.spring;
import org.aspectj.lang.JoinPoint;
public class SecurityHandler {
public void checkSecurity(JoinPoint joinPoint){
//获得参数
Object[] args = joinPoint.getArgs();
for(int i=0;i<args.length;i++){
System.out.println(args);
}
//获得方法名称
System.out.println(joinPoint.getSignature().getName());
System.out.println("=========checkSecurity()==========");
}
}
测试结果如下:
2009-05-18 20:50:21,835 INFO [org.springframework.core.CollectionFactory] - JDK 1.4+ collections available
2009-05-18 20:50:21,876 INFO [org.springframework.beans.factory.xml.XmlBeanDefinitionReader] - Loading XML bean definitions from class path resource [applicationContext.xml]
2009-05-18 20:50:22,120 INFO [org.springframework.context.support.ClassPathXmlApplicationContext] - Bean factory for application context [org.springframework.context.support.ClassPathXmlApplicationContext;hashCode=31476927]: org.springframework.beans.factory.support.DefaultListableBeanFactory defining beans [org.springframework.aop.config.internalAutoProxyCreator,securityhandler,usermanager,allAddMethod,org.springframework.aop.aspectj.AspectJPointcutAdvisor]; root of BeanFactory hierarchy
2009-05-18 20:50:22,132 INFO [org.springframework.context.support.ClassPathXmlApplicationContext] - 5 beans defined in application context [org.springframework.context.support.ClassPathXmlApplicationContext;hashCode=31476927]
2009-05-18 20:50:22,142 INFO [org.springframework.aop.framework.DefaultAopProxyFactory] - CGLIB2 not available: proxyTargetClass feature disabled
2009-05-18 20:50:22,202 INFO [org.springframework.context.support.ClassPathXmlApplicationContext] - Bean 'org.springframework.aop.config.internalAutoProxyCreator' is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2009-05-18 20:50:22,203 INFO [org.springframework.context.support.ClassPathXmlApplicationContext] - Unable to locate MessageSource with name 'messageSource': using default [org.springframework.context.support.DelegatingMessageSource@9b42e6]
2009-05-18 20:50:22,206 INFO [org.springframework.context.support.ClassPathXmlApplicationContext] - Unable to locate ApplicationEventMulticaster with name 'applicationEventMulticaster': using default [org.springframework.context.event.SimpleApplicationEventMulticaster@17431b9]
2009-05-18 20:50:22,207 INFO [org.springframework.beans.factory.support.DefaultListableBeanFactory] - Pre-instantiating singletons in factory [org.springframework.beans.factory.support.DefaultListableBeanFactory defining beans [org.springframework.aop.config.internalAutoProxyCreator,securityhandler,usermanager,allAddMethod,org.springframework.aop.aspectj.AspectJPointcutAdvisor]; root of BeanFactory hierarchy]
Jack
123
addUser
=========checkSecurity()==========
-------UserManagerImpl.addUser()----------
-------UserManagerImpl.deleteUser()----------
spring对AOP的支持
Aspect默认情况下不用实现接口,但对于目标对象(UserManagerImpl.java),在默认情况下必须实现接口
如果没有实现接口必须引入CGLIB库
我们可以通过Advice中添加一个JoinPoint参数,这个值会由spring自动传入,从JoinPoint中可以取得
参数值、方法名等等
SecutityHandler.java代码如下:
package com.wdh.spring;
import org.aspectj.lang.JoinPoint;
public class SecurityHandler {
public void checkSecurity(JoinPoint joinPoint){
//获得参数
Object[] args = joinPoint.getArgs();
for(int i=0;i<args.length;i++){
System.out.println(args);
}
//获得方法名称
System.out.println(joinPoint.getSignature().getName());
System.out.println("=========checkSecurity()==========");
}
}
测试结果如下:
2009-05-18 20:50:21,835 INFO [org.springframework.core.CollectionFactory] - JDK 1.4+ collections available
2009-05-18 20:50:21,876 INFO [org.springframework.beans.factory.xml.XmlBeanDefinitionReader] - Loading XML bean definitions from class path resource [applicationContext.xml]
2009-05-18 20:50:22,120 INFO [org.springframework.context.support.ClassPathXmlApplicationContext] - Bean factory for application context [org.springframework.context.support.ClassPathXmlApplicationContext;hashCode=31476927]: org.springframework.beans.factory.support.DefaultListableBeanFactory defining beans [org.springframework.aop.config.internalAutoProxyCreator,securityhandler,usermanager,allAddMethod,org.springframework.aop.aspectj.AspectJPointcutAdvisor]; root of BeanFactory hierarchy
2009-05-18 20:50:22,132 INFO [org.springframework.context.support.ClassPathXmlApplicationContext] - 5 beans defined in application context [org.springframework.context.support.ClassPathXmlApplicationContext;hashCode=31476927]
2009-05-18 20:50:22,142 INFO [org.springframework.aop.framework.DefaultAopProxyFactory] - CGLIB2 not available: proxyTargetClass feature disabled
2009-05-18 20:50:22,202 INFO [org.springframework.context.support.ClassPathXmlApplicationContext] - Bean 'org.springframework.aop.config.internalAutoProxyCreator' is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2009-05-18 20:50:22,203 INFO [org.springframework.context.support.ClassPathXmlApplicationContext] - Unable to locate MessageSource with name 'messageSource': using default [org.springframework.context.support.DelegatingMessageSource@9b42e6]
2009-05-18 20:50:22,206 INFO [org.springframework.context.support.ClassPathXmlApplicationContext] - Unable to locate ApplicationEventMulticaster with name 'applicationEventMulticaster': using default [org.springframework.context.event.SimpleApplicationEventMulticaster@17431b9]
2009-05-18 20:50:22,207 INFO [org.springframework.beans.factory.support.DefaultListableBeanFactory] - Pre-instantiating singletons in factory [org.springframework.beans.factory.support.DefaultListableBeanFactory defining beans [org.springframework.aop.config.internalAutoProxyCreator,securityhandler,usermanager,allAddMethod,org.springframework.aop.aspectj.AspectJPointcutAdvisor]; root of BeanFactory hierarchy]
Jack
123
addUser
=========checkSecurity()==========
-------UserManagerImpl.addUser()----------
-------UserManagerImpl.deleteUser()----------