`

Spring基础入门五:基于XML和注解的Spring的AOP使用

阅读更多

1。首先基于注解配置的AOP使用:(在学习Spring的AOP之前建意先去学习一下Java的JDK动态代理和CGLIB的代理技术,AOP是基于代理实现的,JDK的动态代理需要目标对象实现一个接口,若没有实现接口则可以使用CGLIB,它的代理对象是继承目标对象。)

目标对象的接口如下:

 

public interface PersonService {

	public abstract void save(String name);

	public abstract String getPersonName(String personId);

	public abstract void deletePerson(Integer id);
}

 

目标对象(PersonServiceImple):

public class PersonServiceImple implements PersonService {

	public void save(String name) {
		System.out.println("aop.annotation.service.imple.PersonServiceImple的save()方法");
		// throw new RuntimeException("手动引用的一个异常信息");
	}

	public String getPersonName(String personId) {
//		throw new RuntimeException("手动抛出的异常信息....");
		return "http://zmx.iteye.com";
		
	}
	
	public void deletePerson(Integer id){
		throw new RuntimeException("手动抛出的异常信息....");
	}
}

 使用Spring的注解配置一个Spring的切面

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;

//@Aspect用来标示一个类为切面
@Aspect
public class MyInterceptor {
	// @Pointcut用来设置一个切入点。aop.annotation.service..包(子包)内的所有类,所有方法,任何参数
	@Pointcut("execution(* aop.annotation.service.imple..*.*(..))")
	private void anyMethod() {

	}

	// 使用@Before(切入点)用来表示目标方法执行前执行的操作(前置通知)
	// @Before("anyMethod()")
	@Before("anyMethod() && args(nameArg)")
	// 使用这个方法可以获取参数。即:在原来的切入点条件上加了另一个条件即:拦截方法的参数有一个并且是String类型
	public void doBefore(String nameArg) {
		System.out.println("前置通知...拦截方法执行参数:" + nameArg);
	}

	// 使用@AfterReturning(切入点)用来表示目标方法执行完执行的操作(后置通知)
	// @AfterReturning("anyMethod()")
	@AfterReturning(pointcut = "anyMethod()", returning = "returnArg")
	// 使用这个方法可以获取返回结果。即:在原来的切入点条件上加了另一个条件即:拦截方法的返回值类型是String类型
	public void doAfterReturning(String returnArg) {
		System.out.println("后置通知...拦截方法返回结查:" + returnArg);
	}

	// 使用@After(切入点)用来表示目标方法执行无论是否出现异常都执行的操作(最终通知)
	@After("anyMethod()")
	public void doFinally() {
		System.out.println("最终通知...");
	}

	// 使用@AfterThrowing(切入点)用来表示目标方法执行出现异常时执行的操作(例外通知)
	// @AfterThrowing("anyMethod()")
	@AfterThrowing(pointcut = "anyMethod()", throwing = "ex")
	public void doException(Exception ex) {
		System.out.println("例外通知...取获异常信息:" + ex);
	}

	// 使用@Around(切入点)用来表示整个通知(环绕通知:该方法必须接受一个org.aspectj.lang.ProceedingJoinPoint类型的参数)
	@Around("anyMethod()")
	public Object doAround(ProceedingJoinPoint pjp) throws Throwable {
		System.out.println("环绕通知之前...");
		Object result = pjp.proceed();
		System.out.println("环绕通知之后...");
		return result;
	}

}

 

 在Spring的配置XML中打开对象上述注解的功能和向Spring容器中注册该目标对象

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	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/aop
           http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
	<!-- 基于注解方式的声明切面 -->
	<aop:aspectj-autoproxy />
	
	<bean id="personService"
		class="aop.annotation.service.imple.PersonServiceImple">
	</bean>
	<bean id="myInterceptor" class="aop.annotation.aspect.MyInterceptor"></bean>
	
</beans>

 

 

 测试:

public static void main(String[] args) {
		ApplicationContext ctx = new ClassPathXmlApplicationContext("beansAOP.xml");
		PersonService personService = (PersonService) ctx.getBean("personService");
		//personService.save("小张");//执行参数
		personService.getPersonName("mengya");//返回结果
		//personService.deletePerson(123);
	}

2。首先基于XML配置的AOP使用:

目标对象接口:

public interface PersonService {

	public abstract void save(String name);

	public abstract String getPersonName(String personId);

	public void deletePerson(Integer id);
}

 

目标对象:

public class PersonServiceImple implements PersonService {

	public void save(String name) {
		System.out
				.println("aop.xml.service.imple.PersonServiceImple的save()方法");
		// throw new RuntimeException("手动引用的一个异常信息");
	}
	public String getPersonName(String personId) {
//		throw new RuntimeException("手动抛出的异常信息....");
		return "http://zmx.iteye.com";
		
	}
	public void deletePerson(Integer id){
		throw new RuntimeException("手动抛出的异常信息....");
	}
}

 

Spring的切面对象:

public class MyInterceptor {

	public void doBefore() {
		System.out.println("前置通知...");
	}

	public void doAfterReturning(String returnArg) {
		System.out.println("后置通知...拦截方法返回结查:" + returnArg);
	}

	public void doFinally() {
		System.out.println("最终通知...");
	}

	public void doException(Exception ex) {
		System.out.println("例外通知...取获异常信息:" + ex);
	}

	public Object doAround(ProceedingJoinPoint pjp) throws Throwable {
		System.out.println("环绕通知之前...");
		Object[] args = pjp.getArgs();
		for (int i = 0; i < args.length; i++) {
			System.out.println(args);
		}
		Object result = pjp.proceed();
		System.out.println("环绕通知之后...");
		return result;
	}

}

 

在XML配置上述内容:

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	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/aop
           http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">

	<bean id="personService"
		class="aop.xml.service.imple.PersonServiceImple">
	</bean>
	
	<bean id="myInterceptor" class="aop.xml.aspect.MyInterceptor"></bean>
	
	<!-- AOP配置 -->
	<aop:config>
		<!-- 切面 -->
		<aop:aspect id="myAspect" ref="myInterceptor">
			<aop:pointcut id="myAnyMethod"
				expression="execution(* aop.xml.service.imple.*.*(..))" />
			<aop:before pointcut-ref="myAnyMethod" method="doBefore"/>
			<aop:after-returning method="doAfterReturning" pointcut-ref="myAnyMethod" returning="returnArg"/>
			<aop:after-throwing method="doException" pointcut-ref="myAnyMethod" throwing="ex"/>
			<aop:after method="doFinally" pointcut-ref="myAnyMethod"/>
			<aop:around method="doAround" pointcut-ref="myAnyMethod"/>
		</aop:aspect>
	</aop:config>

</beans>

 测试:

public static void main(String[] args) {
		ApplicationContext ctx = new ClassPathXmlApplicationContext(
				"beansAOP2.xml");
		PersonService personService = (PersonService) ctx.getBean("personService");
//		personService.save("AOP");
		personService.getPersonName("123");
		
	}

  

5
1
分享到:
评论

相关推荐

    spring aop注解方式、xml方式示例

    演示了spring对aop的支持,包括注解方式、基于xml方式。

    Spring 使用AspectJ 实现 AOP(基于xml文件、基于注解)

    Spring 使用AspectJ 实现 AOP(基于xml文件、基于注解)

    Spring中Aop的使用包括xml和注解

    Aop 1.xml配置aop 2.注解配置aop

    Spring实现AOP的多种方式 切点函数

    里面包括4个例子:(1)Spring实现AOP方式之一:基于XML配置的Spring AOP (2)Spring实现AOP方式之二:使用注解配置 Spring AOP (3)Spring AOP : AspectJ Pointcut 切点 (4)Spring AOP : Advice 声明 (通知注解)

    spring注解&XML配置AOP

    spring注解 XML配置AOP的一个实例

    实验2 Spring AOP源码

    2、掌握基于XML/注解方式的AOP编程; 二:实验内容 1、 定义交易接口: public interface Transaction{ public void income(float amount);//收入 public void expenditure(float amount);//支出 } 定义银行账号...

    Spring AOP实验

    一、 实验目的 1、了解AOP的概念和作用; 2、理解AOP中的相关术语; 3、了解Spring中两种动态代理方式的区别;...分别使用基于XML和注解的AspectJ实现上述功能(创建两个项目)。 (图就是int加减乘除)

    springboot spring aop 拦截器注解方式实现脱敏

    springboot spring aop 拦截器 注解方式实现脱敏(涉及到:pom.xml --&gt;application.properties ---&gt;启动类--&gt;拦截器)

    Spring_AOP开发

    此文档介绍了Spring Aop编程的两种方法: 1、基于XML配置方式进行AOP开发。2、基于注解方式进行AOP开发。 文档中有详细的例子。

    JavaEE Spring AOP使用

    压缩包中函数Spring AOP开发时使用注解和xml文件配置demo

    Spring使用AspectJ注解和XML配置实现AOP

    主要介绍了Spring使用AspectJ注解和XML配置实现AOP的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

    Spring常问的真实大厂面试题汇总(含答案)

    spring面试题 Spring 是个java企业级应⽤的开源开发框架。Spring主要⽤来开发Java应⽤,但是有些扩展是针对构建J2EE平台的web应⽤。...常见的配置⽅式有三种:基于XML的配置、基于注解的配置、基于Java的配置。

    springaop+arg

    springaop的注解和xml配置,也包括aop处理参数的方法

    Spring高级之注解驱动开发视频教程

    学习spring,要有一定的Java基础,同时应用过spring基于xml的配置。(或者学习过官网的Spring课程) 学习springmvc,要有一定java web开发基础。同时对spring框架要有一定了解。 3、课程亮点 系统的学习Spring框架...

    注解+AOP优雅的实现java项目的接口参数校验(含源码)

    基于Spring boot + maven,以注解+AOP方式实现的java后端项目接口参数校验框架。迄今为止使用最简单、最容易理解的参数校验方案。博客地址:https://blog.csdn.net/weixin_42686388/article/details/104009771

    spring_aop.rar

    Spring-Aop 一、Aop的概述 1.1、什么是Aop(摘自百度) 1.2、Aop的相关术语 二、基于XML的AOP配置 三、基于注解的AOP配置

    25个经典的Spring面试问答

    配置与注解:理解Spring的XML配置和注解配置,并能够在这两种配置方式中进行选择。 Spring AOP与AspectJ:理解Spring AOP的概念,以及如何使用AspectJ实现AOP。 Spring与其它技术集成:了解Spring与其它技术的集成,...

    Spring插件安装图解

    在 Spring 中可以使用 XML 和 Java 注解组合这些对象 一站式:在 IOC 和 AOP 的基础上可以整合各种企业应用的开源框架和优秀的第三方类库 (实际上 Spring 自身也提供了展现层的 SpringMVC 和 持久层的 Spring JDBC...

    Spring面试专题.md

    常见的配置方式有三种:基于XML的配置、基于注解的配置、基于Java的配置. 主要由以下几个模块组成: * Spring Core:核心类库,提供IOC服务; * Spring Context:提供框架式的Bean访问方式,以及企业级功能(JNDI...

    struts hibernate spring 集成时使用依赖注解的方式的秘籍

    //applicationContext.xml文件中添加 &lt;beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi=...

Global site tag (gtag.js) - Google Analytics