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();
} }
Spring(十二)使用Spring的xml文件配置方式实现AOP的更多相关文章
- Spring的xml文件配置方式实现AOP
配置文件与注解方式的有很大不同,多了很多配置项. beans2.xml <?xml version="1.0" encoding="UTF-8"?> ...
- spring,springmvc,mybatis基本整合(一)--xml文件配置方式(1)
**这个整合.仅仅是最主要的整合,而且是xml配置文件的方式之中的一个,即当中的mybatis是採用非mapper接口的方式.(第二遍採用mapper接口方式.第三遍採用注解的方式:第四篇採用注解基于 ...
- 三十二、python操作XML文件
'''XML:模块 xml总结 1.解析 str 文件 tree,ElementTree,type root,Element,type2.操作 Element: tag,text,find,iter, ...
- Spring Boot(十二):spring boot如何测试打包部署
Spring Boot(十二):spring boot如何测试打包部署 一.开发阶段 1,单元测试 在开发阶段的时候最重要的是单元测试了,springboot对单元测试的支持已经很完善了. (1)在p ...
- Spring框架入门之基于xml文件配置bean详解
关于Spring中基于xml文件配置bean的详细总结(spring 4.1.0) 一.Spring中的依赖注入方式介绍 依赖注入有三种方式 属性注入 构造方法注入 工厂方法注入(很少使用,不推荐,本 ...
- Spring中加载ApplicationContext.xml文件的方式
Spring中加载ApplicationContext.xml文件的方式 原文:http://blog.csdn.net/snowjlz/article/details/8158560 1.利用Cla ...
- Spring中Bean的配置:基于XML文件的方式
Bean的配置一共有两种方式:一种是基于XML文件的方式,另一种是基于注解的方式.本文主要介绍基于XML文件的方式 <bean id="helloWorld" class=& ...
- 4. Spring 如何通过 XML 文件配置Bean,以及如何获取Bean
在 Spring 容器内拼凑 bean 叫做装配.装配 bean 的时候,你是在告诉容器,需要哪些 bean ,以及容器如何使用依赖注入将它们配合在一起. 理论上,bean 装配的信息可以从任何资源获 ...
- Spring整合Hibernate的XML文件配置,以及web.xml文件配置
利用Spring整合Hibernate时的XML文件配置 applicationContext.xml <?xml version="1.0" encoding=" ...
随机推荐
- .NET:CLR via C# A Brief Look at Metadata
基础知识 A managed PE file has four main parts: the PE32(+) header, the CLR header, the metadata, and th ...
- window与linux文件传输工具
window与linux文件传输工具 [一般用于SecureCRT ssh中使用] 法一:直接用yum安装lrzsz(推荐) yum install lrzsz -y 注意:rhel安装完系统后 直接 ...
- iOS中alloc与init
面向对象的3大特性,封装继承和多态. 我遇到过封装相关的问题,因为初级封装简单,常常暴露出被你封装的接口,进一步进行高级封装隐藏接口的时候才发现,封装是一门学问,而这门学问得从最基础的alloc与in ...
- 利用c#+jquery+echarts生成统计报表(附源代码)
背景: 因为最近项目要生成报表,经过几轮挑选,最终选择了百度的echarts作为报表基础类库.百度echarts简介请参考 http://echarts.baidu.com/ 虽然echarts功能强 ...
- 可操纵网页URL地址的js插件-url.js
url.js是一款能够很有用方便的操纵网页URL地址的js插件.通过url.js你能够设置和获取当前URL的參数,也能够对当前URL的參数进行更新.删除操作.还能够将当前URL的參数显示为json字符 ...
- C语言:返回两个数组中第一个元素的指针,并输出这个值
// // main.c // Pointer_search // // Created by ma c on 15/8/2. // Copyright (c) 2015年. All righ ...
- Bridge 桥接模式 MD
桥接模式 简介 将抽象部分与实现部分分离,使它们都可以独立的变化. 业务抽象角色引用业务实现角色,或者说业务抽象角色的部分实现是由业务实现角色完成的 Bridge模式基于类的最小设计原则,通过使用封装 ...
- Windows服务器PHPstudy配置安装微擎教程
此教程只适于无环境的新服务器,有环境请勿安装用电脑上面的远程桌面连接登陆服务器.1,下载微擎安装包,下载PHPstudy,下载V9运行库.安装PHPstudy.安装V9运行库.准备好3个. <i ...
- vue-router 懒加载优化
一.路由懒加载 1.先安装 babel 动态引入插件 npm install --save-dev babel-plugin-syntax-dynamic-import 2.修改router/inde ...
- python 斐波拉契数列数列
'''斐波拉契数列'''def Fibonacci(n): first, next = 0, 1 i = 0; while i < n: print next first, next = nex ...