Spring(十二)使用Spring的xml文件配置方式實現AOP
阿新 • • 發佈:2017-08-13
註解 asp odi ns2 package hit 實現 object space
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>
配置文件與註解方式的有非常大不同,多了非常多配置項。
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"
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: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(); } }
Spring(十二)使用Spring的xml文件配置方式實現AOP