spring aop实例
package com.mangocity.spring.test; /** * 切面类 * @author test * */ public class Spring3_1Test { public void beforePrintContent(String name) { System.out.println("beforePrintContent..."+name); } public void afterPrintContent() { System.out.println("afterPrintContent..."); } } package com.mangocity.spring.test; /** * 目标类 * @author test * */ public class Spring { public void argsTest(String name) { System.out.println(name); } } spring配置 <?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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"> <!-- 实例化 --> <bean id="salService" class="com.mangocity.spring.test.Spring"/> <bean id="implement" class="com.mangocity.spring.test.Spring3_1Test"/> <aop:config> <!-- 切入点 有参数 --> <aop:pointcut id="pointcut" expression="execution(* com.mangocity..*.*.*(..)) and args(arg1)"/> <aop:aspect ref="implement"> <!-- 有参数传值 arg-names的名字需与args中的参数名一致 --> <aop:before pointcut-ref="pointcut" method="beforePrintContent" arg-names="arg1"/> <!-- 无参数 --> <aop:after pointcut="execution(* com.mangocity..*.*.*(..))" method="afterPrintContent"/> </aop:aspect> </aop:config> </beans> 测试类: package com.mangocity.spring.test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class SpringTest { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); Spring s = (Spring) context.getBean("salService"); s.argsTest("测试"); } } 输出结果: beforePrintContent...测试 测试 afterPrintContent...
相关推荐
yupi0 2020-10-10
spring 2020-08-18
编程点滴 2020-07-29
幸运小侯子 2020-07-05
itjavashuai 2020-07-04
qingjiuquan 2020-06-29
shushan 2020-06-25
小鱿鱼 2020-06-22
咻pur慢 2020-06-18
melonjj 2020-06-17
qingjiuquan 2020-06-13
neweastsun 2020-06-05
小鱿鱼 2020-06-05
mxcsdn 2020-05-31
吾日五省我身 2020-05-27
牧场SZShepherd 2020-05-27
sweetgirl0 2020-05-14