注:本例中Usermanager.java、UsermanagerImpl.java和Clien.java三个文件的源代码和上一篇日志中相同。
spring对AOP的支持(采用配置文件的方式)
1、spring依赖库
* SPRING_HOME/dist/spring.jar
* SPRING_HOME/lib/jakarta-commons/commons-logging.jar
* SPRING_HOME/lib/log4j/log4j-1.2.14.jar
* SPRING_HOME/lib/aspectj/*.jar
2、配置如下
<aop:config>
<aop:aspect id="security" ref="securityhandler">
<aop:pointcut id="allAddMethod" expression="execution(* add*(..))"/>
<aop:before method="checkSecurity" pointcut-ref="allAddMethod"/>
</aop:aspect>
</aop:config>
SecurityHandler.java代码如下:
package com.wdh.spring;
public class SecurityHandler {
public void checkSecurity(){
System.out.println("=========checkSecurity()==========");
}
}
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"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
<aop:aspectj-autoproxy />
<bean id="securityhandler" class="com.wdh.spring.SecurityHandler"/>
<bean id="usermanager" class="com.wdh.spring.UserManagerImpl"/>
<aop:config>
<aop:aspect id="security" ref="securityhandler">
<aop:pointcut id="allAddMethod" expression="execution(* add*(..))"/>
<aop:before method="checkSecurity" pointcut-ref="allAddMethod"/>
</aop:aspect>
</aop:config>
</beans>
测试运行结果如下:
2009-05-18 20:26:44,070 INFO [org.springframework.core.CollectionFactory] - JDK 1.4+ collections available
2009-05-18 20:26:44,133 INFO [org.springframework.beans.factory.xml.XmlBeanDefinitionReader] - Loading XML bean definitions from class path resource [applicationContext.xml]
2009-05-18 20:26:44,595 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:26:44,607 INFO [org.springframework.context.support.ClassPathXmlApplicationContext] - 5 beans defined in application context [org.springframework.context.support.ClassPathXmlApplicationContext;hashCode=31476927]
2009-05-18 20:26:44,620 INFO [org.springframework.aop.framework.DefaultAopProxyFactory] - CGLIB2 not available: proxyTargetClass feature disabled
2009-05-18 20:26:44,741 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:26:44,743 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:26:44,747 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:26:44,749 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]
=========checkSecurity()==========
-------UserManagerImpl.addUser()----------
-------UserManagerImpl.deleteUser()----------
spring对AOP的支持(采用配置文件的方式)
1、spring依赖库
* SPRING_HOME/dist/spring.jar
* SPRING_HOME/lib/jakarta-commons/commons-logging.jar
* SPRING_HOME/lib/log4j/log4j-1.2.14.jar
* SPRING_HOME/lib/aspectj/*.jar
2、配置如下
<aop:config>
<aop:aspect id="security" ref="securityhandler">
<aop:pointcut id="allAddMethod" expression="execution(* add*(..))"/>
<aop:before method="checkSecurity" pointcut-ref="allAddMethod"/>
</aop:aspect>
</aop:config>
SecurityHandler.java代码如下:
package com.wdh.spring;
public class SecurityHandler {
public void checkSecurity(){
System.out.println("=========checkSecurity()==========");
}
}
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"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
<aop:aspectj-autoproxy />
<bean id="securityhandler" class="com.wdh.spring.SecurityHandler"/>
<bean id="usermanager" class="com.wdh.spring.UserManagerImpl"/>
<aop:config>
<aop:aspect id="security" ref="securityhandler">
<aop:pointcut id="allAddMethod" expression="execution(* add*(..))"/>
<aop:before method="checkSecurity" pointcut-ref="allAddMethod"/>
</aop:aspect>
</aop:config>
</beans>
测试运行结果如下:
2009-05-18 20:26:44,070 INFO [org.springframework.core.CollectionFactory] - JDK 1.4+ collections available
2009-05-18 20:26:44,133 INFO [org.springframework.beans.factory.xml.XmlBeanDefinitionReader] - Loading XML bean definitions from class path resource [applicationContext.xml]
2009-05-18 20:26:44,595 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:26:44,607 INFO [org.springframework.context.support.ClassPathXmlApplicationContext] - 5 beans defined in application context [org.springframework.context.support.ClassPathXmlApplicationContext;hashCode=31476927]
2009-05-18 20:26:44,620 INFO [org.springframework.aop.framework.DefaultAopProxyFactory] - CGLIB2 not available: proxyTargetClass feature disabled
2009-05-18 20:26:44,741 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:26:44,743 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:26:44,747 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:26:44,749 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]
=========checkSecurity()==========
-------UserManagerImpl.addUser()----------
-------UserManagerImpl.deleteUser()----------