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();
- }
- }
- 拦截test.spring.service.impl.PersonServiceBean下的所有方法
Spring的xml文件配置方式实现AOP的更多相关文章
- Spring(十二)使用Spring的xml文件配置方式实现AOP
配置文件与注解方式的有非常大不同,多了非常多配置项. beans2.xml <?xml version="1.0" encoding="UTF-8"? & ...
- spring,springmvc,mybatis基本整合(一)--xml文件配置方式(1)
**这个整合.仅仅是最主要的整合,而且是xml配置文件的方式之中的一个,即当中的mybatis是採用非mapper接口的方式.(第二遍採用mapper接口方式.第三遍採用注解的方式:第四篇採用注解基于 ...
- SSM框架中spring的XML文件配置
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w ...
- spring——通过xml文件配置IOC容器
创建相关的类(这里是直接在之前类的基础上进行修改) package com.guan.dao; public interface Fruit { String getFruit(); } packag ...
- Spring中xml文件配置也可以配置容器list、set、map
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...
- Spring中Bean的配置:基于XML文件的方式
Bean的配置一共有两种方式:一种是基于XML文件的方式,另一种是基于注解的方式.本文主要介绍基于XML文件的方式 <bean id="helloWorld" class=& ...
- Spring整合Hibernate的XML文件配置,以及web.xml文件配置
利用Spring整合Hibernate时的XML文件配置 applicationContext.xml <?xml version="1.0" encoding=" ...
- 曹工说Spring Boot源码(16)-- Spring从xml文件里到底得到了什么(aop:config完整解析【上】)
写在前面的话 相关背景及资源: 曹工说Spring Boot源码(1)-- Bean Definition到底是什么,附spring思维导图分享 曹工说Spring Boot源码(2)-- Bean ...
- 曹工说Spring Boot源码(17)-- Spring从xml文件里到底得到了什么(aop:config完整解析【中】)
写在前面的话 相关背景及资源: 曹工说Spring Boot源码(1)-- Bean Definition到底是什么,附spring思维导图分享 曹工说Spring Boot源码(2)-- Bean ...
随机推荐
- WBS功能分解
WBS功能分解表格如下: 功能 子功能 二级子功能 预计花费时间 (小时) 实际花费时间(小时) 地图显示跑道 制作地图 获得开发权限key 1 2 将sdk导入程序 0.5 1 调试程序 1 2 ...
- 使用百度地图api接口获取公交地图路线和车站
需要在页面文件中引用百度的js @*<script type="text/javascript" src="http://api.map.baidu.com/api ...
- 修改项目生成Gemfile的模板
修改项目生成Gemfile的模板 gedit $rvm_path/gems/ruby-2.1.5/gems/railties-4.1.8/lib/rails/generators/rails/app/ ...
- Adressing
- Vim命令合集大全
命令历史 以:和/开头的命令都有历史纪录,可以首先键入:或/然后按上下箭头来选择某个历史命令. 启动vim 在命令行窗口中输入以下命令即可 vim 直接启动vim vim filename 打开vim ...
- XAF 如何将数据库中Byte array图片显示出来
问题比较简单,直接上代码. private Image _Cover; [Size(SizeAttribute.Unlimited), ValueConverter(typeof(ImageValue ...
- maven nexus 私服的搭建学习
之前对maven有过初步的了解与认识,自己也创建过项目使用其来管理,但都是非常粗浅的操作,今天在高人的指点下,也学着在自己的电脑上搭建一个maven私服,虽然技术难度也不高,但为了更深层次的提高,这些 ...
- QT QML目录导航列表视图
[功能] /目录.文件 /文件过滤 /递归 /事件 /高亮当前行 /当前选项 /目录切换动画 /限制根目录 [下载]:http://download.csdn.net/detail/surfsky/8 ...
- mybatis研究:select性能对比
package sss.mybatis_1; import java.io.InputStream; import java.security.Principal; import java.util. ...
- java socket 多线程通讯 使用mina作为服务端
客户端代码不变,参照 http://www.cnblogs.com/Westfalen/p/6251473.html 服务端代码如下: import java.io.IOException; impo ...