1、spring的半自动代理(从spring中获取代理对象)

(1)spring中的通知类型

spring中aop的通知类型有五种:

前置:在目标方法执行前实施加强

后置:在目标方法执行后实施加强

环绕:在目标方法执行前后实施加强,必须手动执行目标方法,如果只在目标方法前面书写方法,就叫前置通知,前置通知可以阻止目标方法的执行,因为抛出异常后进入catch块,后置通知可以获得方法的返回值。

异常:在方法抛出异常后实施加强

引介:在目标类中添加一些新的方法和属性

(2)导入jar包

核心:4+1

aop:aop联盟(规范)、spring-aop(实现)

(3)创建目标类接口和接口的实现类:

public interface StudentService {
void addStudent();
void updateStudent();
void deleteStudent();
}
public class StudentServiceImpl implements StudentService{
@Override
public void addStudent() {
System.out.println("addStudent");
} @Override
public void updateStudent() {
System.out.println("updateStudent");
} @Override
public void deleteStudent() {
System.out.println("deleteStudent");
}
}

(4)创建切面:

public class MyAspect implements MethodInterceptor {
@Override
public Object invoke(MethodInvocation methodInvocation) throws Throwable {
System.out.println("前");
methodInvocation.proceed();
System.out.println("后");
return null;
}
}

(5)配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd">
<!--目标类-->
<bean id="studentService" class="pers.zhb.proxy.StudentServiceImpl"></bean>
<!--切面类-->
<bean id="myAspect" class="pers.zhb.proxy.MyAspect"></bean>
<!--代理类
用于创建代理工厂bean,生成特殊代理对象
-->
<bean id="proxystudentService" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="interfaces" value="pers.zhb.proxy.StudentService"> </property><!--确定接口-->
<property name="target" ref="studentService"></property><!--目标类-->
<property name="interceptorNames" value="myAspect"></property><!--切面类的名称-->
</bean>
</beans>
  <property name="optimize" value="true"></property><!--强制使用cglib-->

如果目标类有接口使用jdk动态代理,没有接口的话采用cglib字节码增强。如果声明为true,则无论是否有接口都是采用cglib

(6)测试:

public class TestFactoryBean {
public static void main(String[] args) {
ApplicationContext applicationContext=new
ClassPathXmlApplicationContext("applicationContext.xml");
StudentService studentService= (StudentService) applicationContext.getBean("proxystudentService");
studentService.addStudent();
studentService.deleteStudent();
studentService.updateStudent();
}
}

addStudent


deleteStudent


updateStudent

2、全自动(从spring容器获得目标类,如果配置aop,spring将自动生成代理)

(1)导包:

要确定目标类,aspectj切入点表达式,导入jar包:

(2)创建目标类接口和接口的实现类:

public interface StudentService {
void addStudent();
void updateStudent();
void deleteStudent();
}
public class StudentServiceImpl implements StudentService{
@Override
public void addStudent() {
System.out.println("addStudent");
} @Override
public void updateStudent() {
System.out.println("updateStudent");
} @Override
public void deleteStudent() {
System.out.println("deleteStudent");
}
}

(3)切面:

public class MyAspect implements MethodInterceptor {
@Override
public Object invoke(MethodInvocation methodInvocation) throws Throwable {
System.out.println("前");
methodInvocation.proceed();
System.out.println("后");
return null;
}
}

(4)配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns="http://www.springframework.org/schema/beans"
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="studentService" class="pers.zhb.proxy.StudentServiceImpl"></bean>
<!--切面类-->
<bean id="myAspect" class="pers.zhb.proxy.MyAspect"></bean>
<!--aop编程-->
<aop:config >
<!--从目标对象获得具体方法,使用了切入点表达式-->
<aop:pointcut id="myPointCut" expression="execution(* pers.zhb.proxy.*.*(..))"></aop:pointcut>
<aop:advisor advice-ref="myAspect" pointcut-ref="myPointCut"></aop:advisor><!--特殊的通知,只有一个通知和切入点-->
</aop:config>
</beans>

(5)测试类:

public class TestFactoryBean {
public static void main(String[] args) {
ApplicationContext applicationContext=new
ClassPathXmlApplicationContext("applicationContext.xml");
//获得目标类
StudentService studentService= (StudentService) applicationContext.getBean("studentService");
studentService.addStudent();
studentService.deleteStudent();
studentService.updateStudent();
}
}

addStudent


deleteStudent


updateStudent

spring的aop编程(半自动、全自动)的更多相关文章

  1. Java基础-SSM之Spring的AOP编程

    Java基础-SSM之Spring的AOP编程 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任.   Spring的本质说白了就是动态代理,接下来我们会体验AOP的用法.它是对OOP的 ...

  2. Spring之AOP编程

    一.AOP简介     AOP的英文全称是Aspect Oriented Programming,意为:面向切面编程.     AOP采取横向抽取的机制,取代了传统纵向继承体系的代码复用.AOP常用于 ...

  3. Spring框架--AOP编程

    2 手动实现AOP编程 AOP 面向切面的编程, AOP可以实现"业务代码"与"关注点代码"分离 // 保存一个用户 public void add(User ...

  4. Spring的AOP编程

    1.手动实现AOP编程(代理模式) AOP是面向切面的编程,主要功能就是实现"业务代码"和辅助业务代码的"关注点代码"分离.在一个方法中,出了核心的业务代码,其 ...

  5. Java进阶知识21 Spring的AOP编程

    1.概述 Aop:(Aspect Oriented Programming)面向切面编程          功能: 让关注点代码与业务代码分离! 关注点:重复代码就叫做关注点:切面: 关注点形成的类, ...

  6. 【AOP】spring 的AOP编程报错:[Xlint:invalidAbsoluteTypeName]error

    AOP来发过程中,报错如下: warning no match for this type name: net.shopxx.wx.institution.controller [Xlint:inva ...

  7. spring相关—AOP编程—切入点、连接点

    1 切入点表达式 1.1 作用 通过表达式的方式定位一个或多个具体的连接点. 1.2 语法细节 ①切入点表达式的语法格式 execution([权限修饰符] [返回值类型] [简单类名/全类名] [方 ...

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

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

  9. Spring基础篇——Spring的AOP切面编程

    一  基本理解 AOP,面向切面编程,作为Spring的核心思想之一,度娘上有太多的教程啊.解释啊,但博主还是要自己按照自己的思路和理解再来阐释一下.原因很简单,别人的思想终究是别人的,自己的理解才是 ...

随机推荐

  1. NeoVIM安装使用

    1.What's Neovim Bram Moolenaar 在写 Vim 时还是 90 年代初,至今已经 20 多年 过去了.其中,不仅包含了大量的遗留代码,而且程序的维护.Bug 的 修复.以及新 ...

  2. Mac包管理神器:Home-brew

    最近看到一个大神修改的Homebrew国内脚本,安装非常方便,以前使用国外的经常下载不下来,这个感觉是非常快的. Homebrew 国内自动安装脚本 ,修改原脚本中的 clone 操作为“浅拷贝”(- ...

  3. ORB-SLAM: A Versatile and Accurate Monocular SLAM System 笔记(一)

    ORB-SLAM: A Versatile and Accurate Monocular SLAM System Abstract 这篇文章提出了 ORB-SLAM,一个基于特征的单目SLAM系统,这 ...

  4. 标准git请求

    initCate() // 定义加载文章分类的方法 function initCate() { $.ajax({ method: 'GET', url: '/my/article/cates', su ...

  5. 夜息seo培训内部教程

    http://www.wocaoseo.com/thread-268-1-1.html 随着SEO日益正规化,在企业中推行SEO变得越来越重要,在上一文<将SEO整合入整个网站项目>中也有 ...

  6. React设计模式相关

    关于我在React设计模式上做的一些思考: 一,项目里实战的经历 最开始我根据组件不同的职能定义,拆分了展示组件和容器组件两大类,后来随着业务逻辑越来越复杂,容器组件代码越来越冗长,我又加入了HOC高 ...

  7. Pytest-allure 生成美观好看的测试报告

    在我们使用pytest-allure生成测试报告时,需要分为以下几步来执行 1.pytest TestCal.py --alluredir=/tmp/my_allure_results[这一步,是设置 ...

  8. Educational Codeforces Round 68 (Rated for Div. 2)-D. 1-2-K Game

    output standard output Alice and Bob play a game. There is a paper strip which is divided into n + 1 ...

  9. Cassandra架构、设计(集群&表)和性能报告

    系统架构: Cassandra 是 一 套 开 源 分 布 式 No -SQL 数据库系统, 基于一致性哈希算法的 P2P 环形结构. 这种结构 各节点功能完全相 同, 可灵活添加节点来完成系 统的扩 ...

  10. IDEA左侧文件目录不见了,帮你找回来!

    前几天不知道什么操作,把IDEA左侧项目的目录给弄没了,如下图,在百度上搜索了不少,就是没有效果,很是头疼,巧的是,今天琢磨了一下,又给弄回来了,所以在此记录一下,以后再给弄没了,就知道了,同时也算是 ...