`
dwj147258
  • 浏览: 192027 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

基于注解实现spring AOP

阅读更多

springaop使用很简单,

一,配置文件

在spring的配置文件中激活组件扫描,激活自动代理功能

<?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:jms="http://www.springframework.org/schema/jms"
          xmlns:amq="http://activemq.apache.org/schema/core"
          xmlns:aop="http://www.springframework.org/schema/aop"
        xsi:schemaLocation=" http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd 
        	  http://activemq.apache.org/schema/core http://activemq.apache.org/schema/core/activemq-core-5.8.0.xsd
              http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  
              http://www.springframework.org/schema/jms http://www.springframework.org/schema/jms/spring-jms-3.0.xsd
              http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd">  
        <!-- 激活组件扫描功能,在包cn.ysh.studio.spring.aop及其子包下面自动扫描通过注解配置的组件 -->
	<context:component-scan base-package="main.java.com.springaop"/>
	<!-- 激活自动代理功能 -->
	<aop:aspectj-autoproxy proxy-target-class="true"/>
	<!-- 用户服务对象 -->
	<bean name="service" class="main.java.com.springaop.DomainObject"/>
  </beans>

 二、需要代理的对象

这个对象必须是spring中注册的对象,也就是说必须是在spring配置文件中声明的对象

public class DomainObject {
	private String name = "d" ;
	public void show(){
		System.out.println("name="+name);
	}
}

 三、AOP实现

这是最核心的,代理对象的触发方法,方法运行前运行后的通知都在这里实现:

@Component
@Aspect
public class ServiceAspect {
	
	@Before("execution(* main.java.com.springaop.DomainObject.show(..))")
	public void beforDo(JoinPoint joinPoint){
		System.out.println("before");
	}
	
	@Around("execution(* main.java.com.springaop.DomainObject.show(..))")
	public void around(JoinPoint joinPoint){
		long start = System.currentTimeMillis();
		try {
			ProceedingJoinPoint jj = (ProceedingJoinPoint)joinPoint ;
			jj.proceed();
			long end = System.currentTimeMillis();
				System.out.println("around " + joinPoint + "\tUse time : " + (end - start) + " ms!");
		} catch (Throwable e) {
			long end = System.currentTimeMillis();
			System.out.println("around " + joinPoint + "\tUse time : " + (end - start) + " ms with exception : " + e.getMessage());
		}
	}
}

 这里只实现了两个方面,

测试代码

public class AopTest {
	public static void main(String[] args) {
		ApplicationContext ap = new ClassPathXmlApplicationContext("main/java/conf/applicationContext.xml");
		DomainObject obj = (DomainObject) ap.getBean("service");
		obj.show();
	}
}

 如此简单,为何不用

分享到:
评论

相关推荐

    基于注解实现SpringAop

    基于注解实现SpringAop基于注解实现SpringAop基于注解实现SpringAop

    Spring基于注解实现AOP

    本篇文章将深入探讨如何在Spring中通过注解实现AOP。 首先,了解AOP的基本概念。面向切面编程是一种编程范式,它允许程序员定义“切面”,这些切面包含了跨越多个对象的行为或责任。切点是这些行为插入到主业务逻辑...

    使用Spring的注解方式实现AOP的细节

    5. **@EnableAspectJAutoProxy**: 在Spring配置类上添加此注解,启用基于Java代理的AOP支持,这样Spring会自动检测并处理带有@Aspect注解的类。 ```java @Configuration @EnableAspectJAutoProxy public class ...

    spring aop 自定义注解保存操作日志到mysql数据库 源码

    2、能够清楚的知道如何用spring aop实现自定义注解以及注解的逻辑实现 (需要知道原理的请看spring aop源码,此处不做赘述) 3、可在现有源码上快速进行功能扩展 4、spring boot,mybatis,druid,spring aop的使用

    Spring 基于注解方式实现AOP

    在Spring框架中,基于注解的AOP(面向切面编程)是一种强大的工具,它允许开发者无需编写XML配置即可实现切面。这种编程方式极大地提高了代码的可读性和可维护性。下面我们将深入探讨如何使用注解来实现Spring AOP。...

    spring aop jar 包

    这个"spring aop jar 包"包含了实现这一功能所需的类和接口,使得开发者能够轻松地实现面向切面的编程。 在Spring AOP中,主要涉及以下几个核心概念: 1. **切面(Aspect)**:切面是关注点的模块化,比如日志记录...

    Spring AOP实现机制

    **Spring AOP 实现机制详解** Spring AOP(面向切面编程)是Spring框架的核心特性之一,它允许程序员在不修改源代码的情况下,通过“切面”来插入额外的业务逻辑,如日志、事务管理等。AOP的引入极大地提高了代码的...

    Spring AOP 16道面试题及答案.docx

    Spring支持两种AOP的实现方式:Spring AspectJ注解风格和Spring XML配置风格。使用AspectJ注解风格是最常见的,它允许开发者直接在方法上使用注解来定义切面。 Spring AOP中有五种不同类型的的通知(Advice): 1....

    反射实现 AOP 动态代理模式(Spring AOP 的实现原理)

    在Spring AOP中,我们通常使用@Aspect注解来定义切面类。切面类中可以包含多个通知(Advice),包括前置通知(Before)、后置通知(After)、返回通知(After-returning)、异常通知(After-throwing)和环绕通知...

    Spring AOP 的实现例子(基于XML配置实现)

    XML配置是Spring AOP早期的主要实现方式,虽然现在有更简洁的注解式配置,但理解XML配置方式对于深入理解AOP原理仍然很有帮助。下面我们将详细探讨如何通过XML配置实现Spring AOP。 首先,我们需要在Spring配置文件...

    基于xml的SpringAOP实例

    在基于XML的配置方式下,Spring AOP提供了直观且灵活的声明式方法来实现这些关注点的分离,使得业务逻辑代码更为简洁。 在Spring AOP中,我们首先需要定义一个切面(Aspect),它包含了若干个通知(Advice)。通知...

    springboot+aspect实现springaop拦截指定方法.zip

    SpringBoot结合AspectJ实现SpringAOP拦截指定方法的知识点涵盖了多个方面,这包括Spring AOP的基本概念、SpringBoot的应用、切点(Pointcut)与通知(Advice)的定义、自定义注解以及AspectJ的使用。以下是这些知识...

    Spring Aop的简单实现

    在这个基于配置文件的实现中,我们首先需要在Spring配置文件(如applicationContext.xml)中定义一个切面。切面通常由一个或多个通知组成,我们可以使用`&lt;aop:config&gt;`标签来声明切面。例如: ```xml &lt;aop:config&gt; ...

    spring aop注解版

    在本主题中,我们将深入探讨Spring AOP的注解版,它是基于Java注解的实现,简化了配置并提高了代码的可读性。 首先,让我们理解AOP的基本概念。AOP是一种编程范式,允许程序员定义“切面”,这些切面封装了跨越多个...

    spring aop demo 两种实现方式

    本示例提供了一种通过注解和配置文件两种方式实现Spring AOP的方法。 首先,我们来详细讲解通过注解实现Spring AOP。在Spring中,我们可以使用`@Aspect`注解来定义一个切面,这个切面包含了多个通知(advice),即...

    SpringAop学习笔记以及实现Demo

    2. **基于注解的AOP**:Spring 2.5开始支持注解驱动的AOP,通过在方法上使用`@Before`、`@After`、`@AfterReturning`、`@AfterThrowing`、`@Around`等注解来定义通知。 3. **基于XML配置的AOP**:在Spring的配置...

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

    ### Spring AOP 实现流程日志跟踪 #### 一、背景与目的 在现代软件开发过程中,为了确保系统的稳定性和可维护性,通常会引入非功能性的需求来增强应用程序的功能,比如日志记录、安全控制等。这些需求往往不是业务...

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

    总结一下,通过上述步骤,我们已经在Spring Boot应用中利用Spring AOP和注解方式实现了数据脱敏。这个拦截器可以在不修改原有业务代码的情况下,确保敏感信息在响应给客户端之前得到处理,提高了应用的安全性。同时...

    Spring AOP完整例子

    Spring AOP(面向切面编程)是Spring框架的核心特性之一,它允许开发者在不修改源代码的情况下,通过插入切面来增强或改变程序的行为。在本教程中,我们将深入探讨Spring AOP的不同使用方法,包括定义切点、通知类型...

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

    本教程将探讨如何在Spring中结合AspectJ实现AOP,包括基于XML配置和基于注解的方式。 **一、AOP基本概念** AOP的核心概念有切面(Aspect)、连接点(Join Point)、通知(Advice)、切点(Pointcut)和引入...

Global site tag (gtag.js) - Google Analytics