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();
	}

}

时间: 2024-07-30 22:32:52

Spring(十二)使用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="

spring,springmvc,mybatis基本整合(一)--xml文件配置方式(2)

spring,springmvc,mybatis基本整合(一)–xml文件配置方式(2)之mapper接口 一,整合结构 二,所需jar包 如上图. 三,整合配置 1,web.xml文件 <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://j

spring,springmvc,mybatis基本整合(一)--xml文件配置方式(1)

**这个整合,只是最基本的整合,并且是xml配置文件的方式之一,即其中的mybatis是采用非mapper接口的方式.(第二遍采用mapper接口方式:第三遍采用注解的方式:第四篇采用注解基于maven的方式),记录在这里,以免下次忘记时留作备用. ===================================================================================================** 一,整体结构 二,所需jar包: 实质上并不需

Spring Batch 简单应用 (三)(XML文件操作)

前篇关于Spring Batch的文章,讲述了Spring Batch 对CSV文件的读写操作. 本文将通过一个完整的实例,与大家一起讨论运用Spring Batch对XML文件的读写操作.实例流程是从一个XML文件中读取商品信息,经过简单的处理,写入另外一个XML文件中. 工程结构如下图: log4j.xml是log处理的配置文件,与本文没有必然联系,再此不做论述. application.xml文件内容如下: 按 Ctrl+C 复制代码 <?xml version="1.0"

idea的spring整合基于xml文件配置的mybatis报Invalid bound statement (not found): com.music.dao.MusicDao.findAll的问题

一. 题主当时就是自己尝试整合spring和mybatis的时候遇到了这个问题,当时题主只看到了用注解的方式配置的dao层,题主用的是xml文件配置的形式, 而且坑爹的是题主的两个文件的路径写的也不一致,就导致直接用<property name="basePackage" value="com.music.dao"></property> 导致绑定异常 后来在网上查到了解决的办法 ,就是如果路径一致,(如果一致你也就不会来看到本文了), 两个

源码跟读,Spring是如何解析和加载xml中配置的beans

Spring版本基于: 跟踪代码源码基于: https://github.com/deng-cc/KeepLearning commit id:c009ce47bd19e1faf9e07f12086cd440b7799a63 1.配置启动Spring所需的监听器 web.xml中配置监听器 <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-cla

攻城狮在路上(贰) Spring(二)--- Spring IoC概念介绍

一.IoC的概念: IoC(控制反转)是Spring容器的核心.另一种解释是DI(依赖注入),即让调用类对某一个接口的依赖关系由第三方注入,以移除调用类对某一个接口实现类的一览. 定义如此,由此可见,在面向接口编程的情况下,IoC可以很好的实现解耦,可以以配置的方式为程序提供所需要的接口实现类. 在实际程序开发中,我们只需要提供对应的接口及实现类,然后通过Spring的配置文件或者注解完成对依赖类的装配.二.IoC的类型: 1.通过构造函数: 此种方式的缺点是,在构造函数中注入之后一般会作为一个

QT开发(六十二)———QT5解析Json文件

QT开发(六十二)---QT5解析Json文件 一.QT5 Json简介 QT4中使用第三方库QJson解析JSON文件. QT5新增加了处理JSON的类,类均以QJson开头,包含在QtCore模块中.QT5新增加六个相关类: QJsonArray 封装 JSON 数组 QJsonDocument 读写 JSON 文档 QJsonObject 封装 JSON 对象 QJsonObject::iterator 用于遍历QJsonObject的STL风格的非const遍历器 QJsonParseE

Maven管理SSM框架的pom.xml文件配置(自动下载所依赖的jar包)

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion&