动态代理AOP的配置

来源:互联网 发布:php正则$1 编辑:程序博客网 时间:2024/06/09 23:33

Common.Java


  1. package com.aspect;  
  2.   
  3. public class Common {  
  4.     public void fn()  
  5.     {  
  6.         System.out.println("****************");  
  7.     }  
  8.     public void tc()  
  9.     {  
  10.         System.out.println("|||||||||||||||||");  
  11.     }  
  12. }

check.java


  1. package com.aspect;  
  2.   
  3. public class Check {  
  4.     public void check()  
  5.     {  
  6.         System.out.println("验证!!!!!!!!!!!");  
  7.     }  
  8. }


web.xml配置文件如下


  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app version="2.5"   
  3.     xmlns="http://java.sun.com/xml/ns/javaee"   
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
  5.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   
  6.     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">  
  7.     <context-param>  
  8.         <param-name>contextConfigLocation</param-name>  
  9.         <param-value>classpath:applicationContext.xml</param-value>  
  10.     </context-param>  
  11.       
  12.     <listener>   
  13.         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>   
  14.     </listener>  
  15.       
  16.     <welcome-file-list>  
  17.         <welcome-file>index.jsp</welcome-file>  
  18.     </welcome-file-list>  
  19. </web-app>


applicationContext.xml配置文件如下


  1. <?xml version="1.0" encoding="UTF-8"?>   
  2.   
  3. <beans xmlns="http://www.springframework.org/schema/beans"   
  4.   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
  5.   xmlns:aop="http://www.springframework.org/schema/aop"   
  6.   xmlns:tx="http://www.springframework.org/schema/tx"   
  7.   xsi:schemaLocation="   
  8.       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd   
  9.       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd   
  10.       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">      
  11.    
  12.   <bean id="common" class="com.aspect.Common"/>   
  13.   <bean id="check" class="com.aspect.Check"/>   
  14.       
  15.   <aop:config>   
  16.     <aop:aspect id="deal" ref="check">   
  17.       <aop:pointcut id="target" expression="execution(* com.aspect.Common.*(..))"/>   
  18.       <aop:before method="check" pointcut-ref="target"/>   
  19.     </aop:aspect>   
  20.   </aop:config>   
  21.       
  22. </beans>


测试类Client.java


  1. package com.aspect;  
  2.   
  3. import org.springframework.beans.factory.BeanFactory;  
  4. import org.springframework.context.ApplicationContext;  
  5. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  6. import org.springframework.context.support.FileSystemXmlApplicationContext;  
  7.   
  8. public class Client {  
  9.     public static void main(String[] args) {  
  10.         ApplicationContext factory=new ClassPathXmlApplicationContext("applicationContext.xml");
  11.     }
  12. }

  1. //下面这句也可以   
  2.     //  BeanFactory factory=new ClassPathXmlApplicationContext("applicationContext.xml");   
  3.         Common c=(Common) factory.getBean("common");   
  4.         c.fn();   
  5.         c.tc();  
  6.     }  
  7. }


结果如下:

[html] view plain copy
  1. 验证!!!!!!!!!!!  
  2. ****************  
  3. 验证!!!!!!!!!!!  
  4. |||||||||||||||||

导入的5个jar包如下:

aopalliance.jar

aspectjweaver.jar

cglib-nodep-2.2.3.jar

commons-logging-1.0.4.jar

org.springframework.aop-3.1.1.RELEASE.jar


org.springframework.asm-3.1.1.RELEASE.jar

org.springframework.beans-3.1.1.RELEASE.jar

org.springframework.context-3.1.1.RELEASE.jar

org.springframework.core-3.1.1.RELEASE.jar

org.springframework.expression-3.1.1.RELEASE.jar


0 0
原创粉丝点击