一。aop编程思想
  1.面向切面,就是能够不动源码的情况下,从横切面切入新的代码功能。
  2.实现原理是动态代理
  动态代理的步骤
    a。写生产厂家,实现接口,代理只能代理接口
    b。动态代理类实现InvocationHandler接口,实现bind方法来绑定厂家,重写invoke方法
    c。消费者找动态代理类来获取商品消费
  3.aop的作用:是将系统级别的重复的操作统一实现,节省代码,扩展性更好
    例如:系统日志,系统非法访问,系统权限,系统事务

二。spring怎么实现AOP
  1。在spring的配置文件中加入aop的schema,并且打开aop的注解开关

    <!-- 开启aop的注解模式 -->
    <aop:aspectj-autoproxy></aop:aspectj-autoproxy>

  2。pom.xml中导入依赖spring-aop,spring-aspects

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-aspects</artifactId>
      <version>4.2.4.RELEASE</version>
    </dependency>

  3。写一个aop的切面类,加上注解@comptent @Aspect
  4。在切面类中写切入的方法,在放上加上注解@Before @After @Around

实例1:动态代理

手机工厂生产的手机有发短信、打电话等功能,如果通过代理拿货,可以赠送自拍杆、免费贴膜等。

IPhoneFactory.java
package com.proxy;

public interface IPhoneFactory {

    void mobile();

    void mobile2();

    void mobile3();
}
PhoneFactory.java
package com.proxy;

public class PhoneFactory implements IPhoneFactory {
public void mobile(){
System.out.println("我是iphone XXX");
} public void mobile2(){
System.out.println("我可以打电话");
} public void mobile3(){
System.out.println("我可以发信息");
}
}

  代理类 MyProxy.java

package com.proxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy; public class MyProxy implements InvocationHandler { private Object obj;// 要代理的目标产品,用object表示可以代理任何类型 public Object bind(Object o) {
this.obj = o;
return Proxy.newProxyInstance(obj.getClass().getClassLoader(), obj.getClass().getInterfaces(), this);
} @Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
// TODO Auto-generated method stub
System.out.println("给手机贴膜");
Object result = method.invoke(obj, new Object[]{});//是厂家的产品的原始功能
System.out.println("赠送自拍杆");
return result;
} }

  调用代码 Test.java

package com.proxy;

public class Test {
public static void main(String[] args) {
MyProxy mp = new MyProxy();
IPhoneFactory iphone = (IPhoneFactory)mp.bind(new PhoneFactory());
iphone.mobile3();
}
}

实例2:aop面向切面

IStudentDAO.java
package com.aop;

public interface IStudentDAO {

    void addStudent();

    void delStudent();

    void updateStudent();

    void queryStudent();

}
StudentDAO.java
package com.aop;

import org.springframework.stereotype.Repository;

@Repository("stuDAO")
public class StudentDAO implements IStudentDAO {
public void addStudent(){
System.out.println("新增学员");
}
public void delStudent(){
int i=1/0;
System.out.println("删除学员");
} public void updateStudent(){
System.out.println("修改学员");
} public void queryStudent(){
System.out.println("查询学员");
}
}

 切面类 

TranscationAOP.java
package com.aop;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component; @Aspect//注册为一个切面
@Component
public class TranscationAOP {
/**
* 表示在方法执行前的切入
* 第一个*表示方法的返回类型
* 第二个*表示该包下所有的类
* 第三个*表示类中所有的方法
* (..)表示任意参数类型
*/
// @Before("execution(* com.aop.*.*(..))")
// public void beforeDAO(){
// System.out.println("开启事务");
// } // @After("execution(* com.aop.*.*(..))")
// public void afterDAO(){
// System.out.println("事务提交");
// } /**
* 前后切入
* @param pjp 切入点
*/
@Around("execution(* com.aop.*.*(..))")
public void aroundDAO(ProceedingJoinPoint pjp){
System.out.println("开启事务");
try {
pjp.proceed();//原方法正在执行
} catch (Throwable e) {
System.out.println("业务发生异常"+e.getMessage());
System.out.println("事务回滚");
e.printStackTrace();
return;
}
System.out.println("事务提交");
}
}

调用代码 

Test.java
package com.aop;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class Test {
public static void main(String[] args) {
BeanFactory bf = new ClassPathXmlApplicationContext("beans.xml");
IStudentDAO stuDAO = (IStudentDAO) bf.getBean("stuDAO");
stuDAO.delStudent();
}
}

 配置文件 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:context="http://www.springframework.org/schema/context"
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/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 打开spring的注解功能 -->
<context:annotation-config></context:annotation-config>
<!-- 告诉spring到哪些包下去扫描bean对象 -->
<context:component-scan base-package="com"></context:component-scan>
<!-- 开启aop的注解模式 -->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy> <!-- <bean id="bt" class="com.bean.BeanTest"></bean> --> <!-- <bean id="stuDAO" class="com.dao.StudentDAO"></bean> -->
<!-- <bean id="stuMysqlDAO" class="com.dao.StudentMysqlDAO"></bean> --> <!-- <bean id="stuSer" class="com.service.StudentService"> -->
<!-- <property name="stuDAO"> -->
<!-- <ref bean="stuMysqlDAO" /> -->
<!-- </property> -->
<!-- </bean> --> <!-- <bean id="stuControl" class="com.control.StudentControl"> -->
<!-- <property name="stuSer"> -->
<!-- <ref bean="stuSer" /> -->
<!-- </property> -->
<!-- </bean> -->
</beans>

  依赖 pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>spring06</groupId>
<artifactId>spring06</artifactId>
<version>0.0.1-SNAPSHOT</version>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.3.13.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.13.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>4.2.4.RELEASE</version>
</dependency>
</dependencies>
</project>

二)Spring AOP编程思想与动态代理的更多相关文章

  1. Spring AOP的作用,动态代理模式

    AOP即面向切面编程.AOP是基于代理模式的. 代理模式: 当我们需要修改一个类,在类中加入代码时,为了不破坏这个类的封装性.可以使用代理模式,建立一个代理类. 比如:修改需求,在调用UserCont ...

  2. spring aop的前奏,动态代理 (5)

    目录 一.先看一个计算器的抽取和实现 二.使用动态代理解决以上问题. 1 设计原理 2 代码实现 2.1 接口代码 2.2 实现接口的代码 2.3 测试代码 2.3 创建动态代理类 2.4 动态代理类 ...

  3. spring aop,静态及动态代理例子

    @Aspect@Componentpublic class AopText { @Pointcut("execution(public * com.llf.service.*Service. ...

  4. Spring笔记06(Spring AOP的底层实现动态代理)

    1.代理模式readMe: 代理设计模式: 是java中常用的设计模式! 特点: .委托类和代理类有相同的接口或者共同的父类! .代理类为委托类负责处理消息,并将消息转发给委托类! .委托类和代理类对 ...

  5. Spring @Trasactionl 失效, JDK,CGLIB动态代理

    @Transaction:  http://blog.csdn.net/bao19901210/article/details/41724355 Spring上下文:  http://blog.csd ...

  6. 【Spring Framework】Spring入门教程(五)AOP思想和动态代理

    本文主要讲解内容如下: Spring的核心之一 - AOP思想 (1) 代理模式- 动态代理 ① JDK的动态代理 (Java官方) ② CGLIB 第三方代理 AOP概述 什么是AOP(面向切面编程 ...

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

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

  8. 浅析Spring中AOP的实现原理——动态代理

    一.前言   最近在复习Spring的相关内容,刚刚大致研究了一下Spring中,AOP的实现原理.这篇博客就来简单地聊一聊Spring的AOP是如何实现的,并通过一个简单的测试用例来验证一下.废话不 ...

  9. Spring AOP 源码分析 - 创建代理对象

    1.简介 在上一篇文章中,我分析了 Spring 是如何为目标 bean 筛选合适的通知器的.现在通知器选好了,接下来就要通过代理的方式将通知器(Advisor)所持有的通知(Advice)织入到 b ...

随机推荐

  1. Storm入门(六)深入理解可靠性机制

    转自http://blog.csdn.net/zhangzhebjut/article/details/38467145 一 可靠性简介 Storm的可靠性是指Storm会告知用户每一个消息单元是否在 ...

  2. 自己动手用原生实现 bind/call/apply

    大家好!!!注册一年多的第一篇博客. 自我介绍: 本人非计算机专业出身,转行进入前端半年时间,写的东西可能观赏性不强,一起进步吧道友们... 接下来的一段时间, 我都会不定期整理自己理解的js知识点, ...

  3. 来一波C#发送邮件

    1.所用工具和资源:VS2012   在.NET Frameword类库中提供SmtpClient类(System.NET.Mail) 2.运行截图 3.具体代码实现如下: using System; ...

  4. 网卡也能虚拟化?网卡虚拟化技术 macvlan 详解

    本文首发于我的公众号 Linux云计算网络(id: cloud_dev),专注于干货分享,号内有 10T 书籍和视频资源,后台回复「1024」即可领取,欢迎大家关注,二维码文末可以扫. 01 macv ...

  5. 从壹开始前后端分离【 .NET Core2.0 +Vue2.0 】框架之十一 || AOP自定义筛选,Redis入门 11.1

    代码已上传Github+Gitee,文末有地址 书说上文<从壹开始前后端分离[ .NET Core2.0 Api + Vue 2.0 + AOP + 分布式]框架之十 || AOP面向切面编程浅 ...

  6. jquery判空 string类型的日期比较大小

    jquery 判空 if(value.length<=0){  alert("kongzhi"); } jquery string类型的日期比较大小 var startTim ...

  7. 《IDEO,设计改变一切》(Change By Design)- 读书笔记

    一.关于IDEO与设计思维 IDEO是一家世界顶级创意公司,而作者蒂姆布朗是IDEO的CEO.当然,在未阅读本书之前,我都是不知道的,也不会主动去了解IDEO和蒂姆布朗的.那么,我为什么要去读这样一本 ...

  8. LitepalNewDemo【开源数据库ORM框架-LitePal2.0.0版本的使用】

    版权声明:本文为HaiyuKing原创文章,转载请注明出处! 前言 本Demo使用的是LitePal2.0.0版本,对于旧项目如何升级到2.0.0版本,请阅读<赶快使用LitePal 2.0版本 ...

  9. 用消息队列和socket实现聊天系统

    前言:最近在学进程间通信,所以做了一个小项目练习一下.主要用消息队列和socket(UDP)实现这个系统,并数据库存储数据,对C语言操作不熟悉的可以参照我的这篇博客:https://www.cnblo ...

  10. Shell从入门到精通进阶之四:流程控制

    流程控制是改变程序运行顺序的指令. 4.1 if语句 4.1.1 单分支 if 条件表达式; then 命令 fi 示例: #!/bin/bash N=10 if [ $N -gt 5 ]; then ...