`

Spring AOP使用步骤

 
阅读更多

在Spring中使用AOP编程步骤:


以下使用的Spring为2.5.6版本


1、在Spring配置文件(applicationContext.xml)中配置Spring对AspectJ的支持;以下两种方式任意一种即可

     <!-- <aop:aspectj-autoproxy/> -->
    <bean class="org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator"/>


2、引入Jar文件(在Spring的根目录下的lib/aspectj下)

     aspectjrt.jar  和  aspectjweaver.jar


3、定义连接点(若不使用接口编程,即SimplePersonManager不实现任何接口,此时要实现AOP代理,需要引入CGLIB 包cglib-nodep-2.1_3.jar,但是一旦实现了接口Spring就会使用JDK的动态代理实现)

 

public interface PersonManager {

	public void addPerson(Person person);
}

 

public class SimplePersonManager implements PersonManager{

	public void addPerson(Person person) {
		Person.persons.add(person);
                System.out.println("添加了一个用户");
	}
}
 

 

4、定义一个切面类,该切面类中定义了一个切入点,连接点为SimplePersonManager类中的addPerson方法

 

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;

@Aspect
public class SimpleAspect {

	@Before ("execution (* rote.spring.aop.service.impl.SimplePersonManager.addPerson(..))")
	public void addPersonAop(){
		System.out.println("-------------------addPerson-----------------------");
	};
	
}

 

5、在Spring配置文件中声明切面类

    <!-- 切面声明 -->
    <bean id="simpleAspectj" class="rote.spring.aop.aspectj.SimpleAspect">
    </bean>


6、在Spring配置文件中配置连接点类

     <bean id="personManager" class="rote.spring.aop.service.impl.SimplePersonManager">
    </bean>


7、启动测试

 

ApplicationContext context = new ClassPathXmlApplicationContext("rote/spring/aop/applicationContext-aop.xml");
		
PersonManager personManager = (PersonManager)context.getBean("personManager");
		
personManager.addPerson(new Person());

 上述代码运行后,将输出如下结果,在调用addPerson方法之前执行了addPersonAop方法:

-------------------addPerson-----------------------
添加了一个用户

 

分享到:
评论

相关推荐

    使用SpringAop使用Oracle数据权限控制

    接下来,我们使用Spring AOP来实现数据过滤。这通常涉及以下步骤: 1. **定义切点(Pointcut)**:确定哪些方法需要进行权限检查。这可以通过注解或XML配置来实现,例如定义一个名为`@DataAccess`的注解,并将其...

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

    2. **轻量级**:与一些需要预编译器的AOP框架不同,Spring AOP无需特殊的工具或编译步骤即可使用。这种轻量级的特性使得Spring AOP更易于学习和集成。 3. **灵活的通知模型**:Spring AOP提供了多种类型的通知,...

    spring AOP依赖三个jar包

    这个jar包使得Spring应用程序能够方便地利用AOP特性,无需引入额外的编译工具或构建步骤。 在实际应用中,这三个jar包的协同工作使得Spring AOP能够实现以下功能: - **切点定义**:使用AspectJ的切点表达式(例如...

    Spring AOP简单demo

    **Spring AOP 简介** Spring AOP(Aspect Oriented Programming,面向切面编程)是Spring框架的一个重要模块,它扩展...在Spring AOPD1这个压缩包中,可能包含了创建和运行上述示例的步骤和代码,是学习AOP的好材料。

    springaop多数据库读写分离

    要实现Spring AOP的多数据库读写分离,我们需要以下步骤: 1. **配置数据源**:首先,我们需要配置多个数据源,一个作为主库(处理写操作),另一个或多个作为从库(处理读操作)。这可以通过Spring的`...

    spring AOP入门教程

    - **SpringAOP.doc**:可能是文档教程,包含了详细的步骤和示例代码。 - **SpringAOP_src.rar**:源代码示例,供你参考和实践。 - **readme.txt**:可能包含了关于这些资源的使用指南和注意事项。 通过学习和实践...

    spring AOP注解的应用1

    ### 使用Spring AOP的步骤 1. **创建Aspect(切面)**:定义一个类,并使用`@Aspect`注解标记它。 2. **定义切入点(Pointcut)**:使用`@Pointcut`注解定义切入点表达式,匹配需要拦截的方法。 3. **编写通知...

    SpringAOP实例.docx

    在本例中,我们将使用 Spring AOP 来实现日志记录功能,记录商品的 service 层的日志操作。下面是实现的步骤和关键技术点: 1. 创建商品日志记录表 Product_Log 在日志记录功能中,首先需要创建一个用于存储日志...

    spring aop jar

    Spring AOP使用一个名为`@Pointcut`的注解来声明切入点,并通过`@Before`, `@After`, `@AfterReturning`, `@AfterThrowing` 和 `@Around` 注解与通知关联。 3. **织入(Weaving)**:织入是将切面应用到目标对象以...

    第四章:Spring AOP API 设计模式1

    11. **模板方法模式(Template Method)**:在Spring AOP中,模板方法模式可能体现在一些基类中,定义了操作的基本步骤,并允许子类在特定步骤中进行扩展。 12. **责任链模式(Chain of Responsibility)**:Spring...

    spring aop支持jdk1.7的jar

    在实际开发中,我们可以通过以下步骤来利用Spring AOP和AspectJ: 1. 定义切面:创建一个Java类,使用`@Aspect`注解标记,然后在这个类中定义切入点表达式(@Pointcut)和通知(@Before、@After、@Around、@After...

    Spring AOP的简单实现

    在这个场景中,我们将使用Spring AOP来实现一个日志记录的功能,以追踪系统中各个方法的调用情况,包括访问时间以及传递的参数。下面将详细阐述如何实现这一目标。 首先,我们需要了解AOP的基本概念。AOP的核心是切...

    SpringAop学习笔记以及实现Demo

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

    spring aop需要的jar包

    Spring AOP通过实现这些接口与AOP Alliance保持兼容,这样你可以在Spring环境中使用非Spring的AOP框架的切面。 在实际使用Spring AOP时,你需要在项目中引入这些库,并配置Spring容器来识别和处理切面。这通常涉及...

    spring-boot aop

    这个`spring-aop`示例项目很可能会包含上述步骤的实现,通过阅读源代码,我们可以更深入地理解Spring Boot AOP的使用。同时,它可能还会展示如何结合实际业务逻辑,比如服务层的方法调用,来演示AOP如何优雅地注入...

    一个简单的spring AOP的实现

    对于提供的文件名"mySpring",可能是一个包含Spring配置或示例代码的文件,其中可能包含了关于如何实现Spring AOP的具体步骤和示例。通过查看这个文件,我们可以更深入地理解Spring AOP的实际应用和配置。在实际开发...

    spring aop实例annotation方法实现

    为了启用注解驱动的AOP,需要在Spring配置文件中添加`&lt;aop:aspectj-autoproxy&gt;`元素,或者在Java配置中使用`@EnableAspectJAutoProxy`注解。 ```xml &lt;aop:aspectj-autoproxy /&gt; ``` 或者 ```java @Configuration ...

    spring aop hessian 基础demo 部署即可运行

    Spring AOP和Hessian是两个在Java开发中广泛使用的技术,尤其在分布式系统和服务治理中。下面将详细解释这两个概念以及它们如何结合使用。 **Spring AOP(面向切面编程)** Spring AOP是Spring框架的一个重要组成...

    Spring AOP配置实例

    **配置Spring AOP的基本步骤** 1. **引入依赖** 首先,你需要在项目中引入Spring AOP的依赖。如果你使用的是Maven,可以在pom.xml文件中添加如下依赖: ```xml &lt;groupId&gt;org.springframework &lt;artifactId&gt;...

Global site tag (gtag.js) - Google Analytics