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 团队提供的全新框架,其设计目的是 ...
随机推荐
- 【翻译】自定义 UIViewController Transitions
原文地址:http://www.shinobicontrols.com/blog/posts/2013/10/03/ios7-day-by-day-day-10-custom-uiviewcontro ...
- CSS的伪元素和伪类
css3为了区分伪类和伪元素,伪元素采用双冒号写法. 常见伪类—— :hover, :active, :visited, :focus. 常见伪元素——::first-letter,::first-l ...
- HDU 4289 Control (最小割 拆点)
Control Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Su ...
- [转载]深入理解java多态性
FROM:http://blog.csdn.net/thinkGhoster/article/details/2307001 昨天看到一个关于多态性的帖子,参考了回帖者的理解,加入了一些自己的看法,整 ...
- 【业务自动化】iTop,全面支持ITIL流程的一款ITSM工具
iTop产品针对的主要应用场景为:内部IT支持.IT外包管理.数据中心运维管理和企业IT资产管理.常青管理从绿象认证产品中选取了iTop作为主要推荐产品,本类别的绿象认证产品还包括:OTRS和RT3等 ...
- chrome护眼模式
chrome护眼模式 使用stylish插件: 学习:https://jingyan.baidu.com/article/b907e627f74df146e6891c67.html 插件下载:http ...
- 一种让UITableView的数据从下往上增长的方式
遇到问题 一般来说tableview的数据都是从上往下增长,如下图所示(先是aaa出现在表格列表的最顶部,然后bbb出现在aaa的下面,以此类推) 但是如果我们想反向这个过程该怎么做呢?如下图所示(先 ...
- Instant Messaging for OpenERP v7
This module is based on the socket.io real time implementation. It inherit Of the modules web_longpo ...
- 谷歌Cartographer学习 -快速安装测试
参考资料:https://www.cnblogs.com/hitcm/p/5939507.html PC下面进行安装: 遇到的问题如下 1.首先安装ceres solver 在编译的时候,如果是低配的 ...
- css处理超出文本截断问题的两种情况(多行或者单行)
1.非多行的简单处理方式: css代码: .words{ width:400px; overflow:hidden; /*超过部分不显示*/ text-overflow:ellipsis; /*超过部 ...