一、引入Jar包

        <!--测试1使用-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
<!--测试2、3、4、5、6使用-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
<!--测试Aop使用-->
<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.1</version>
</dependency>

注意,如果不引入aspectjweaver包,会报找不到类的错误。

二、测试步骤

1、新建切入点类(JoinPoint):

package aoptest1;

public class MyWorker {
public void aopPointMethod1() {
System.out.println("this is aopPointMethod1 executed.");
}
}

2、建立增强类(Advice)

package aoptest1;

import org.aspectj.lang.ProceedingJoinPoint;

public class MyWorkerExtension {
public void aopInspectAtBefore() {
System.out.println("this is aopInspectAtBefore method execute.");
} public void aopInspectAtAfter() {
System.out.println("this is aopInspectAtAfter method execute.");
} public void aopAround(ProceedingJoinPoint proceedingJoinPoint) {
try {
System.out.println("aopAround1");
proceedingJoinPoint.proceed();
System.out.println("aopAround2");
} catch (Throwable throwable) {
throwable.printStackTrace();
}
}
}

3、建立配置文件在resources下:applicationContextAopTest1.xml

<?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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<context:component-scan base-package="aoptest1"/>
<!--配置实体-->
<bean id="myworker1" class="aoptest1.MyWorker"/>
<bean id="myworkerExtension1" class="aoptest1.MyWorkerExtension"/>
<!--配置AOP-->
<aop:config>
<!--配置切入点-->
<aop:pointcut expression="execution(* aoptest1.MyWorker.aopPointMethod1(..))" id="aopPointMethod1PointCut"/>
<!--配置切面-->
<aop:aspect ref="myworkerExtension1">
<aop:before method="aopInspectAtBefore" pointcut-ref="aopPointMethod1PointCut"/>
<aop:after method="aopInspectAtAfter" pointcut-ref="aopPointMethod1PointCut"/>
<aop:around method="aopAround" pointcut-ref="aopPointMethod1PointCut"/>
</aop:aspect> </aop:config>
</beans>

4、测试

import aoptest1.MyWorker;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class AopTest {
@Test
public void aopTest1() {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContextAopTest1.xml");
MyWorker mywoker = context.getBean(MyWorker.class);
mywoker.aopPointMethod1();
}
}

5、测试结果

this is aopInspectAtBefore method execute.
aopAround1
this is aopPointMethod1 executed.
aopAround2
this is aopInspectAtAfter method execute.

使用Spring框架入门三:基于XML配置的AOP的使用的更多相关文章

  1. Spring框架入门之基于xml文件配置bean详解

    关于Spring中基于xml文件配置bean的详细总结(spring 4.1.0) 一.Spring中的依赖注入方式介绍 依赖注入有三种方式 属性注入 构造方法注入 工厂方法注入(很少使用,不推荐,本 ...

  2. Spring框架入门之基于Java注解配置bean

    Spring框架入门之基于Java注解配置bean 一.Spring bean配置常用的注解 常用的有四个注解 Controller: 用于控制器的注解 Service : 用于service的注解 ...

  3. Spring3.0 入门进阶(三):基于XML方式的AOP使用

    AOP是一个比较通用的概念,主要关注的内容用一句话来说就是"如何使用一个对象代理另外一个对象",不同的框架会有不同的实现,Aspectj 是在编译期就绑定了代理对象与被代理对象的关 ...

  4. spring框架之AspectJ的XML方式完成AOP的开发

    1. 步骤一:创建JavaWEB项目,引入具体的开发的jar包 * 先引入Spring框架开发的基本开发包 * 再引入Spring框架的AOP的开发包 * spring的传统AOP的开发的包 * sp ...

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

    本文演示的是Spring中使用AspectJ注解和XML配置两种方式实现AOP 下面是使用AspectJ注解实现AOP的Java Project首先是位于classpath下的applicationC ...

  6. 基于XML配置的AOP实现日志打印

    Spring中可以使用注解或XML文件配置的方式实现AOP.1.导入jar包 com.springsource.net.sf.cglib -2.2.0.jar com.springsource.org ...

  7. Spring依赖注入:基于xml配置

    基础接口 BeanFactory.ApplicationContext. BeanFactory用于创建并管理.获取各种类的对象. ApplicationContext从BeanFactory派生而来 ...

  8. 一步一步深入spring(6)--使用基于XML配置的spring实现的AOP

    上节我们提到了使用基于注解实现的AOP,这节我们将用基于xml配置的方式来实现的AOP. 1.首先建立一个类,作为切面类,这个类主要用来实现注解中各种通知要实现的方法. package com.yan ...

  9. Spring3.2 中 Bean 定义之基于 XML 配置方式的源码解析

    Spring3.2 中 Bean 定义之基于 XML 配置方式的源码解析 本文简要介绍了基于 Spring 的 web project 的启动流程,详细分析了 Spring 框架将开发人员基于 XML ...

随机推荐

  1. SQLServer2008:在查看表记录或者修改存储过程时出现错误。错误消息为: 目录名无效

    登陆数据库后,右键打开表提示:目录名无效,执行SQL语句也提示有错误,本来想重装的这个肯定能解决,但是这个方法真的不视为上上策啊,于是在网上找到了这个解决办法,还真是立即见效啊!分享给大家,希望有帮助 ...

  2. Mybatis-Plus 多表联查分页

    原文:https://www.jianshu.com/p/759b6430ed5b 分析 使用的工程,依旧是 spring-boot,关于分页,官网给出了一个单表的 demo,其实多表分页实现原理相同 ...

  3. Odoo(OpenERP)配置文件openerp-server.conf详解

    原文地址:http://blog.csdn.net/wangnan537/article/details/42283465 [options] ; addons模块的查找路径 addons_path ...

  4. 解决ADB server didn't ACK问题

    ADB server didn't ACK | 浏览:7400 | 更新:2013-08-11 21:19 1 2 3 4 5 6 7 分步阅读 当我们通过eclipse开发Android应用时,会连 ...

  5. 每天一个linux命令-用户之间切换

    怎么从root用户切换到普通用户 su是在用户间切换,可以是从普通用户切换到root用户,也可以是从root用户切换到普通用户.如果当前是root用户,那么切换成普通用户test用以下命令:su - ...

  6. tomcat配置jdbc

    server.xml下<GlobalNamingResources> <Resource name="jdbc/Huobanplus"               ...

  7. 自定义的圆形ProgressBar

        之前已经详细讲解过自定义控件的使用方式了.这里我单独把定以好的控件列出来. 之前定义的各式各样的ProgressBar http://www.cnblogs.com/tianzhijiexia ...

  8. Eclipse设置方法模板

    我们在项目中进行代码书写的时候,对个人所写的代码打上个人的标签的话,这样便于后期代码的维护跟踪,好处多多. 设置注释模板的入口: Window->Preference->Java-> ...

  9. Statistical Artifact (error)

    In natural science and signal processing, an artifact is any error in the perception or representati ...

  10. [转]hive metadata 存mysql 注释中文乱码的有关

    FROM : http://blog.csdn.net/tswisdom/article/details/41444287 hive metadata 存mysql 注释中文乱码的问题 hive me ...