AOP 手动,半自动,全自动
dao层
package com.yaorange.dao;
public interface StudentDao {
public void saveStudent();
public void deleteStudent();
}
dao层的实现
package com.yaorange.dao.impl;
import com.yaorange.dao.StudentDao;
public class StudentDaoImpl implements StudentDao{
@Override
public void saveStudent() {
System.out.println("saveStudent");
}
@Override
public void deleteStudent() {
System.out.println("deleteStudent");
}
}
工具层
第一个是工厂制造studentDao
package com.yaorange.utils;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import com.yaorange.dao.StudentDao;
import com.yaorange.dao.impl.StudentDaoImpl;
public class MyFactory {
public static StudentDao createStudentProxy(){
final StudentDao studentDaoImpl = new StudentDaoImpl();
final StudentAspect studentAspect = new StudentAspect();
StudentDao studentDao = (StudentDao) Proxy.newProxyInstance(MyFactory.class.getClassLoader(),
studentDaoImpl.getClass().getInterfaces(),
new InvocationHandler() {
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
studentAspect.before();
Object object = method.invoke(studentDaoImpl, args);
studentAspect.after();
return object;
}
});
return studentDao;
}
}
StudentAspect是学生的一个aspect
package com.yaorange.utils;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
public class StudentAspect implements MethodInterceptor{
//开始事务
public void before(){
System.out.println("开始事务");
}
//提交事务
public void after(){
System.out.println("提交事务");
}
@Override
public Object invoke(MethodInvocation mi) throws Throwable {
before();
Object object = mi.proceed();
after();
return object;
}
}
手动的是不写beans.xml的
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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
">
<!--*****************************半自动 **************************** -->
<!--创建目标类 -->
<bean id="studentDaoImpl" class="com.yaorange.dao.impl.StudentDaoImpl"/>
<!--创建切面类 -->
<bean id="studentAspect" class="com.yaorange.utils.StudentAspect"/>
<!-- 3 创建代理类
* 使用工厂bean FactoryBean ,底层调用 getObject() 返回特殊bean
* ProxyFactoryBean 用于创建代理工厂bean,生成特殊代理对象
interfaces : 确定接口们
通过<array>可以设置多个值
只有一个值时,value=""
target : 确定目标类
interceptorNames : 通知 切面类的名称,类型String[],如果设置一个值 value=""
optimize :强制使用cglib
<property name="optimize" value="true"></property>
底层机制
如果目标类有接口,采用jdk动态代理
如果没有接口,采用cglib 字节码增强
如果声明 optimize = true ,无论是否有接口,都采用cglib
-->
<!--代理类 -->
<bean id="proxyStudentDao" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="optimize" value="true"/>
<property name="interfaces" value="com.yaorange.dao.StudentDao"/>
<property name="target" ref="studentDaoImpl"/>
<property name="interceptorNames" value="studentAspect"></property>
</bean>
<!-- **********************全自动 ************************-->
<!-- 目标类 -->
<bean id="studentDaoImpl2" class="com.yaorange.dao.impl.StudentDaoImpl"/>
<!-- *******************切面类************************* -->
<bean id="studentAspect2" class="com.yaorange.utils.StudentAspect"/>
<aop:config>
<!-- *代表匹配 任意包 包里面任意的类 类中任意的方法 .代表方法中的参数 -->
<aop:pointcut expression="excution(* com.yaorange.utils.*.*(..))" id="mypointcut"/>
<aop:advisor advice-ref="studentAspect2" pointcut-ref="mypointcut"/>
</aop:config>
</beans>
最后是我们的测试类
package com.yaorange.test;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.yaorange.dao.StudentDao;
import com.yaorange.utils.MyFactory;
public class AOPTest {
//手动
@Test
public void test1(){
StudentDao studentDao = MyFactory.createStudentProxy();
studentDao.saveStudent();
}
//半自动
@Test
public void test2(){
ClassPathXmlApplicationContext cpx = new ClassPathXmlApplicationContext("beans.xml");
StudentDao studentDao = (StudentDao) cpx.getBean("proxyStudentDao");
studentDao.saveStudent();
studentDao.deleteStudent();
}
//全自动
@Test
public void test3(){
StudentDao studentDao = MyFactory.createStudentProxy();
studentDao.saveStudent();
}
}
AOP 手动,半自动,全自动的更多相关文章
- 继电器是如何成为CPU的(1)
继电器是如何成为CPU的(1) ——<穿越计算机的迷雾>整理和总结 究竟是如何设计的电路,具有计算和控制的智力? 这一点也不高深.本系列文章从初中学的最简单的电路图说起,看看能不能从最初的 ...
- 继电器是如何成为CPU的
阅读目录(Content) 从电池.开关和继电器开始 用继电器做个与门 用继电器做个或门 用继电器做个异或门 做一些看起来可用的东西 小小约定 振荡器 加法器 寄存器 R-S触发器 D触发器 上升沿D ...
- 继电器是如何成为CPU的(1)【转】
转自:http://www.cnblogs.com/bitzhuwei/p/from_relay_to_tiny_CPU.html 阅读目录(Content) 从电池.开关和继电器开始 用继电器做个与 ...
- 8.AOP全自动
CGLIB字节码增强 l没有接口,只有实现类. 采用字节码增强框架 cglib,在运行时 创建目标类的子类,从而对目标类进行增强. 导入jar包: 自己导包(了解): 核心:hibernate-dis ...
- AOP切面编程
1.JDK动态代理方式实现 public static UserService createService(){ //目标类 final UserService userService = new U ...
- Spring AOP编程(二)-AOP实现的三种方式
AOP的实现有三种方式: l aop底层将采用代理机制进行实现. l 接口 + 实现类 :spring采用 jdk 的动态代理Proxy. l 实现类: ...
- Spring day02笔记
spring day01回顾 编写流程(基于xml) 1.导入jar包:4+1 --> beans/core/context/expression | commons-logging 2.编写目 ...
- Spring2
简介:1.Aop编程.2.AspectJ基于xml文件.3.AspectJ基于注解. 4.JdbcTemplate. 5.配置properties文件 1 AOP 1.1 AOP介绍 ...
- Spring 框架(二)
1AOP 1.1 AOP介绍 1.1.1 什么是AOP l 在软件业,AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译方式和运行期动态代理实现程序功 ...
随机推荐
- Haskell Platform (windows)
一.下载地址:https://www.haskell.org/platform/windows.html Haskell Platform 整合了 Glasgow Haskell Compiler,W ...
- C# 大小写转换,方便index of
ToUpper:小写转大写ToLower:大写转小写 例: string str=120cm*150g/m2;从中取出120和150,但是又要规避大小写问题,这时候就需要将str转换为大写,然后ind ...
- Scala编程--基本类型和操作
如果你熟悉Java,你会很开心地发现Java基本类型和操作符在Scala里有同样的意思.然而即使你是一位资深Java开发者,这里也仍然有一些有趣的差别使得本章值得一读.因为本章提到的一些Scala的方 ...
- jquery 显示弹出层可利用动画效果
1 show()方法和hide()方法 $("selector").show() 从display:none还原元素默认或已设置的display属性$("selecto ...
- 用AE (Adobe After Effects) 处理视频
这话要从年会说起,我们组的年会节目需要一段场外亲友团的评价视频,于是我们就靠在公司门口的logo前拍了这么一段.但是呢,有很多的不理想: 画面抖动 杂音好烦(中午吃饭时拍的,还有好大的微波炉好了的声音 ...
- Dapper学习笔记(1)-开始
Dapper是一款开源的轻量级ORM工具,源代码下载地址为https://github.com/StackExchange/dapper-dot-net,其具有以下特点: 1.Dapper是一个轻型的 ...
- break into Ubuntu System
This morning, I got a spare machine from of of the labmates. The OS is ubuntu 12.04. I could not log ...
- Yosemite系统怎么录制 iOS8设备屏幕
我一年前一直想要的一个功能,发布时很想用.一直没找到 ,很巧的是今天被测试发现了. 感谢CCTV.自己在这里也记录下: 你好! 在 OS X Yosemite 系统中,QuickTime 支持 ...
- GridView不能添加头布局,并且scrollView与GridView冲突导致一些页面无法融合
此贴为标记贴 方便下次使用 在项目需求中原本是用ScrollView来进行整个页面的滑动,ScrollView里面包含的有图片轮播,文字轮播,与2列GridView的item 问题 使用原生的Grid ...
- Spring3.0 与 MyBatis框架 整合小实例
本文将在Eclipse开发环境下,采用Spring MVC + Spring + MyBatis + Maven + Log4J 框架搭建一个Java web 项目. 1. 环境准备: 1.1 创建数 ...