一。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. JNI实战(三):JNI 数据类型映射

    在JNI实战(二):Java 调用 C 我们了解了JNI的静态注册和动态注册.也知道我们应该使用动态注册来进行JNI函数与Java方法之间的映射. 示例的映射表的数组为如下: static JNINa ...

  2. ASP.NET MVC默认配置如有跳转到指定的Area区域中的对应程序中

    今天在搭建一个基于MVC的项目,因为项目涉及到了手机和pc端,为了方便和减少二者之间的耦合我在区域(Areas)中建立了两个 程序空间,那么问题来了我想让程序默认跳转到我所指定的areas中的对应项目 ...

  3. vue.js window.removeEventListener 移除

    vue项目中的小坑记录下,想要移除window的addEventListener,需要把后面的function挂在到this上,removeEventListener 和 addEventListen ...

  4. 从壹开始前后端分离 [ vue + .netcore 补程 ] 三十一║ Nuxt终篇:基于Vuex的权限验证探究

    缘起 哈喽大家好,今天周四啦,楼主明天要正式放假了,这里先祝大家节日快乐咯,希望在家里能继续研究点儿东西吧,今天呢是 nuxt 的最后一篇,主要是对权限登录进行研究,这一块咱们之前在说第一个项目的时候 ...

  5. .NET Core微服务之基于Ocelot+IdentityServer实现统一验证与授权

    Tip: 此篇已加入.NET Core微服务基础系列文章索引 一.案例结构总览 这里,假设我们有两个客户端(一个Web网站,一个移动App),他们要使用系统,需要通过API网关(这里API网关始终作为 ...

  6. SpringBoot整合Swagger2,再也不用维护接口文档了!

    前后端分离后,维护接口文档基本上是必不可少的工作.一个理想的状态是设计好后,接口文档发给前端和后端,大伙按照既定的规则各自开发,开发好了对接上了就可以上线了.当然这是一种非常理想的状态,实际开发中却很 ...

  7. NavigationViewDemo【和DrawerLayout搭配使用实现侧滑导航视图界面】

    版权声明:本文为HaiyuKing原创文章,转载请注明出处! 前言 主要记录NavigationView的使用,而一般情况下NavigationView是和DrawerLayout搭配使用的,还有To ...

  8. ViewPagerWithRecyclerDemo【RecyclerView+ViewPager实现类似TabLayout+ViewPager效果】

    版权声明:本文为HaiyuKing原创文章,转载请注明出处! 前言 使用RecyclerView+ViewPager实现类似TabLayout+ViewPager效果. 效果图 使用步骤 一.项目组织 ...

  9. Java基础知识回顾之六 ----- IO流

    前言 在上一篇文章中,回顾了Java的多线程.而在本篇文章中主要介绍Java IO的相关知识. IO的介绍 什么是IO? IO的名称又来是Input与Output的缩写,也就是输入流和输出流.输入流用 ...

  10. 【Vue】 ----- 浅谈vue的生命周期

    一.概念 vue生命周期,又叫生命周期钩子函数,是组件从创建到销毁的过程. 二.主要的八大生命周期 1.首先,为方便观察每个周期的特点,我们模拟一个"one"组件的创建与销毁,并在 ...