Spring(十二)使用Spring的xml文件配置方式实现AOP
配置文件与注解方式的有非常大不同,多了非常多配置项。
beans2.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: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
http://www.springframework.org/schema/context/spring-context-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="personService" class="test.spring.service.impl.PersonServiceBean"></bean>
<bean id="myInterceptor" class="test.spring.aop.MyInterceptor2"></bean>
<aop:config>
<aop:aspect id="myAspect" ref="myInterceptor">
<aop:pointcut id="myPointCut" expression="execution(* test.spring.service.impl.PersonServiceBean.*(..))"
/>
<aop:before pointcut-ref="myPointCut" method="doAccessCheck" />
<aop:after-returning pointcut-ref="myPointCut" method="doAfterReturning" />
<aop:after-throwing pointcut-ref="myPointCut" method="doAfterThrowing" />
<aop:around pointcut-ref="myPointCut" method="doAround" />
<aop:after pointcut-ref="myPointCut" method="doAfter" />
</aop:aspect>
</aop:config>
</beans>
切面的切入点语法定义
- 拦截test.spring.service.impl.PersonServiceBean下的全部方法
expression="execution(* test.spring.service.impl.PersonServiceBean.*(..))"
- 拦截test.spring.service.impl子包下的全部类的全部方法
expression="execution(* test.spring.service.impl..*.*(..))"
- 拦截test.spring.service.impl.PersonServiceBean下的全部返回值为String类型的方法
expression="execution(java.lang.String test.spring.service.impl.PersonServiceBean.*(..))"
- 拦截test.spring.service.impl.PersonServiceBean下的全部方法中第一个參数类型为String的方法
expression="execution(* test.spring.service.impl.PersonServiceBean.*(java.lang.String,..))"
package test.spring.service.impl; import test.spring.service.PersonService; //代理对象实现目标对象全部接口 public class PersonServiceBean implements PersonService { public PersonServiceBean() { } @Override public void save(String name) { System.out.println("save()->>" + name); throw new RuntimeException(">>----自己定义异常----<<"); } @Override public String getResult() { return "getResult()==>>返回结果"; } }
package test.spring.aop; import org.aspectj.lang.ProceedingJoinPoint; public class MyInterceptor2 { public void doAccessCheck() { System.out.println("前置通知-->>"); } public void doAfterReturning() { System.out.println("后置通知-->>"); } public void doAfter() { System.out.println("终于通知"); } public void doAfterThrowing() { System.out.println("异常通知-->"); } public Object doAround(ProceedingJoinPoint pJoinPoint) throws Throwable { System.out.println("围绕通知"); // 这里假设pJoinPoint.proceed()不运行。后面拦截到的方法都不会运行,很适用于权限管理 Object result = pJoinPoint.proceed(); System.out.println("退出"); return result; } }
package test.spring.junit; import org.junit.Test; import org.springframework.context.support.AbstractApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import test.spring.service.PersonService; public class AOPTest3 { @Test public void test() { AbstractApplicationContext aContext = // new ClassPathXmlApplicationContext("beans2.xml"); PersonService pService = (PersonService) aContext .getBean("personService"); pService.save("LinDL"); pService.getResult(); aContext.close(); } }
posted on 2017-08-13 16:42 gavanwanggw 阅读(252) 评论(0) 编辑 收藏 举报