PersonDaoImpl.java
package aop.annotation; import org.springframework.stereotype.Repository; @Repository("personDao") public class PersonDaoImpl implements PersonDao { @Override public void savePerson() { System.out.println("savePerson"); } }
Transaction.java
package aop.annotation; import org.aspectj.lang.annotation.AfterReturning; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.aspectj.lang.annotation.Pointcut; import org.springframework.stereotype.Component; /** * * @author Hqy * @Aspect 标识切面类<aop:config></aop:config> * @Pointcut 标注切入点 */ @Aspect @Component(value = "transaction") public class Transaction { @Pointcut("execution(* aop.annotation.PersonDaoImpl.*(..))") private void trans() {} //切入点签名 @Before("trans()") public void beforeMethod() { System.out.println("before Method"); } @AfterReturning(value="trans()", returning="val") public void afterMethod(Object val) { System.out.println("return method."); } }
applicationContext.xml
<?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" xmlns:context="http://www.springframework.org/schema/context" 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 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"> <!-- 导入spring AOP注解解析器 --> <aop:aspectj-autoproxy></aop:aspectj-autoproxy> <!-- 导入类扫描注解解析器 --> <context:component-scan base-package="aop.annotation"></context:component-scan> </beans>
浙公网安备 33010602011771号