1.spring 不会自动去寻找注解,必须告诉 spring 哪些包下的类中可能有注解;使用注解来取代配置文件.
1.1 引入xmlns:context ,指定扫描范围

<context:component-scan base-package="com.advice,com.test"></context:component-scan>

1.3 @Component

1.4 相当于<bean/>

1.5 如果没有参数,把类名首字母变小写,相当于<bean id=”别名”/>

1.6 @Component(“自定义名称”)

1.7     <aop:aspectj-autoproxy proxy-target-class="true"> </aop:aspectj-autoproxy>
                  <!--proxy-target-class="true"使用cglib动态代理;
                          否则使用jdk动态代理-->

1.8  实现步骤:

2.1  在spring 配置文件中设置注解在哪些包中 ,多个文件夹用分号隔开 ;以及加入属性:"  xmlns:context"" xsi:schemaLocation"

<?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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
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
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd"> <context:component-scan base-package="com.advice,com.test"></context:component-scan>
<aop:aspectj-autoproxy proxy-target-class="true"></aop:aspectj-autoproxy>
<!--proxy-target-class="true"使用cglib动态代理;
否则使用jdk动态代理--> </beans>

2.2在Demo 类中添加@Componet

  2.2.1在方法上添加@Pointcut(“”) 定义切点

import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component; @Component
public class demo {
@Pointcut("execution(* com.test.demo.demo1())")
public void demo1(){
// int i=5/0;
System.out.println("demo1");
}
}

2.3在通知类中配置 --@Component @Aspect 都不能剩下!!

  2.3.1@Component 类被spring 管理
    @Aspect 相当于<aop:aspect/>表示通知方法在当前类中;

package com.advice;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component; @Component
@Aspect
public class MyAdvice {
@Before("com.test.demo.demo1()")
public void mybefore(){
System.out.println("前置通知!!");
}
@After("com.test.demo.demo1()")
public void myafter(){
System.out.println("后置通知");
}
@AfterThrowing("com.test.demo.demo1()")
public void mythrow(){
System.out.println("异常通知");
}
@Around("com.test.demo.demo1()")
public Object myArround(ProceedingJoinPoint p) throws
Throwable {
System.out.println("环绕-前置");
Object result = p.proceed();
System.out.println("环绕-后置");
return result;
}
}

2.4 编写test测试类并进行测试

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class test {
public static void main(String[] args) {
ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
// 输出Spring自动加载的所有类
// String[] names = ac.getBeanDefinitionNames();
// for(int i=0;i< names.length;i++)
// System.out.println(names[i]);
demo d=ac.getBean("demo",demo.class);
d.demo1(); }
}
myAdvice
demo
org.springframework.context.annotation.internalConfigurationAnnotationProcessor
org.springframework.context.annotation.internalAutowiredAnnotationProcessor
org.springframework.context.annotation.internalRequiredAnnotationProcessor
org.springframework.context.annotation.internalCommonAnnotationProcessor
org.springframework.aop.config.internalAutoProxyCreator
org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor
org.springframework.context.annotation.ConfigurationClassPostProcessor.enhancedConfigurationProcessor
环绕-前置
前置通知!!
demo1
环绕-后置
后置通知

(测试结果)


八.代理设计模式

1.设计模式:前人总结的一套解决特定问题的代码.
2.代理设计模式优点:
2.1保护真实对象
2.2让真实对象职责更明确.
2.3扩展
3.代理设计模式
3.1真实对象.(老总)
3.2代理对象(秘书)
1.抽象对象(抽象功能),谈小目标

九. 静态代理设计模式

2.由代理对象代理所有真实对象的功能.
2.1自己编写代理类

2.2每个代理的功能需要单独编写
3.静态代理设计模式的缺点:
1.当代理功能比较多时,代理类中方法需要写很多.

十. 动态代理

2.为了解决静态代理频繁编写代理功能缺点.
3.分类:
3.1JDK 提供的cglib 动态代理

十一. JDK 动态代理

1.和cglib 动态代理对比
1.1优点:jdk 自带,不需要额外导入jar
1.2缺点:
1.2.1真实对象必须实现接口
1.2.2利用反射机制.效率不高.
2.使用JDK 动态代理时可能出现异常ClasscastException : cat not cast to xxx.
出现原因:希望把接口对象转换为具体真实对象

十二: cglib 动态代理

1.cglib 优点:
1.1基于字节码,生成真实对象的子类.
1.1.1运行效率高于JDK 动态代理.
1.2不需要实现接口
2.cglib 缺点:
2.1非JDK 功能,需要额外导入jar
3.使用spring aop 时,只要出现Proxy 和真实对象转换异常
3.1设置为true 使用cglib
3.2设置为false 使用jdk(默认值)

<aop:aspectj-autoproxy
proxy-target-class="true"></aop:aspectj-autoproxy>

Spring -07 -AOP [面向切面编程] - 使用注解@+ AspectJ 方式实现环绕/前/后等通知 -超简洁 --静态代理/动态代理{JDK/cglib}的更多相关文章

  1. spring:AOP面向切面编程(注解)03

    使用注解写aop时最好使用环绕通知写 切面类: /** * 用于记录日志的工具类,它里面提供了公共的代码 */ @Component("logger") @Aspect //表示当 ...

  2. Spring:AOP面向切面编程

    AOP主要实现的目的是针对业务处理过程中的切面进行提取,它所面对的是处理过程中的某个步骤或阶段,以获得逻辑过程中各部分之间低耦合性的隔离效果. AOP是软件开发思想阶段性的产物,我们比较熟悉面向过程O ...

  3. Spring 08: AOP面向切面编程 + 手写AOP框架

    核心解读 AOP:Aspect Oriented Programming,面向切面编程 核心1:将公共的,通用的,重复的代码单独开发,在需要时反织回去 核心2:面向接口编程,即设置接口类型的变量,传入 ...

  4. Spring AOP 面向切面编程相关注解

    Aspect Oriented Programming 面向切面编程   在Spring中使用这些面向切面相关的注解可以结合使用aspectJ,aspectJ是专门搞动态代理技术的,所以比较专业.   ...

  5. Spring的AOP面向切面编程

    什么是AOP? 1.AOP概念介绍 所谓AOP,即Aspect orientied program,就是面向方面(切面)的编程. 功能: 让关注点代码与业务代码分离! 关注点: 重复代码就叫做关注点: ...

  6. Spring框架——AOP面向切面编程

    简介 AOP练习 使用动态代理解决问题 Spring AOP 用AspectJ注解声明切面 前置后置通知 利用方法签名编写AspectJ切入点表达式 指定切面的优先级 基于XML的配置声明切面 Spr ...

  7. Spring框架 AOP面向切面编程(转)

    一.前言 在以前的项目中,很少去关注spring aop的具体实现与理论,只是简单了解了一下什么是aop具体怎么用,看到了一篇博文写得还不错,就转载来学习一下,博文地址:http://www.cnbl ...

  8. spring:AOP面向切面编程02

    参考: https://blog.csdn.net/jeffleo/article/details/54136904 一.AOP的核心概念AOP(Aspect Oriented Programming ...

  9. Spring之AOP(面向切面编程)_入门Demo

    AOP是OOP的延续,是Aspect Oriented Programming的缩写,意思是面向切面编程.AOP实际是GoF设计模式的延续,设计模式孜孜不倦追求的是调用者和被调用者之间的解耦,AOP可 ...

随机推荐

  1. 【SSH进阶之路】Spring的IOC逐层深入——依赖注入的两种实现类型(四)

    上篇博文,我们介绍了为什么使用IOC容器,和IOC的设计思想以及IOC容器的优缺点,并且给大家转载了一篇介绍IOC原理的博文,我们这篇主要给大家依赖注入的两种方式,以及他们的优缺点. 我们这篇博文还是 ...

  2. 【VS开发】WaitForSingleObject 和 WaitForMultipleObjects函数 (让线程挂起等待事件)

    WaitForSingleObject 和 WaitForMultipleObjects:1.WaitForSingleObject  等待函数可使线程自愿进入等待状态,直到一个特定的内核对象变为已通 ...

  3. csu 1987: 绚丽的手链

    1987: 绚丽的手链 Submit Page   Summary   Time Limit: 6 Sec     Memory Limit: 512 Mb     Submitted: 13     ...

  4. tp5之服务器不显示验证码

    今天在使用tp框架的时候遇到的一个bug,前辈们早已有解决方法,遇到了做个笔记 TP5框架,自带的验证码在本地localhost运行是没问题的,可以正常显示,如图: 然后,把框架拿到服务器是去运行,验 ...

  5. Qt5 QtQuick系列----QtQuick的Secne Graph剖析(2)--自定义QML类型 (继承QQuickItem)

    "当下即永恒"  --- 佚名 Qt用户可以方便地使用QML中的Rectangle等基本类型,但是当不够用时,或,需要开发更高级的界面时,可以自己定义QML类型. 自定义QML类型 ...

  6. chamfer_pcd

    import tensorflow as tf import numpy as np def distance_matrix(array1, array2): """ a ...

  7. LeetCode 686. 重复叠加字符串匹配(Repeated String Match)

    686. 重复叠加字符串匹配 686. Repeated String Match 题目描述 给定两个字符串 A 和 B,寻找重复叠加字符串 A 的最小次数,使得字符串 B 成为叠加后的字符串 A 的 ...

  8. 一起来学Spring Cloud | 第八章:消息总线(Spring Cloud Bus)

    上一章节,我们讲解了分布式配置中心spring cloud config,我们把配置项存放在git或者本地,当我们修改配置时,需要重新启动服务才能生效.但是在生产上,一个服务部署了多台机器,重新启动比 ...

  9. chmod: changing permissions of 'xxx': Operation not permitted

    众所周知,在linux系统中,权限最大的是root账号,但凡修改涉及到系统本身的重大权限的操作,都需要root的权限才能操作.但是有些时候也有root干不了的事情. 比如:chmod: changin ...

  10. 你有自信写while(true)吗?

    每次写while(true)的时候会不会心虚? 特别逻辑稍微复杂一点