`

Spring中AOP切面的定义方式

阅读更多

Spring切面的定义有四种:

   一,基于Annotation也就是@AspectJ的注解方式

   二,基于<aop:aspect>的方式

   三,基于<aop:advisor>的方式

   四,基于Advisor类的方式

 

注:第四种一般不用,其他的看情况用

 

先看第一种基于Annotation的:

beans.xml(此处要加aop和context的命名空间的名字和location,然后加那三个配置,第一个是基于annotation,第二个是自动检测组件,第三个就是aspectj的支持)

<?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">
	<context:annotation-config />
	<context:component-scan base-package="com.lbx"/>
 	<aop:aspectj-autoproxy />
 	
</beans>

 

AOP就类似拦截器,使用AOP就可以轻松的处理很多的东西,例如:事务的处理,权限的处理,日记的处理,统计等

下面就是一个Advice(增强)(其中@Aspect表示是一个切面,@Pointcut是一个切点,相当于一个查询条件,@Before是一个前置增强,@After是一个后置增强)

package com.lbx.aop;

import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class LogInterceptor {
	
	@Pointcut("execution(public * com.lbx.dao..*.*(..))")
	public void getMethod(){}
	
	@Before("getMethod()")
	public void before(){
		System.out.println("LogInterceptor.before()");
	}
	
	//@After("getMethod()")
	//@AfterReturning("getMethod()")
	@AfterReturning("getMethod()")
	public void afterReturning(){
		System.out.println("LogInterceptor.afterReturning()");
	}
	
}	

 

测试方法没写,只要是满足那个@Pointcut条件的都可以进行增强

 

 

 

第二种基于<aop:aspect>的方式

bean.xml文件配置(把要植入的那个Advice加一个bean,然后使用<aop:aspect>来配置)

<?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">
	<context:annotation-config />
	<context:component-scan base-package="com.lbx"/>

	<bean id="logInterceptor" class="com.lbx.aop.LogInterceptor"></bean>
	<aop:config>
		<aop:pointcut expression="execution(public * com.lbx.dao..*.*(..))" id="servicePointCut"/>
		<aop:aspect id="logAspect" ref="logInterceptor">
			<aop:before method="before" pointcut-ref="servicePointCut"/>
			<aop:after method="afterReturning" pointcut-ref="servicePointCut"/>
		</aop:aspect>
	</aop:config>
 	
</beans>

 Advice代码

package com.lbx.aop;


public class LogInterceptor {
	
	public void getMethod(){}
	
	public void before(){
		System.out.println("LogInterceptor.before()");
	}
	
	public void afterReturning(){
		System.out.println("LogInterceptor.afterReturning()");
	}
	
}	

 

测试没写(效果和上面的一样)

分享到:
评论

相关推荐

    Spring AOP面向切面三种实现

    在IT行业中,Spring框架是Java企业级应用开发的首选,其强大的功能之一就是AOP(面向切面编程)。本文将详细解析Spring AOP的三种实现方式,帮助你深入理解这一重要概念。 首先,理解AOP的基本概念至关重要。AOP是...

    spring的aop切面编程实例

    3. **配置XML**: 在Spring的配置文件(如`applicationContext.xml`)中,你需要启用AOP代理并声明切面。首先,启用AOP上下文: ```xml &lt;aop:config&gt; &lt;/aop:config&gt; ``` 然后,声明切面,指定其类和通知: ```...

    spring aop 自定义切面示例

    在Spring AOP(面向切面编程)中,自定义切面是实现业务逻辑解耦、增强代码可维护性的重要手段。AspectJ是一个强大的面向切面的编程库,它提供了与Spring AOP集成的能力,使我们可以编写更为灵活和模块化的代码。...

    spring AOP切面编程

    在Spring AOP中,切面由通知(advice)和切点(pointcut)定义。 2. 通知(Advice):在特定的连接点上执行的动作,例如方法调用前、后或者异常发生时。 3. 切点(Pointcut):定义了通知将在何时应用。它可以是一个...

    Spring mvc mybatis plus 实现AOP 切面日志系统

    在IT行业中,Spring MVC、MyBatis Plus以及AOP(面向切面编程)是Java Web开发中的重要组件,常用于构建高效、灵活的企业级应用。本项目“Spring MVC Mybatis Plus 实现AOP 切面日志系统”旨在提供一个基础的日志...

    spring aop切面拦截指定类和方法实现流程日志跟踪

    为了解决这一问题,可以利用Spring框架中的AOP(Aspect Oriented Programming,面向切面编程)技术来实现。 #### 二、Spring AOP 概述 Spring AOP 是Spring框架提供的一种实现AOP的方法。通过AOP,开发者可以在不...

    Spring使用AOP的三个jar包

    在Java世界中,Spring框架以其强大的功能和灵活性深受开发者喜爱,尤其在面向切面编程(AOP)方面,Spring提供了非常全面的支持。AOP是一种编程范式,它允许程序员定义“切面”,这些切面可以封装关注点,如日志、...

    spring切面AOP所使用的jar包

    通过这个库,Spring AOP可以在不改变原有代码结构的情况下,将切面逻辑插入到方法调用的前、后或者异常处理等特定位置,提供了一种强大的代码解耦方式。 2. **aspectjrt.jar**: AspectJ Runtime库是AspectJ运行...

    Spring 动态代理和aop切面编程例子

    在Spring框架中,动态代理和AOP(面向切面编程)是两个重要的概念,它们极大地增强了代码的可维护性和灵活性。下面将详细讲解这两个概念及其实际应用。 动态代理,是Spring提供的一种机制,允许我们在不修改原对象...

    day39-Spring 06-Spring的AOP:带有切点的切面

    在Spring框架中,AOP(Aspect Oriented Programming,面向切面编程)是一种强大的设计模式,它允许程序员将关注点从核心业务逻辑中分离出来,如日志、事务管理等。在"day39-Spring 06-Spring的AOP:带有切点的切面...

    spring-aop-jar

    Spring AOP模块提供了实现AOP规范的功能,它允许开发者定义“切面”来封装系统中的横切关注点,如日志、事务管理等。该jar文件包含了Spring AOP的核心类和接口,如`org.springframework.aop.*`包下的`...

    Spring-aop面向切面编程实例

    面向切面编程(Aspect-Oriented Programming,AOP)是Spring框架的核心特性之一,它提供了一种优雅的方式来处理系统的横切关注点,如日志、事务管理、性能监控和权限控制等。在Spring中,AOP主要通过代理模式实现,...

    Spring面向切面编程AOP

    面向切面编程(AOP,Aspect Oriented Programming)是Spring框架中的一个重要特性,它提供了一种模块化和声明式的方式来处理程序中的横切关注点,如日志、事务管理、安全控制等。AOP的核心概念包括切面、通知、连接...

    Spring AOP切面编程简介

    **Spring AOP切面编程简介** 在Java世界中,面向切面编程(Aspect-Oriented Programming,简称AOP)是一种编程范式,它允许我们分离关注点,将横切关注点(如日志、事务管理、性能监控等)与业务逻辑代码解耦。...

    Spring实现AOP的4种方式

    在Spring的XML配置文件中,可以使用&lt;aop:config&gt;元素来定义切面,&lt;aop:pointcut&gt;定义切点表达式,&lt;aop:advisor&gt;定义通知,&lt;aop:aspect&gt;定义切面。这种方式虽然相对繁琐,但清晰地展示了各个组件之间的关系,对于...

    spring aop 切面添加日志

    在Spring框架中,AOP(面向切面编程)是一种强大的工具,它允许我们在不修改源代码的情况下,对程序的关键行为进行增强或监控。本项目旨在演示如何在Spring AOP中添加日志功能,以实现对应用程序执行过程的透明跟踪...

    mybatis 拦截器 + spring aop切面 + spring事务+ 反射工具类

    例如,可能会有一个自定义的MyBatis拦截器用于分页查询,一个Spring AOP切面用于记录操作日志,Spring事务管理确保数据的一致性,而反射工具类可能用于动态加载配置或处理某些通用的反射任务。通过这些组件的组合,...

    spring4 AOP 面向切面编程@Aspect

    在Spring框架中,AOP(面向切面编程)是一种强大的设计模式,它允许开发者将关注点从业务逻辑中分离出来,比如日志记录、事务管理、权限检查等。`@Aspect`是Spring AOP的核心注解,用于定义一个切面。下面我们将详细...

    spring-aop面向切面系统日志案例

    在IT行业中,Spring AOP(Aspect Oriented Programming,面向切面编程)是Spring框架的核心特性之一,它使得我们能够以一种声明式的方式处理系统中的横切关注点,如日志记录、事务管理、性能监控等。这个“spring-...

    spring-boot aop

    Spring Boot AOP(面向切面编程)是一种强大的设计模式,它允许我们在不修改现有代码的情况下,插入额外的功能或监控代码。在Spring框架中,AOP主要用于日志记录、事务管理、性能统计等场景。本示例是关于如何在...

Global site tag (gtag.js) - Google Analytics