-
配置文件與注解方式的有很大不同,多了很多配置項。
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();
- }
- }
- 攔截test.spring.service.impl.PersonServiceBean下的所有方法