首页 > 代码库 > Spring的xml文件配置方式实现AOP
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,..))"
[java] view plain copy- 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()==>>返回结果";
- }
- }
[java] view plain copy- 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;
- }
- }
[java] view plain copy- 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下的所有方法
Spring的xml文件配置方式实现AOP