spring中aop以xml配置方式
1 引jar包
springAOP\aopalliance.jar springAOP\aspectjrt.jar springAOP\aspectjweaver.jar springAOP\spring-aop-3.2.9.RELEASE.jar
2 aop配置
<?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-4.3.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <bean id="userDao" class="com.huitong.Dao3.UserDao"></bean> <bean id="aop" class="com.huitong.Dao3.Aop"></bean> <aop:config> <aop:pointcut expression="execution(* com.huitong.Dao3.UserDao.*(..))" id="pt"/> <aop:aspect id="asp" ref="aop"> <aop:around method="around" pointcut-ref="pt"/> </aop:aspect> </aop:config> </beans>
3 测试
public void fun2(){ ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("bean-dao.xml"); UserDao userDao = (UserDao) ac.getBean("userDao"); userDao.saveUser(); }
注意:sop拦截点定义到方法,所以在写拦截点时要写全。
学习过程中,难免出错。如果您在阅读过程中遇到不太明白,或者有疑问。欢迎指正...联系邮箱crazyCodeLove@163.com
如果觉得有用,想赞助一下请移步赞助页面:赞助一下