struts2中的通知就是环绕通知;
直接上代码:
beans.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"
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"><aop:aspectj-autoproxy/>
<bean id="myInterceptor"/>
<bean id="personService"/>
</beans>
PersonService.java
package cn.itcast.service;
/**
*
* @author kang.cunhua
*/
public interface PersonService {void save(String name);
void update(String name, Integer personid);
public String getPersonName(Integer personid);
}
PersonServiceBean.java
package cn.itcast.service.impl;
import cn.itcast.service.PersonService;
/**
*
* @author kang.cunhua
*/
public class PersonServiceBean implements PersonService {
@Override
public void save(String name) {
//throw new RuntimeException("我爱例外!");
System.out.println("我是save()方法!");
}@Override
public void update(String name, Integer personid) {
System.out.println("我是update方法()方法!");
}public String getPersonName(Integer personid) {
System.out.println("我是getPersonName()方法!");
return "xxx";
}
}
MyInterceptor.java
package cn.itcast.aop;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;/**
*
* @author kang.cunhua
*/
@Aspect
public class MyInterceptor {@Pointcut("execution (* cn.itcast.service.impl.PersonServiceBean.*(..))")
private void anyMethod() { // 声明一个切入点
}@Before("anyMethod() && args(userName)")
public void doAccessCheck(String userName) {
System.out.println("前置通知触发:" + userName);
}
@AfterReturning(pointcut="anyMethod()",returning="result")
public void doAfterReturning(String result) {
System.out.println("后置通知触发:" + result);
}
@After("anyMethod()")
public void doAfter() {
System.out.println("最终通知触发!!");
}
@AfterThrowing(pointcut="anyMethod()",throwing="e")
public void doAfterThrowing(Exception e) {
System.out.println("异常通知触发:"+ e);
}
// 环绕通知在我们做权限时,可以大量使用到
@Around("anyMethod()")
public Object doBasicProfiling(ProceedingJoinPoint pjp) throws Throwable{
//if () { // 判断用户是否有权限
System.out.println("进入方法");
Object result = pjp.proceed();// 如果不调用此方法,业务bean方法不会最终被调用;如果后续有其他切面,先执行其他切面,最后执行业务bean方法;
System.out.println("退出方法");
//}
return result;
}}
MyInterceptorTest.java
package cn.itcast.aop;
import cn.itcast.service.PersonService;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;/**
*
* @author kang.cunhua
*/
public class MyInterceptorTest {public MyInterceptorTest() {
}@BeforeClass
public static void setUpClass() throws Exception {
}/**
* Test of doAccessCheck method, of class MyInterceptor.
*/
@Test
public void testDoAccessCheck() {
ApplicationContext cxt = new ClassPathXmlApplicationContext("beans.xml");
PersonService personService = (PersonService) cxt.getBean("personService");
personService.save("xxx");
System.out.println("-----------------");
personService.getPersonName(2);
}
}
测试结果:
2010-7-20 15:00:20 org.springframework.context.support.AbstractApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@1837697: display name [org.springframework.context.support.ClassPathXmlApplicationContext@1837697]; startup date [Tue Jul 20 15:00:20 CST 2010]; root of context hierarchy
2010-7-20 15:00:20 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [beans.xml]
2010-7-20 15:00:20 org.springframework.context.support.AbstractApplicationContext obtainFreshBeanFactory
信息: Bean factory for application context [org.springframework.context.support.ClassPathXmlApplicationContext@1837697]: org.springframework.beans.factory.support.DefaultListableBeanFactory@698403
2010-7-20 15:00:20 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
信息: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@698403: defining beans [org.springframework.aop.config.internalAutoProxyCreator,myInterceptor,personService]; root of factory hierarchy
前置通知触发:xxx
进入方法
我是save()方法!
后置通知触发:null
最终通知触发!!
退出方法
-----------------
进入方法
我是getPersonName()方法!
后置通知触发:xxx
最终通知触发!!
退出方法