一、配置文件
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd "> <!-- 需要注入的对象 --> <bean id="goodsProcess" class="com.wl.service.GoodsProcess"></bean> <!-- 额外内容 --> <bean id="afterLogAdvice" class="com.wl.advices.AfterLogAdvice"></bean> <!-- 定义切点 --> <aop:config> <aop:pointcut expression="execution(* com.wl.service..*.*(String,..))" id="pct"/> <aop:advisor advice-ref="afterLogAdvice" pointcut-ref="pct"/> </aop:config> </beans>
二、被扫描的类(注意这个类不能继承接口,不然aop会失效)
package com.wl.service; public class GoodsProcess { public void addUser(String name,String mobile){ System.out.println("add user"); } public void modifyUser(String name){ System.out.println("modify user"); } public void removeUser(Integer id){ System.out.println("remove user"); } }
三、测试
package com.wl.test; import org.junit.Test; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.wl.service.GoodsProcess; public class Demo_test { @Test public void testLog() { @SuppressWarnings("resource") ClassPathXmlApplicationContext cxt = new ClassPathXmlApplicationContext("spring-context.xml"); GoodsProcess goodsDao = (GoodsProcess) cxt.getBean("goodsProcess"); goodsDao.modifyUser("ddd"); } }
四、结果
作者:醉烟
出处:https://www.cnblogs.com/WangLei2018/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。