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 手动,半自动,全自动的更多相关文章

  1. 继电器是如何成为CPU的(1)

    继电器是如何成为CPU的(1) ——<穿越计算机的迷雾>整理和总结 究竟是如何设计的电路,具有计算和控制的智力? 这一点也不高深.本系列文章从初中学的最简单的电路图说起,看看能不能从最初的 ...

  2. 继电器是如何成为CPU的

    阅读目录(Content) 从电池.开关和继电器开始 用继电器做个与门 用继电器做个或门 用继电器做个异或门 做一些看起来可用的东西 小小约定 振荡器 加法器 寄存器 R-S触发器 D触发器 上升沿D ...

  3. 继电器是如何成为CPU的(1)【转】

    转自:http://www.cnblogs.com/bitzhuwei/p/from_relay_to_tiny_CPU.html 阅读目录(Content) 从电池.开关和继电器开始 用继电器做个与 ...

  4. 8.AOP全自动

    CGLIB字节码增强 l没有接口,只有实现类. 采用字节码增强框架 cglib,在运行时 创建目标类的子类,从而对目标类进行增强. 导入jar包: 自己导包(了解): 核心:hibernate-dis ...

  5. AOP切面编程

    1.JDK动态代理方式实现 public static UserService createService(){ //目标类 final UserService userService = new U ...

  6. Spring AOP编程(二)-AOP实现的三种方式

    AOP的实现有三种方式: l         aop底层将采用代理机制进行实现. l         接口 + 实现类 :spring采用 jdk 的动态代理Proxy. l         实现类: ...

  7. Spring day02笔记

    spring day01回顾 编写流程(基于xml) 1.导入jar包:4+1 --> beans/core/context/expression | commons-logging 2.编写目 ...

  8. Spring2

    简介:1.Aop编程.2.AspectJ基于xml文件.3.AspectJ基于注解. 4.JdbcTemplate. 5.配置properties文件 1       AOP 1.1   AOP介绍 ...

  9. Spring 框架(二)

    1AOP 1.1 AOP介绍 1.1.1 什么是AOP l 在软件业,AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译方式和运行期动态代理实现程序功 ...

随机推荐

  1. 当在浏览器输入一个url访问后发生了什么

    首先根据DNS获取该url的ip地址,ip地址的获取可能通过本地缓存,路由缓存等得到. 然后在网络层通过路由选择查找一条可达路径,最后利用tcp/ip协议来进行数据的传输. 其中在传输层将信息添加源端 ...

  2. java基础之 工具类

    一.StringUtils StringUtils.isEmpty(null) && StringUtils.isEmpty(""); // true String ...

  3. mysql 操作杂记

    SHOW VARIABLES LIKE 'character%'; SET character_set_client = utf8; SET character_set_connection = ut ...

  4. 解决Inno Setup制作中文安装包在非中文系统上显示乱码的问题

    尼玛,好几个月没更新了.囧... 目前我司新的客户端开发已经接近尾声,该改的bug已经改完,该重构的地方也都差不多了.视觉效果也已经根据美工的样式改完了.所以,就差制作安装包了.正所谓万事俱备,只欠东 ...

  5. liquibase的使用

    前言 liquibase是一个数据库持续集成插件.独立于数据库存在,oracle,mysql,db2,h2,sql server,postgresql都能使用.它使用配置文件来更新数据库结构,并加入版 ...

  6. clip:rect矩形剪裁

    clip:rect(top right bottom left);依据上-右-下-左的顺序提供自图片左上角为(0,0)坐标计算的四个偏移数值,其中任一数值都可用auto替换. 矩形剪裁 还需要绝对定位 ...

  7. 移动测试主要使用的测试框架,基于python

    1.uiautomator google自己的测试框架,可以跨应用测试, 语言支持java,python也可以在GitHub上找到封装的包,去调用uiautomator. 2.Robotium jav ...

  8. android 透明度

    透明度百分比对应的十六进制: (说明:百分比计算出来会有小数,按照常规的四舍五入处理,详情请往下查看) 百分比:0% HEX: 00 百分比:1% HEX: 30 百分比:2% HEX: 50 百分比 ...

  9. SRS文档

    1什么是用例? 在介始用例方法之前,我们首先来看一下传统的需求表述方式-"软件需求规约"(Software Requirement Specification).传统的软件需求规约 ...

  10. 运行带cocoa pods 的项目,遇到的问题是找不到文件,解决办法

    打开终端,进入项目所在的目录,也就是和Podfile在同一目录下,输入以下命令(由于已经有Podfile,所以不需要再创建Podfile):  pod update 过几秒(也许需要十几秒,取决于你的 ...