ApplicationContext.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" 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 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"> <context:component-scan base-package="com.spring.aop.xml.transaction"> </context:component-scan> <aop:aspectj-autoproxy></aop:aspectj-autoproxy> </beans>
package com.spring.aop.xml.transaction; public interface PersonDao { public void savePerson(); }
package com.spring.aop.xml.transaction; import org.springframework.stereotype.Repository; @Repository("personDao") public class PersonDaoImpl implements PersonDao{ public void savePerson() { System.out.println("save person"); } }
package com.spring.aop.xml.transaction; 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; /** * @Aspect * @Pointcut("execution(* com.spring.aop.xml.transaction.PersonDaoImpl.*(..))") * private void aa(){}== * <aop:config> <aop:pointcut expression="execution(* com.spring.aop.xml.transaction.PersonDaoImpl.*(..))" id="aa()"/> </aop:config> * @author zd * */ @Component("transaction") @Aspect public class Transaction { @Pointcut("execution(* com.spring.aop.xml.transaction.PersonDaoImpl.*(..))") private void aa(){} //方法签名 @Before("aa()") public void beginTransaction(){ System.out.println("begin transaction"); } @AfterReturning("aa()") public void commit(){ System.out.println("commit"); } }
时间: 2024-10-05 01:30:47