67,549
社区成员




/**
* //:MyInteceptor.java 2009-9-1 Benjamin Wu.
*/
package com.yakoo5.service;
import org.aspectj.lang.ProceedingJoinPoint;
public class MyInteceptor {
public void doBefore(){
System.out.println("前置通知");
}
public void doAfterReturning(String name){
System.out.println("后置通知:"+name);
}
public void doAfter(){
System.out.println("最终通知:");
}
public void doAfterThrowing(){
System.out.println("异常通知");
}
public Object doBasicProfiling(ProceedingJoinPoint pjp) throws Throwable{
System.out.println("进入方法");
Object result = pjp.proceed();
System.out.println("退出方法");
return result;
}
}
/**
* //:PersonService.java 2009-9-1 Benjamin Wu.
*/
package com.yakoo5.service;
public interface PersonService {
public String getPersonName(Integer id);
public void save(String name);
public void update(String name, Integer id);
}
/**
* //:PersonServiceBean.java 2009-9-1 Benjamin Wu.
*/
package com.yakoo5.service.impl;
import com.yakoo5.service.PersonService;
public class PersonServiceBean implements PersonService {
public String getPersonName(Integer id){
return "xxx";
}
public void save(String name){
//throw new RuntimeException("我爱例外");
System.out.println("我是save()方法");
}
public void update(String name, Integer id){
System.out.println("我是update()方法");
}
}
package junit.test;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.context.support.AbstractXmlApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.yakoo5.service.PersonService;
import com.yakoo5.service.impl.PersonServiceBean;
public class SpringTest {
@BeforeClass
public static void setUpBeforeClass() throws Exception {
}
@Test
public void aopTest() {
AbstractXmlApplicationContext ctx = new ClassPathXmlApplicationContext(
"beans.xml");
PersonService ps = (PersonServiceBean) ctx.getBean("personService");
ps.save("hello");
}
}
<?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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
<aop:aspectj-autoproxy/>
<bean id="personService" class="com.yakoo5.service.impl.PersonServiceBean" />
<bean id="aspetBean" class="com.yakoo5.service.MyInteceptor" />
<aop:config>
<aop:aspect id="asp" ref="aspetBean">
<aop:pointcut expression="execution(* com.yakoo5.service.impl.PersonServiceBean.*(..))" id="mycut"/>
<aop:before pointcut-ref="mycut" method="doBefore"/>
</aop:aspect>
</aop:config>
</beans>