Java--简单的Spring AOP配置以及AOP事物管理,JDK/GCLib动态代理
一、看一下简单的通过XML的AOP配置
1.首先创建一个简单的Student类
public class Student {
private Integer age;
private String name; public void setAge(Integer age) {
this.age = age;
} public Integer getAge() {
System.out.println("Age : " + age);
return age;
} public void setName(String name) {
this.name = name;
} public String getName() {
System.out.println("Name : " + name);
return name;
} public void printThrowException() {
System.out.println("Exception raised");
throw new IllegalArgumentException();
}
}
2.创建一个简单的aspect切面class
public class Logging {/**
* This is the method which I would like to execute
* before a selected method execution.
*/public void beforeAdvice() {
System.out.println("Going to setup student profile.");
} /**
* This is the method which I would like to execute
* after a selected method execution.
*/public void afterAdvice() {
System.out.println("Student profile has been setup.");
} /**
* This is the method which I would like to execute
* when any method returns.
*/
public void afterReturningAdvice(Object retVal) {
System.out.println("Returning:" + retVal.toString());
} /**
* This is the method which I would like to execute
* if there is an exception raised.
*/
public void AfterThrowingAdvice(IllegalArgumentException ex) {
System.out.println("There has been an exception: " + ex.toString());
}
}
3.SpringAOP.xml配置
<bean id="student" class="com.seeyon.SpringBean.aop.Student" p:name="yangyu" p:age="27"></bean>
<bean id="logging" class="com.seeyon.SpringBean.aop.Logging"></bean> <!--XML方式配置Spring AOP-->
<aop:config>
<aop:aspect id="log" ref="logging"> 【切面class】
<aop:pointcut id="studentMethod" expression="execution(* com.seeyon.SpringBean.aop.Student.get*(..))"/> 【切点】
<aop:before pointcut-ref="studentMethod" method="beforeAdvice"/> 【方法执行之前触发切面class的beforeAdvice方法】
<aop:after pointcut-ref="studentMethod" method="afterAdvice"/> 【方法执行之后触发切面class的afterAdvice方法】
</aop:aspect>
</aop:config>
分析一下这个execution(* com.seeyon.SpringBean.aop.Student.get*(..))切点表达式:
(1)第一个*代表方法的返回值是任意的
(2)get*代表以get开头的所有方法
(3)(..)代表方法的参数是任意个数
4.main方法
public class test {
public static void main(String[] args) {
ApplicationContext context =
new ClassPathXmlApplicationContext("SpringAop.xml");
Student student = (Student) context.getBean("student");
student.getName();
// student.getAge();
// student.printThrowException();
}
}
5.输出结果
Going to setup student profile.
Name : yangyu
Student profile has been setup.
二、Spring AOP注解的使用。
1.首先创建一个简单的Student类(同一.1中,请看上面
Java--简单的Spring AOP配置以及AOP事物管理,JDK/GCLib动态代理的更多相关文章
- Java基础---Java---基础加强---类加载器、委托机制、AOP、 动态代理技术、让动态生成的类成为目标类的代理、实现Spring可配置的AOP框架
类加载器 Java虚拟机中可以安装多个类加载器,系统默认三个主要类加载器,每个类负责加载特定位置的类:BootStrap,ExtClassLoader,AppClassLoader 类加载器也是Jav ...
- 简单理解Spring之IOC和AOP及代码示例
Spring是一个开源框架,主要实现两件事,IOC(控制反转)和AOP(面向切面编程). IOC 控制反转,也可以称为依赖倒置. 所谓依赖,从程序的角度看,就是比如A要调用B的方法,那么A就依赖于B, ...
- Spring AOP高级——源码实现(1)动态代理技术
在正式进入Spring AOP的源码实现前,我们需要准备一定的基础也就是面向切面编程的核心——动态代理. 动态代理实际上也是一种结构型的设计模式,JDK中已经为我们准备好了这种设计模式,不过这种JDK ...
- 基于SpringBoot实现AOP+jdk/CGlib动态代理详解
动态代理是一种设计模式.在Spring中,有俩种方式可以实现动态代理--JDK动态代理和CGLIB动态代理. JDK动态代理 首先定义一个人的接口: public interface Person { ...
- spring boot配置mybatis和事务管理
spring boot配置mybatis和事务管理 一.spring boot与mybatis的配置 1.首先,spring boot 配置mybatis需要的全部依赖如下: <!-- Spri ...
- 动态代理:JDK原生动态代理(Java Proxy)和CGLIB动态代理原理+附静态态代理
本文只是对原文的梳理总结,以及自行理解.自己总结的比较简单,而且不深入,不如直接看原文.不过自己梳理一遍更有助于理解. 详细可参考原文:http://www.cnblogs.com/Carpenter ...
- JAVA JDK的动态代理反射实现
动态代理类使用到了一个接口InvocationHandler和一个代理类Proxy ,这两个类配合使用实现了动态代理的功能. 什么是动态代理呢? 普通代理类是指: 给每个具体类写一个代理类,以后要使 ...
- java框架之Spring(3)-JDBC模板使用&事务管理
下面内容使用到的 jar 包下载 JDBC模板使用 入门 1.导包,如要导入 Spring 的基本开发包.数据库驱动包.Spring 提供的 JDBC 模板包,如下: 2.测试: @Test publ ...
- Spring学习之声明式事物管理
public List<Student> selectStudent() { Student s = new Student(); s.setName("zhengbin&quo ...
随机推荐
- Atitit oodbms的查询,面向对象的sql查询jpa jpql hql
Atitit oodbms的查询,面向对象的sql查询jpa jpql hql 1.1. 标准API历史1 1.2. JPA定义了独特的JPQL(Java Persistence Query Lang ...
- salesforce 零基础学习(十七)Trigger用法
看本篇之前可以相应阅读以下Trigger相关文章: 1.https://developer.salesforce.com/page/Trigger_Frameworks_and_Apex_Trigge ...
- Java EE开发平台随手记2——Mybatis扩展1
今天来记录一下对Mybatis的扩展,版本是3.3.0,是和Spring集成使用,mybatis-spring集成包的版本是1.2.3,如果使用maven,如下配置: <properties&g ...
- Python数据类型之“序列概述与基本序列类型(Basic Sequences)”
序列是指有序的队列,重点在"有序". 一.Python中序列的分类 Python中的序列主要以下几种类型: 3种基本序列类型(Basic Sequence Types):list. ...
- ASP.NET MVC TagBuilder使用
ASP.NET MVC在需要进行新建HTML辅助方法时,可以来使用TagBuilder类. TagBuilder类常用方法: 方法名称 说明 AddCssClass() 可在卷标中添加一个新的Clas ...
- docker快速入门+搭建javaweb环境
一.windows安装 不要安装旧的 boot2docker包,直接安装 DockerToolbox. 一路next,安装完成以后 试用 1.以管理员身份运行 docker quickstart te ...
- Android切换动画之ViewPager
有过开发经验的程序员都知道这个效果,就是当我们第一次安装一个软件时有一个使用说明的图片切换效果,他是如何实现的呢?今天我们就一起学习一下吧,难度系数1.0,就是只要你仔细分析,都可以学会.废话不多说, ...
- (转)JS模块化编程之AMD规范
模块的规范 原文地址 先想一想,为什么模块很重要? 因为有了模块,我们就可以更方便地使用别人的代码,想要什么功能,就加载什么模块. 但是,这样做有一个前提,那就是大家必须以同样的方式编写模块,否则你有 ...
- Deep learning:四十四(Pylearn2中的Quick-start例子)
前言: 听说Pylearn2是个蛮适合搞深度学习的库,它建立在Theano之上,支持GPU(估计得以后工作才玩这个,现在木有这个硬件条件)运算,由DL大牛Bengio小组弄出来的,再加上Pylearn ...
- Fatal signal 11 (SIGSEGV) at 0x00000000 (code=1), thread 1755 (CrBrowserMain)问题