使用Spring配置文件实现AOP
来源:互联网 发布:seo工程师薪资待遇 编辑:程序博客网 时间:2024/04/28 01:38
前面给大家介绍了怎样使用注解的方式来开发AOP应用,现在学习一下使用XML方式怎样来开发AOP应用。
如果使用XML方式开发AOP应用的话,我们就不再需要提供注解的配置,我们只需要一个普通的Java类对象
MyInterceptor.java
- package cn.itcast.service;
- import org.aspectj.lang.ProceedingJoinPoint;
- /**
- * 切面
- */
- public class MyInterceptor {
- 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 doBasicProfiling(ProceedingJoinPoint pjp) throws Throwable {
- System.out.println("进入方法");
- Object result = pjp.proceed();
- System.out.println("退出方法");
- return result;
- }
- }
可以看到,在这个切面里面,这只是一个普普通通的Java类,里面没有任何的注解。如果我们采用基于XML方式来开发AOP应用的话,我们是要在配置文件中对切面进行配置的。现在看一下切面该如何配置
基于基于XML配置方式声明切面
--------------------------------------------------------------------
- <bean id="orderservice" class="cn.itcast.service.OrderServiceBean"/>
- <bean id="log" class="cn.itcast.service.LogPrint"/>
- <aop:config>
- <aop:aspect id="myaop" ref="log">
- <aop:pointcut id="mycut" expression="execution(* cn.itcast.service..*.*(..))"/>
- <aop:before pointcut-ref="mycut" method="doAccessCheck"/>
- <aop:after-returning pointcut-ref="mycut" method="doReturnCheck "/>
- <aop:after-throwing pointcut-ref="mycut" method="doExceptionAction"/>
- <aop:after pointcut-ref="mycut" method=“doReleaseAction"/>
- <aop:around pointcut-ref="mycut" method="doBasicProfiling"/>
- </aop:aspect>
- </aop:config>
首先要把用做切面的类交给Spring管理,然后通过<aop:config>配置元素,首先配置引用这个切面<aop:aspect id="myaop" ref="log">。然后在切面底下,我们可以定义一些像切入点,前置通知,后置通知,例外通知,最终通知和环绕通知。这里有个参数pointcut-ref(切入点引用),那么引入的切入点就是前面定义的切入点<aop:pointcut id="mycut" expression="execution(* cn.itcast.service..*.*(..))"/>。
当方法被拦截到后呢,它就会应用这个method所指定的方法。
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: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="cn.itcast.service.impl.PersonServiceBean"></bean>
- <bean id="aspetbean" class="cn.itcast.service.MyInterceptor"/>
- <aop:config>
- <aop:aspect id="asp" ref="aspetbean">
- <aop:pointcut id="mycut" expression="execution(* cn.itcast.service.impl.PersonServiceBean.*(..))"/>
- <aop:before pointcut-ref="mycut" method="doAccessCheck"/>
- <aop:after-returning pointcut-ref="mycut" method="doAfterReturning"/>
- <aop:after-throwing pointcut-ref="mycut" method="doAfterThrowing"/>
- <aop:after pointcut-ref="mycut" method="doAfter"/>
- <aop:around pointcut-ref="mycut" method="doBasicProfiling"/>
- </aop:aspect>
- </aop:config>
- </beans>
PersonServiceBean.java
- package cn.itcast.service.impl;
- import cn.itcast.service.PersonService;
- public class PersonServiceBean implements PersonService {
- public String getPersonName(Integer id) {
- System.out.println("我是getPersonName()方法");
- return "xxx";
- }
- public void save(String name) {
- //throw new RuntimeException("我爱例外");
- System.out.println("我是save()方法");
- }
- public void update(String name, Integer id) {
- System.out.println("我是update()方法");
- }
- }
SpringAOPTest.java
- package junit.test;
- import org.junit.BeforeClass;
- import org.junit.Test;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- import cn.itcast.service.PersonService;
- public class SpringAOPTest {
- @BeforeClass
- public static void setUpBeforeClass() throws Exception {
- }
- @Test public void interceptorTest(){
- ApplicationContext cxt = new ClassPathXmlApplicationContext("beans.xml");
- PersonService personService = (PersonService)cxt.getBean("personService");
- personService.save("yyy");
- }
- }
现在看一下,我们采用基于XML方式开发AOP应用是否能够成功,在这里,目前除了例外通知不会执行外,其它通知都会执行,
运行单元测试代码,控制台输出
前置通知
进入方法
我是save()方法
后置通知
最终通知
退出方法
改动下代码,看下例外通知是否被执行
PersonServiceBean.java
- package cn.itcast.service.impl;
- import cn.itcast.service.PersonService;
- public class PersonServiceBean implements PersonService {
- public String getPersonName(Integer id) {
- System.out.println("我是getPersonName()方法");
- return "xxx";
- }
- public void save(String name) {
- throw new RuntimeException("我爱例外");
- //System.out.println("我是save()方法");
- }
- public void update(String name, Integer id) {
- System.out.println("我是update()方法");
- }
- }
运行单元测试代码:控制台输出
前置通知
进入方法
例外通知
最终通知
在JUnit里,有例外抛出
java.lang.RuntimeException: 我爱例外
at cn.itcast.service.impl.PersonServiceBean.save(PersonServiceBean.java:13)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
...................................................
例外通知也被执行了。
大家看到,基于XML方式开发AOP应用也是挺简单的,和前面基于注解方式开发AOP应用不同之处是MyInterceptor.java这个类已经没有了注解的定义,它是一个纯粹的,普通的java bean对象,没有任何注解。 配置信息都是在XML配置文件里定义的。
我们采用AOP编程,一般都是用来做权限系统,运行期监控。。。这些系统
- 使用Spring配置文件实现AOP
- 使用Spring配置文件实现AOP
- 使用spring配置文件实现AOP
- 使用Spring配置文件实现AOP
- 使用Spring配置文件实现AOP
- spring中使用配置文件实现AOP
- Spring学习笔记(16)----使用Spring配置文件实现AOP
- spring-aop 配置文件方式实现
- Spring AOP之配置文件实现方式
- Spring---->采用静态配置文件方式实现AOP
- Spring-通过xml配置文件实现切面(AOP)
- Spring-AOP 通过配置文件实现 前置增强
- Spring-AOP 通过配置文件实现 后置增强
- Spring-AOP 通过配置文件实现 环绕增强
- 使用注解实现Spring aop
- spring使用注解实现AOP
- Spring学习笔记(四)Spring配置文件方式实现AOP
- spring aop配置文件
- Web 地理定位(Geo-Location)知识大全
- BulkLoader多个文件一起加载
- JavaScript中实现拖放功能的演示代码
- 使用Spring的注解方式实现AOP的细节
- maven-tomcat-plugin的用法
- 使用Spring配置文件实现AOP
- aspectj的切入点语法定义细节
- 搭建和配置Spring与jdbc整合的环境
- 台湾一校长的演讲:震动所有中国人!
- Spring集成的jdbc编码和事务管理
- ubuntu下卸载软件(两种方法)
- c# 实体符号转换
- 使用Spring注解方式管理事务与传播行为详解
- 使用Spring配置文件实现事务管理