Spring入门之AOP篇
一、准备工作
下载复制或配置JAR包。图方便,我将下载的SPRING包都加上去了。另外还需要aspectj的两个包,见下图
二、主要代码
我在SRC目录下面,建了com.spring.aop的包,然后在下面写的代码。
新建三个类文件Book.java Mybook.java和TestAop.java,主要测试AOP 的前后加载和环绕加载(横向抽取)
Book.java代码:
package com.spring.aop; public class Book {
public void add() {
System.out.println("Add.............");
}
}
Mybook.java代码:
package com.spring.aop; import org.aspectj.lang.ProceedingJoinPoint; public class Mybook {
public void aopAdd() { System.out.println("前置!!!");
} public void afterAdd() { System.out.println("后置!");
} public void around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable { System.out.println("环绕前!");
proceedingJoinPoint.proceed(); System.out.println("环绕后!");
} }
TestAop.java
package com.spring.aop; import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class TestAop {
@Test
public void testSevice() {
ApplicationContext context = new ClassPathXmlApplicationContext("beans3.xml");
Book book = (Book) context.getBean("book");
book.add();
} }
TestAop.java是测试运行的文件,直接在这个文件中,点鼠标右键,选Run as ------Junit Test
三、spring配置文件beans.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: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/aop
http://www.springframework.org/schema/aop/spring-aop.xsd " >
<!-- 1、配置对象 -->
<bean id = "book" class="com.spring.aop.Book"></bean>
<bean id = "mybook" class="com.spring.aop.Mybook"></bean>
<!-- 2、配置AOP操作 -->
<aop:config>
<!-- 2.1配置切入点 -->
<aop:pointcut expression="execution(* com.spring.aop.Book.*(..))" id ="pointcut1" />
<!-- 2.2配置切面 把增强用到方法上面-->
<aop:aspect ref="mybook">
<!--配置增强类型,method:增加类里使用哪个方法作为前置 -->
<aop:before method="aopAdd" pointcut-ref="pointcut1"/>
<aop:after-returning method="afterAdd" pointcut-ref="pointcut1"/>
<aop:around method="around" pointcut-ref="pointcut1"/>
</aop:aspect> </aop:config>
</beans>
其中,配置文件中前面的约束要先配好,我以前是从别的配置文件中复制过来的,一直没有学会从下载的包装中找约束。必须要有xmlns:aop="......"内容,否则会报错。
首先配置用到两个类的<bean>
其次,配置AOP的核心:代码在<aop:config>和〈/aop:config>之间。aspect:pointcut是指定切入点的方法,expression="execution(* com.spring.aop.Book.*(..))"中,用*表示Book类的所有方法都可以被切入。id 是切入点名称,这个在后面的“切面和增强”配置中要用到,指明增强要用在什么地方;
四、运行结果
说明先执行“前置”aop:before指定的方法,再执行环绕aop:around指定的前方法。第三步执行环绕的后方法后置方法,最后执行后置方法 ,aspect:after-returning指定的方法 ;
Spring入门之AOP篇的更多相关文章
- Spring入门4.AOP配置深入
Spring入门4.AOP配置深入 代码下载 链接: http://pan.baidu.com/s/11mYEO 密码: x7wa 前言: 之前学习AOP中的一些概念,包括连接点.切入点(pointc ...
- Spring入门3.AOP编程
Spring入门3.AOP编程 代码下载: 链接: http://pan.baidu.com/s/11mYEO 密码: x7wa 前言: 前面学习的知识是Spring在Java项目中的IoC或DJ,这 ...
- Spring入门介绍-AOP(三)
AOP的概念 AOP是面向切面编程的缩写,它是一种编程的新思想.对我们经常提起的oop(面对对象编程)有一定的联系. AOP和OOP的关系 AOP可以说是oop的某一方便的补充,oop侧重于对静态的属 ...
- Spring入门之AOP实践:@Aspect + @Pointcut + @Before / @Around / @After
零.准备知识 1)AOP相关概念:Aspect.Advice.Join point.Pointcut.Weaving.Target等. ref: https://www.cnblogs.com/zha ...
- 让Spring不再难懂-aop篇
什么是aop AOP(Aspect-OrientedProgramming,面向方面编程),可以说是OOP(Object-Oriented Programing,面向对象编程)的补充和完善.OOP允许 ...
- Spring课程 Spring入门篇 5-1 aop基本概念及特点
概念: 1 什么是aop及实现方式 2 aop的基本概念 3 spring中的aop 1 什么是aop及实现方式 1.1 aop,面向切面编程,比如:唐僧取经需要经过81难,多一难少一难都不行.孙悟空 ...
- Spring入门篇——AOP基本概念
1.什么是AOP及实现方式 什么是AOP AOP:Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术 主要 ...
- Spring入门篇总结:
本文是对慕课网上"搞定SSM开发"路径的系列课程的总结,详细的项目文档和课程总结放在github上了.点击查看 视频传送门:Spring入门篇 该门课程主要从Spring的Bean ...
- Spring Boot 入门之基础篇(一)
原文地址:Spring Boot 入门之基础篇(一) 博客地址:http://www.extlight.com 一.前言 Spring Boot 是由 Pivotal 团队提供的全新框架,其设计目的是 ...
随机推荐
- 使用Vue-cli创建project遇到的坑
环境: win10 / node( v10.2.1) /npm( v5.6.0) 准备: 1.安装node:上node官网下载node版本进行安装 2.安装vue-cli:npm install -- ...
- 关于spring-data-jpa的排序问题
本测试基于springBoot框架实现. pom.xml文件: <project xmlns="http://maven.apache.org/POM/4.0.0" xmln ...
- Oracle Process Cloud流程云实践
本篇适合对Oracle Unified BPM有一定基础的人参考,本篇也是参考Oracle A-team John Featherly的文章进行的实践. 1. 流程创建 打开cloud.o ...
- Python图像处理(16):图像金字塔
快乐虾 http://blog.csdn.net/lights_joy/ 欢迎转载,但请保留作者信息 图像金字塔是图像中多尺度表达的一种,最初用于机器视觉和图像压缩.一幅图像的金字塔是一系列以金字塔形 ...
- JavaScript面向对象总结
对象(Object)应该算是js中最为重要的部分,也是js中非常难懂晦涩的一部分.更是面试以及框架设计中各出没.本文章,主要参考JavaScript红宝书(JavaScript高级程序设计 第六章)以 ...
- scrapy-splash抓取动态数据例子十五
一.介绍 本例子用scrapy-splash爬取电视之家(http://www.tvhome.com/news/)网站的资讯信息,输入给定关键字抓取微信资讯信息. 给定关键字:数字:融合:电视 抓取信 ...
- vmware已经全面支持open-vm-tools
以后不用再为vmware vm单独安装vmware-tools了,vmware已经全面支持open-vm-tools, 今天突然发现安装vmware-tools时出现deprecated提示,原来vm ...
- [HTML5] Render Hello World Text with Custom Elements
Custom elements are fun technology. In this video, you will learn how to set one up and running in l ...
- Node.js meitulu图片批量下载爬虫1.04版
//====================================================== // https://www.meitulu.com图片批量下载Node.js爬虫1. ...
- vue - 安装脚手架
最后不得不屈服与虚拟DOM和框架,太方便了... 1.首先安装node:点击进入官网. 2. 安装后检测 3. 安装yarn(至于为嘛,速度呗) yarn官网,npm转yarn. 3.1 window ...