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 ...
随机推荐
- tabs左右滚动
$(function () { //IdivLeft小于0,说明左边还有菜单,菜单总数大于8 //IdivLeft等于0,说明菜单总数小于8 //IdivLeft大于0,说明右边还有菜单,菜单总数大于 ...
- java中IO流相关知识点
(一) 下边使用outputStream字节输出流进行写操作 package zdbIO;import java.io.File;import java.io.FileNotFoundExceptio ...
- Zend Studio导入ThinkPHP工程
1.一般来说,thinkPHP文件工程(简称php工程)要部署到www下面,那么可以先复制一份php工程到非www文件夹的地方(如桌面): 2.打开zend studio右键,File-New-Loc ...
- 查看SSIS Package 部署的历史记录
1,通过Integration Services Catalogs来查看 打开SSISDB->Projects,查看指定project的version history,这种方式查看Deploye ...
- 接触LLBL Gen Pro 对象关系映射框架后 前途变的一片光明
时间回到2010年,那时候还是熟悉代码生成+基础框架这种模式,基本的开发思路是通过代码生成器生成实体,再生成接口与实现类,最后拖拉控件,写界面数据绑定代码.基本上就是动软代码生成器给出的模式,或是微软 ...
- KendoUI系列:PanelBar
<link href="@Url.Content("~/Content/kendo/2014.1.318/kendo.common.min.css")" ...
- px 与 dp, sp换算公式?
PPI = Pixels per inch,每英寸上的像素数,即 "像素密度" xhdpi: 2.0 hdpi: 1.5 mdpi: 1.0 (baseline) ldpi: 0. ...
- poj 2777(线段树的节点更新策略)
/* 之前的思想是用回溯的方式进行颜色的更新的!如果用回溯的方法的话,就是将每一个节点的颜色都要更新 通过子节点的颜色情况来判断父节点的颜色情况 !这就是TLE的原因! 后来想一想没有必要 !加入[a ...
- 使用ExifInterface设置Datetime发生的问题
最近在弄一个Android小程序,需要把图像的生成时间设置到Exif的Datetime,用ExifInterface.setAttribute(ExifInterface.TAG_DATETIME,& ...
- Windows Azure Virtual Network (6) 设置Azure Virtual Machine固定公网IP (Virtual IP Address, VIP) (1)
<Windows Azure Platform 系列文章目录> 注意:本文介绍的是Global Azure (http://www.windowsazure.com),如果你使用的是由世纪 ...