日期:2014-05-19 浏览次数:20657 次
package com.spring.aop.service; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.aspectj.lang.annotation.Pointcut; @Aspect public class InterCeptor { @Pointcut("execution (public void com.spring.aop.service.impl.PersonServiceBeanImpl.*(..))") private void anyMethod(){}//声明一个切入点 @Before("anyMethod()")//放入切入点的名称带() public void doBeforCheck(){ System.out.println("我是前置通知!!"); } }
package com.spring.aop.service; public interface PersonServiceBean { public void save(String name); public void update(String name,Integer id); public String getPersonName(Integer id); }
package com.spring.aop.service.impl; import com.spring.aop.service.PersonServiceBean; public class PersonServiceBeanImpl implements PersonServiceBean{ public void save(String name) { System.out.println("我是存入方法:"+name); } public void update(String name, Integer id) { System.out.println("我是更新方法:"+id); } public String getPersonName(Integer id) { return "测试:"+id; } }
<?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/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"> <!-- <bean name="myTest" class="com.service.impl.PersonServiceImpl"> </bean> <bean id="myTest3" name="myTest4" class="com.service.impl.PersonServiceImpl"/> <bean id="myTest2" name="myTest" class="com.service.impl.PersonServiceImpl"/> <bean id="personServiceImpl" class="com.service.impl.PersonServiceImpl" lazy-init="false" scope="prototype"></bean> --> <aop:aspectj-autoproxy></aop:aspectj-autoproxy> <bean id="interCeptor" class="com.spring.aop.service.InterCeptor"></bean> <bean id="personServiceBeanImpl" class="com.spring.aop.service.impl.PersonServiceBeanImpl"></bean> </beans>
package mytest; import java.lang.reflect.Method; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.spring.aop.service.PersonServiceBean; import com.spring.aop.service.impl.PersonServiceBeanImpl; //import com.service.impl.