Spring框架的关键组件是面向方面编程(AOP)框架。面向方面的编程不仅打破程序逻辑分成不同的部分称为所谓的担忧。跨越多个点的应用程序的功能被称为横切关注点和这些横切关注点是从应用程序的业务逻辑概念上区分开来。还有像日志记录,审计,声明性事务,安全性和高速缓存等方面的各种常见的好例子

模块化的OOP中的关键单元是类,而在AOP中模块化的单元则是切面。依赖注入可以帮助你从对方解耦应用程序对象和AOP可以帮助你从他们影响的对象分离横切关注点。 AOP是一样的编程语言如Perl,.NET,Java和其他触发器。

Spring AOP模块提供了拦截器拦截的应用程序,例如,执行一个方法时,可以之前或之后执行的方法添加额外的功能。

AOP术语:

在我们开始使用AOP之前,先熟悉AOP的概念和术语。这些条款是不特定于Spring,问题都是有关AOP。

建议的类型

Spring方面可以用5种下面提到的建议:

自定义方面实现

Spring基于XML模式的AOP需要如下所述导入Spring AOP架构:

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns:aop="http://www.springframework.org/schema/aop"

xsi:schemaLocation="http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans-3.0.xsd

http://www.springframework.org/schema/aop

http://www.springframework.org/schema/aop/spring-aop-3.0.xsd ">

还需要在以下应用程序CLASSPATH中的AspectJ库。这些库可以在AspectJ的安装'lib'目录可用,可以从互联网上下载它们。

aspectjrt.jar

aspectjweaver.jar

aspectj.jar

声明一个切面

一个方面是使用元素中声明,并且支持bean是使用ref属性如下参考:

...

...

这里的“aBean”将配置和依赖注入,就像任何其他的Spring bean,我们已经在前面的章节看到。

声明一个切入点

一个切入点有助于确定与不同要执行的连接点的利息(即方法)。同时与XML架构基础的配置工作,切入点将被定义如下:

expression="execution(* com.xyz.myapp.service.*.*(..))"/>

...

...

下面的示例定义一个名为'的businessService“切入点将匹配可用的软件包com.yiibai下执行getName()方法在Student类:

expression="execution(* com.yiibai.Student.getName(..))"/>

...

...

声明建议

可以声明任意五个建议的使用元素下面给出一个内:

expression="execution(* com.xyz.myapp.service.*.*(..))"/>

method="doRequiredTask"/>

method="doRequiredTask"/>

returning="retVal"

method="doRequiredTask"/>

throwing="ex"

method="doRequiredTask"/>

method="doRequiredTask"/>

...

...

可以使用相同的doRequiredTask或不同的方法针对不同的建议。这些方法将被定义为纵横模块的一部分。

基于XML模式的AOP例

要理解上述关系到XML模式的AOP提到的概念,让我们写这将实现几个建议的一个例子。

这里是Logging.java文件的内容。这实际上是纵横模块的一个示例,它定义的方法被调用的各个点。

package com.yiibai;

public class Logging {

/**

* This is the method which I would like to execute

* before a selected method execution.

*/

public void beforeAdvice(){

System.out.println("Going to setup student profile.");

}

/**

* This is the method which I would like to execute

* after a selected method execution.

*/

public void afterAdvice(){

System.out.println("Student profile has been setup.");

}

/**

* This is the method which I would like to execute

* when any method returns.

*/

public void afterReturningAdvice(Object retVal){

System.out.println("Returning:" + retVal.toString() );

}

/**

* This is the method which I would like to execute

* if there is an exception raised.

*/

public void AfterThrowingAdvice(IllegalArgumentException ex){

System.out.println("There has been an exception: " + ex.toString());

}

}

以下是Student.java文件的内容:

package com.yiibai;

public class Student {

private Integer age;

private String name;

public void setAge(Integer age) {

this.age = age;

}

public Integer getAge() {

System.out.println("Age : " + age );

return age;

}

public void setName(String name) {

this.name = name;

}

public String getName() {

System.out.println("Name : " + name );

return name;

}

public void printThrowException(){

System.out.println("Exception raised");

throw new IllegalArgumentException();

}

}

以下是MainApp.java文件的内容:

package com.yiibai;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MainApp {

public static void main(String[] args) {

ApplicationContext context =

new ClassPathXmlApplicationContext("Beans.xml");

Student student = (Student) context.getBean("student");

student.getName();

student.getAge();

student.printThrowException();

}

}

以下是配置文件beans.xml文件:

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns:aop="http://www.springframework.org/schema/aop"

xsi:schemaLocation="http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans-3.0.xsd

http://www.springframework.org/schema/aop

http://www.springframework.org/schema/aop/spring-aop-3.0.xsd ">

expression="execution(* com.yiibai.*.*(..))"/>

returning="retVal"

method="afterReturningAdvice"/>

throwing="ex"

method="AfterThrowingAdvice"/>

创建源代码和bean配置文件完成后,让我们运行应用程序。如果一切顺利,这将打印以下信息:

Going to setup student profile.

Name : Zara

Student profile has been setup.

Returning:Zara

Going to setup student profile.

Age : 11

Student profile has been setup.

Returning:11

Going to setup student profile.

Exception raised

Student profile has been setup.

There has been an exception: java.lang.IllegalArgumentException

.....

other exception content

解释一下,上面定义选择所有的包com.yiibai下定义的方法。让我们假设,想有一个特定的方法之前或之后执行意见,可以定义切入点与实际的类和方法的名称取代星号(*)的切入点定义来缩小执行。下面是修改后的XML配置文件,以显示概念:

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns:aop="http://www.springframework.org/schema/aop"

xsi:schemaLocation="http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans-3.0.xsd

http://www.springframework.org/schema/aop

http://www.springframework.org/schema/aop/spring-aop-3.0.xsd ">

expression="execution(* com.yiibai.Student.getName(..))"/>

如果执行这些配置更改的示例应用程序,这将打印以下信息:

Going to setup student profile.

Name : Zara

Student profile has been setup.

Age : 11

Exception raised

.....

other exception content

基于@AspectJ的AOP@ AspectJ是指声明方面的风格注释的使用Java 5注释普通的Java类。对@ AspectJ支持由包括您基于XML Schema的配置文件里面的下列元素启用。

您还需要在以下应用程序的类路径中的AspectJ库。这些库可以在AspectJ的安装的'lib'目录,可以从网上下载他们.

aspectjrt.jar

aspectjweaver.jar

aspectj.jar

声明一个切面

方面类是像任何其他普通的bean,并可能有方法和字段,就像任何其他类,但他们将被标注了@Aspect 如下:

package org.xyz;

import org.aspectj.lang.annotation.Aspect;

@Aspect

public class AspectModule {

}

他们将在XML中进行配置像任何其他的bean,如下所示:

声明一个切入点

一个切入点有助于确定与不同意见要执行的连接点的权益(即方法)。同时用@AspectJ的基础配置工作,切入点声明有两个部分:

切入点表达式,决定哪些方法执行我们感兴趣

一个切入点签名的包含名字和任意数量的参数。该方法的实际主体是不相关的,实际上应为空。

下面的示例定义一个名为'businessService“切入点将匹配每个方法的可用包com.xyz.myapp.service下执行中的类:

import org.aspectj.lang.annotation.Pointcut;

@Pointcut("execution(* com.xyz.myapp.service.*.*(..))") // expression

private void businessService() {} // signature

下面的示例定义一个名为'getName'切入点将匹配可用的软件包com.yiibai下执行getName()方法在Student类:

import org.aspectj.lang.annotation.Pointcut;

@Pointcut("execution(* com.yiibai.Student.getName(..))")

private void getname() {}

声明建议

可以声明任何使用 @{ADVICE-NAME} 注释下面给出的五个建议。这假定已经定义了一个切入点签名的方法的businessService():

@Before("businessService()")

public void doBeforeTask(){

...

}

@After("businessService()")

public void doAfterTask(){

...

}

@AfterReturning(pointcut = "businessService()", returning="retVal")

public void doAfterReturnningTask(Object retVal){

// you can intercept retVal here.

...

}

@AfterThrowing(pointcut = "businessService()", throwing="ex")

public void doAfterThrowingTask(Exception ex){

// you can intercept thrown exception here.

...

}

@Around("businessService()")

public void doAroundTask(){

...

}

可以定义内置切入点的任何意见的。下面是一个例子定义内联的切入点之前的建议:

@Before("execution(* com.xyz.myapp.service.*.*(..))")

public doBeforeTask(){

...

}

@AspectJ 基于AOP例子

要理解上述关系到@AspectJ的AOP的基础概念提到,让我们写这将实现几个建议的一个例子。

这里是Logging.java文件的内容。这实际上是方面模块的一个示例,它定义的方法被调用的各个点。

package com.yiibai;

import org.aspectj.lang.annotation.Aspect;

import org.aspectj.lang.annotation.Pointcut;

import org.aspectj.lang.annotation.Before;

import org.aspectj.lang.annotation.After;

import org.aspectj.lang.annotation.AfterThrowing;

import org.aspectj.lang.annotation.AfterReturning;

import org.aspectj.lang.annotation.Around;

@Aspect

public class Logging {

/** Following is the definition for a pointcut to select

* all the methods available. So advice will be called

* for all the methods.

*/

@Pointcut("execution(* com.yiibai.*.*(..))")

private void selectAll(){}

/**

* This is the method which I would like to execute

* before a selected method execution.

*/

@Before("selectAll()")

public void beforeAdvice(){

System.out.println("Going to setup student profile.");

}

/**

* This is the method which I would like to execute

* after a selected method execution.

*/

@After("selectAll()")

public void afterAdvice(){

System.out.println("Student profile has been setup.");

}

/**

* This is the method which I would like to execute

* when any method returns.

*/

@AfterReturning(pointcut = "selectAll()", returning="retVal")

public void afterReturningAdvice(Object retVal){

System.out.println("Returning:" + retVal.toString() );

}

/**

* This is the method which I would like to execute

* if there is an exception raised by any method.

*/

@AfterThrowing(pointcut = "selectAll()", throwing = "ex")

public void AfterThrowingAdvice(IllegalArgumentException ex){

System.out.println("There has been an exception: " + ex.toString());

}

}

以下是Student.java文件的内容:

package com.yiibai;

public class Student {

private Integer age;

private String name;

public void setAge(Integer age) {

this.age = age;

}

public Integer getAge() {

System.out.println("Age : " + age );

return age;

}

public void setName(String name) {

this.name = name;

}

public String getName() {

System.out.println("Name : " + name );

return name;

}

public void printThrowException(){

System.out.println("Exception raised");

throw new IllegalArgumentException();

}

}

以下是MainApp.java文件的内容:

package com.yiibai;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MainApp {

public static void main(String[] args) {

ApplicationContext context =

new ClassPathXmlApplicationContext("Beans.xml");

Student student = (Student) context.getBean("student");

student.getName();

student.getAge();

student.printThrowException();

}

}

以下是配置文件beans.xml文件:

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns:aop="http://www.springframework.org/schema/aop"

xsi:schemaLocation="http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans-3.0.xsd

http://www.springframework.org/schema/aop

http://www.springframework.org/schema/aop/spring-aop-3.0.xsd ">

创建源程序和bean配置文件完成后,让我们运行应用程序。如果一切顺利,这将打印以下信息:

Going to setup student profile.

Name : Zara

Student profile has been setup.

Returning:Zara

Going to setup student profile.

Age : 11

Student profile has been setup.

Returning:11

Going to setup student profile.

Exception raised

Student profile has been setup.

There has been an exception: java.lang.IllegalArgumentException

.....

other exception content

java aop模式_Java的Spring框架下的AOP编程模式示例相关推荐

  1. java 自动装载_java_详解Java的Spring框架下bean的自动装载方式,Spring容器可以自动装配相互协 - phpStudy...

    详解Java的Spring框架下bean的自动装载方式 Spring容器可以自动装配相互协作bean之间的关系,这有助于减少对XML配置,而无需编写一个大的基于Spring应用程序的较多的和元素. 自 ...

  2. Spring框架(IoC、AOP面向接口切面)

    新建一个Maven工程 Spring框架是由于软件开发的复杂性而创建的.Spring使用的是基本的JavaBean来完成以前只可能由EJB完成的事情.然而,Spring的用途不仅仅限于服务器端的开发. ...

  3. 2022年Java应届生面试之Spring框架基础知识

    前言:工作的原因,最近没有及时更新相关系列.很抱歉!在复习Spring框架的同时也别忘了j2ee的相关知识理论wo~ 大家好!我是JAVA 中的Spring框架,我是一个开源的容器性质的轻量级框架. ...

  4. 深入剖析 RabbitMQ —— Spring 框架下实现 AMQP 高级消息队列协议

    前言 消息队列在现今数据量超大,并发量超高的系统中是十分常用的.本文将会对现时最常用到的几款消息队列框架 ActiveMQ.RabbitMQ.Kafka 进行分析对比. 详细介绍 RabbitMQ 在 ...

  5. Java开发人员可以从Spring框架中学到编程技巧

    毫无疑问,Spring Framework是最受欢迎的Java框架之一,通过提供依赖注入和控制反转等特性,可以轻松创建真实的企业级Java应用程序.但是,Spring不仅是一个DI和IOC框架.通过提 ...

  6. 解决Spring框架下中文乱码的问题

    解决Spring框架下中文乱码的问题 参考文章: (1)解决Spring框架下中文乱码的问题 (2)https://www.cnblogs.com/Summer7C/p/4712818.html (3 ...

  7. Arduino框架下 ESP32看门狗使用示例

    Arduino框架下 ESP32看门狗使用示例 相关篇<Arduino ESP32 看门狗定时器> 开发板型号为:ESP32 DEVKIT V1-DOIT 板载led灯 GPIO 2 -- ...

  8. ESP32C3基于Arduino框架下的 ESP32 RainMaker开发示例教程

    ESP32C3基于Arduino框架下的 ESP32 RainMaker开发示例教程 ESP RainMaker ESP RainMaker 是乐鑫推出的一个端到端平台.基于该平台,用户无需管理基础设 ...

  9. STM32G070RBT6基于Arduino框架下串口数据接收使用示例

    STM32G070RBT6基于Arduino框架下串口数据接收使用示例 相关篇<STM32G070RBT6基于Arduino串口的使用>

最新文章

  1. java 文件内容排序_在Java中对2个大型文本文件进行排序的最佳方法是什么?
  2. 自然语言处理美国政客的社交媒体消息分类
  3. Qt ModbusTCP ModbusRTU 使用同步读和异步写
  4. vue.js建立主页的路由 - 另类的实现方式
  5. java 获取客户端的域用户名_使用java有没有办法提取局域网中的客户端的用户名,客户端是以域用户身份进去的...
  6. ds6708 symbol 驱动_Symbol DS6708扫描器
  7. MongoDB的排除查询$ne缺陷
  8. 【c++ primer读书笔记】【第6章】函数
  9. java反射 set_Java反射
  10. PyTorch并行与分布式(一)概述
  11. CAD计算机辅助设计——文件管理和界面设置
  12. 浅谈歌词文件(LRC、QRC、KRC)
  13. 基于stm32的无线多点温度采集系统设计
  14. Google Play 支付流程参考
  15. SQLServerDBA十大必备工具
  16. 泰坦尼克号人员预测模型(python/jupyter-notebook/数据挖掘/数据分析)
  17. 好的用户界面-界面设计的一些技巧
  18. Kafka3.0 提交offset方式
  19. iPhone设备链接Fiddler代理的设置
  20. 如何通过浏览器访问家里电脑

热门文章

  1. 微信公众号开启开发者模式
  2. TIL —静态工厂方法
  3. 浅写策略模式,及map、枚举结合小例子
  4. 在学校使用计算机的作文,电脑学校作文
  5. 门店定位怎么在地图上显示_怎么能让顾客在地图上搜索到自己店的位置?
  6. 杨辉三角金字塔c语言编程,scratch编程绘制数字金字塔(杨辉三角)
  7. Unity Shader 卡通渲染 (五):仿日式赛璐珞风格 Shader(顶点外扩描边)
  8. wifi网络信息查看
  9. 流媒体解码及H.264编码推流
  10. 爬虫快速入门——Request对象的使用