Spring的AOP配置 小咪咪 2022-08-14 02:54 109阅读 0赞 Spring的AOP配置 1.先写一个普通类: package com.spring.aop; public class Common \{ public void execute(String username,String password)\{ System.out.println("------------------普通类\----------------"); \} \} 2.写一个切面类,用于合法性校验和日志添加: package com.spring.aop; public class Check \{ public void checkValidity()\{ System.out.println("------------------验证合法性\----------------"); \} public void addLog(JoinPoint j)\{ System.out.println("------------------添加日志\----------------"); Object obj\[\] = j.getArgs(); for(Object o :obj)\{ System.out.println(o); \} System.out.println("========checkSecurity=="+j.getSignature().getName());//这个是获得方法名 \} \} 3.配置AOP,使用XML方式:(**注意红色标志的内容**) <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="[http://www.springframework.org/schema/beans][http_www.springframework.org_schema_beans]" xmlns:xsi="[http://www.w3.org/2001/XMLSchema-instance][http_www.w3.org_2001_XMLSchema-instance]" xmlns:aop="[http://www.springframework.org/schema/aop][http_www.springframework.org_schema_aop]" xsi:schemaLocation="[http://www.springframework.org/schema/beans][http_www.springframework.org_schema_beans][http://www.springframework.org/schema/beans/spring-beans-2.5.xsd][http_www.springframework.org_schema_beans_spring-beans-2.5.xsd] [http://www.springframework.org/schema/aop][http_www.springframework.org_schema_aop] [http://www.springframework.org/schema/aop/spring-aop-2.5.xsd][http_www.springframework.org_schema_aop_spring-aop-2.5.xsd]"> <bean id="common" class="com.spring.aop.Common"/> <bean id="check" class="com.spring.aop.Check"/> <aop:config> <aop:aspect id="myAop" ref="check"> <aop:pointcut id="target" **expression="execution(\* com.spring.aop.Common.execute(..))"/>** <aop:before method="checkValidity" pointcut-ref="target"/> <aop:after method="addLog" pointcut-ref="target"/> </aop:aspect> </aop:config> </beans> **注意:** **execution(\* com.spring.aop.\*.\*(..))"/** 这样写应该就可以了 这是com.aptech.jb.epet.dao.hibimpl 包下所有的类的所有方法。。 第一个\*代表所有的返回值类型 第二个\*代表所有的类 第三个\*代表类所有方法 最后一个..代表所有的参数。 4.最后写一个测试: package com.spring.aop; import org.springframework.beans.factory.BeanFactory; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Client \{ public static void main(String\[\] args) \{ BeanFactory factory=new ClassPathXmlApplicationContext("applicationContext-aop.xml"); Common c=(Common) factory.getBean("common"); c.execute("zhengjunhua","zhengjunhua"); \} \} 注意: 需要添加三个包:spring-aop.jar , aspectjrt.jar ,aspectjweaver.jar,否则会报错。 输出结果: \------------------验证合法性\---------------- \------------------普通类\---------------- \------------------添加日志\---------------- zhengjunhua zhengjunhua ========checkSecurity==execute [http_www.springframework.org_schema_beans]: http://www.springframework.org/schema/beans [http_www.w3.org_2001_XMLSchema-instance]: http://www.w3.org/2001/XMLSchema-instance [http_www.springframework.org_schema_aop]: http://www.springframework.org/schema/aop [http_www.springframework.org_schema_beans_spring-beans-2.5.xsd]: http://www.springframework.org/schema/beans/spring-beans-2.5.xsd [http_www.springframework.org_schema_aop_spring-aop-2.5.xsd]: http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
还没有评论,来说两句吧...