本质上就是代理,只是做成了配置文件形式。
基类
package _10SpringAOP; public class Person { public int id; public String name; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
dao接口
package _10SpringAOP; public interface personDao { public String savePerson(Person person); }
dao接口实现
package _10SpringAOP; public class PersonDaoImpl implements personDao { @Override public String savePerson(Person person) { // TODO Auto-generated method stub //测试springAop的错误处理 // int a = 1/0; System.out.println("save person"); return "savePerson"; } }
切面类
package _10SpringAOP; import org.aspectj.lang.ProceedingJoinPoint; public class MyTransaction { public void beginTransaction(){ System.out.println("beginTransaction"); } public void commit(Object var){ System.out.println("commit"); System.out.println("var is " + var); } public void finalMethod(){ System.out.println("the finally method"); } public void throwMethod(Throwable ex){ System.out.println("ex is " + ex.toString()); } public void aroundMethod(ProceedingJoinPoint point) throws Throwable{ //获取目标方法的名称 System.out.println("method is " + point.getSignature().getName()); //调用目标方法 // point.proceed(); System.out.println("end"); } }
测试类
package _10SpringAOP; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class test { // beginTransaction // method is savePerson // save person // commit // var is savePerson // the finally method // end //aroundMethod方法里面不调用的时候 // beginTransaction // method is savePerson // end //没有调用的话,那么目标方法和后面的后置方法等全部不调用 public static void main(String[] args) { // TODO Auto-generated method stub ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); //如果是PersonDao,有实现接口 产生的是代理对象 //如果是PersonDaoImpl,无实现接口 产生的是目标对象 personDao persondao = (_10SpringAOP.personDao) context.getBean("personDao"); persondao.savePerson(new Person()); } }
配置文件
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" 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-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"> <!-- spring aop的命名空间需要自己加进来 http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd spring aop还需要两个jar包 aspectjrt.jar aspectjweaver.jar 导入目标类,导入切面 --> <bean id="personDao" class="_10SpringAOP.PersonDaoImpl"></bean> <bean id="myTransaction" class="_10SpringAOP.MyTransaction"></bean> <aop:config> <aop:pointcut expression="execution(* _10SpringAOP.PersonDaoImpl.*(..))" id="perform"/> <aop:aspect ref="myTransaction"> <aop:before method="beginTransaction" pointcut-ref="perform"/> <!-- 记得加上returning --> <aop:after-returning method="commit" pointcut-ref="perform" returning="var"/> <aop:after method="finalMethod" pointcut-ref="perform"/> <!-- 记得加上throwsing --> <aop:after-throwing method="throwMethod" pointcut-ref="perform" throwing="ex"/> <aop:around method="aroundMethod" pointcut-ref="perform"/> </aop:aspect> </aop:config> </beans>
时间: 2024-10-10 13:15:53