`
panmingzhi0815
  • 浏览: 13132 次
  • 性别: Icon_minigender_2
  • 来自: 北京
最近访客 更多访客>>
社区版块
存档分类
最新评论

用注解的方式进行SpringAOP开发

阅读更多

1.先定义一个简单的接口:

package org.pan.service;

public interface PersonService {
	
	public void save();
	
	public void update();
	
	public void delete();

}

 

2.实现这个接口:

package org.pan.service.impl;

import org.pan.service.PersonService;

public class PersonServiceBean implements PersonService{

	@Override
	public void delete() {
		System.out.println("调用delete方法");
	}

	@Override
	public void save() {
		System.out.println("调用save方法");	
	}

	@Override
	public void update() {
		System.out.println("调用update方法");	
	}

}

 3.定义一个拦截类:

package org.pan.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;

@Aspect	//此字段说明此类为切面
@SuppressWarnings("unused")
public class PersonInterceptor  {
	
	@Pointcut("execution (* org.pan.service.*.*(..))")
	private void anyMethod(){}//声明一个切点
	
	@Before("anyMethod()")
	public void doAccess(){
		System.out.println("前置通知!");
	}
	
	@AfterReturning("anyMethod()")
	public void afterDo(){
		System.out.println("后置通知!");
	}
}

 4.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:p="http://www.springframework.org/schema/p"
	xmlns:aop="http://www.springframework.org/schema/aop" 
	xmlns:tx="http://www.springframework.org/schema/tx"
	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 
	http://www.springframework.org/schema/tx 
	http://www.springframework.org/schema/tx/spring-tx-2.5.xsd ">
	
	<aop:aspectj-autoproxy />
	
	<bean id="personInterceptor" class="org.pan.aop.PersonInterceptor" />
	
	<bean id="personService" class="org.pan.service.impl.PersonServiceBean" />
	
</beans>

 大功告成了,下面来单元测试一下吧:

package junit.test;

import org.junit.BeforeClass;
import org.junit.Test;
import org.pan.service.PersonService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class SpringAopTest {

	@BeforeClass
	public static void setUpBeforeClass() throws Exception {
	}
	@Test public void interceptorTest(){
		ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
		PersonService personService=(PersonService)ctx.getBean("personService");
		personService.save();
	}
}

 

  • 大小: 14.1 KB
分享到:
评论

相关推荐

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

    本篇文章将深入探讨如何通过Spring的注解方式实现AOP的细节。 首先,我们需要了解AOP的基本概念。AOP的核心是切面(Aspect),它封装了跨越多个对象的行为或责任。切点(Pointcut)定义了哪些方法会被通知(Advice...

    用xml配置的方式进行SpringAOP开发

    通过以上步骤,我们就成功地使用XML配置完成了Spring AOP的开发。这种方式虽然相对繁琐,但它清晰地展示了AOP的各个组成部分,有利于理解和学习。然而,在实际开发中,更常见的是使用注解驱动的AOP,因为其更简洁且...

    spring aop jar 包

    在使用Spring AOP时,我们可以通过XML配置或注解的方式来定义切面。例如,可以使用`@Aspect`注解定义一个切面类,`@Before`、`@After`等注解来声明通知,`@Pointcut`定义切点表达式。 在实际开发中,Spring AOP广泛...

    Spring AOP 注解方式

    本篇文章将深入探讨如何使用注解方式在Spring AOP中实现内部方法的拦截。 首先,理解AOP的基本概念至关重要。AOP的核心是切面(Aspect),它封装了横切关注点,即那些跨越多个对象的业务逻辑,如日志记录、事务管理...

    spring AOP注解的应用1

    在Spring AOP中,我们使用`@Before`注解来定义一个前置通知。这个注解需要与一个方法关联,该方法将在目标方法执行前被调用。例如: ```java @Aspect public class LoggingAspect { @Before("execution(* ...

    Spring注解方式实现AOP demo

    本篇将通过注解方式探讨如何在Spring中实现AOP,基于提供的资源,我们可以看到一个实际的Demo项目结构。 首先,我们来看项目的基本结构: 1. `bin`目录:编译后的Java类文件会放在这里。 2. `.settings`目录:包含...

    spring注解aop配置详解

    Spring AOP,即Aspect-Oriented Programming(面向切面编程),是Spring框架的重要特性,它为应用程序提供了声明式的企业级服务,如...在实际开发中,熟练掌握Spring AOP的注解配置无疑会极大地提升我们的工作效率。

    spring 使用 AspectJ注解开发Spring AOP.docx

    在本案例中,我们将探讨如何使用AspectJ注解来开发Spring AOP。 首先,我们要了解Spring AOP的主要目标是为方法调用提供拦截机制,也就是在方法执行前、后或发生异常时执行特定的操作。这通常被称为通知(advises)...

    spring aop依赖jar包

    现在,我们回到主题——"springaop依赖的jar包"。在Spring 2.5.6版本中,使用Spring AOP通常需要以下核心jar包: - `spring-aop.jar`:这是Spring AOP的核心库,包含了AOP相关的类和接口。 - `spring-beans.jar`:...

    SpringAOP注解特棒例子

    在这个"SpringAOP注解方式"的示例中,我们将深入探讨如何使用注解来实现Spring AOP的功能。 首先,Spring AOP通过两种主要的方式来定义切面:XML配置和注解。本示例主要关注注解方式,因为它提供了更简洁、更直观的...

    spring aop的demo

    在`springAop1`这个压缩包中,可能包含了一个简单的应用示例,展示了如何定义一个切面类,以及如何在该类中定义通知方法。例如,我们可能会看到一个名为`LoggingAspect`的类,其中包含了`@Before`注解的方法,用于在...

    JavaEE Spring AOP使用

    JavaEE Spring AOP(面向切面编程)是企业级应用开发中的重要技术,它允许...无论是通过注解还是XML配置,掌握Spring AOP的使用都能显著提升我们的开发效率。在实践中,可以根据项目的具体需求选择最适合的实现方式。

    使用Spring的注解方式实现AOP

    本篇文章将深入探讨如何通过注解方式在Spring中实现AOP。 首先,我们需要了解AOP的基本概念。AOP是一种编程范式,它允许程序员定义“切面”,这些切面包含了一组相关操作,可以在多个对象或方法上统一实施。Spring ...

    注解配置SpringAOP共4页.pdf.zip

    标题"注解配置SpringAOP共4页.pdf.zip"指出这是一个关于Spring AOP(面向切面编程)的文档,通过注解方式进行配置,文档共有四页。通常,这样的资料会涵盖Spring AOP的基础概念、注解的使用以及如何在实际应用中配置...

    spring AOP依赖三个jar包

    Spring AOP,即Spring的面向切面编程模块,是Spring框架的重要组成部分,它允许开发者在不修改源代码的情况下,对程序进行横切关注点的处理,如日志、事务管理等。实现这一功能,主要依赖于三个核心的jar包:aop...

    Spring基于注解实现AOP

    在Java开发领域,Spring框架是不可或缺的一部分,尤其在企业级应用中广泛使用。Spring AOP(面向切面编程)提供了一种优雅的方式来处理系统中的横切关注点,如日志、事务管理等。本篇文章将深入探讨如何在Spring中...

    Spring AOP面向方面编程原理:AOP概念

    Spring AOP支持多种方式来定义切入点,包括但不限于方法名、类名、注解等。 5. **引入(Introduction)**:允许向被通知对象添加新的接口实现或者新的方法。例如,可以使用引入让任何对象实现`IsModified`接口,...

    Spring Aop使用实例

    Spring AOP有两种实现方式:基于代理的AOP(JDK动态代理和CGLIB代理)和基于注解的AOP。 - **JDK动态代理**:当目标类实现了接口时,Spring会使用JDK的Proxy类创建一个代理对象,该代理对象会在调用接口方法时插入...

    详解使用Spring AOP和自定义注解进行参数检查

    使用Spring AOP和自定义注解进行参数检查的技术可以应用于各种场景,例如:Web开发、微服务架构、分布式系统等。该技术可以帮助我们提高代码的可读性和可维护性,减少代码的重复性和耦合度。 优点 使用Spring AOP...

Global site tag (gtag.js) - Google Analytics