一、AOP(基于XML方式配置AOP)

AOP(Aspect Oriented Program):面向切面编程。思想是:把功能分为核心业务功能和周边功能。

所谓核心业务功能:比如登录,增删改数据都叫做核心业务

周边功能:比如性能统计,日志,事务管理等。

在面向切面编程AOP的思想里面,核心业务功能和切面功能分别独立进行开发。然后再把他们两个"编织"在一起,就叫做AOP。

1、首先是jar包导入

为了支持AOP,需要用到一些额外的jar包。

2、开发思路图

  2.1、功能分两大类:辅助功能和核心业务功能

  2.2、辅助功能和核心业务功能彼此独立开发

  2.3、比如说登录功能,就是没有性能统计和日志输出功能,也可以正常运行的

  2.4、如果有需要的话,就可以把日志输出功能和登录功能编织在一起,这样登录的时候可以查看到日志输出。

  2.5、辅助功能:也叫做切面功能,能够选择性的,低耦合的把切面和核心业务功能结合在一起的编程思想叫做面向切面编程。

3、准备业务类ProductService

package com.demo.service;

public class ProductService{
public void doService(){
System.out.println("doService");
}
}

4、TestSpring测试

package com.demo.test;

import org.springframework.context.ApplicationContext;
import org.springframework.support.ClassPathXmlApplicationContext; import com.demo.service.ProductService; public class TestSpring{
public static void main(String[] args){
//加载applicationContext.xml配置文件
ApplicationContext context=new ClassPathXmlApplicationContext(new String[]{"applicationContext.xml"});。
ProductService service=(ProductService)context.getBean("service");//获取配置文件的ProductService属性name
service.doService();//doService
}
}

5、准备日志切面LoggerAspect

日志切面的功能:在调用核心功能之前和之后分别打印日志,所谓切面就是那些辅助功能。

package com.demo.aspect;

import org.aspectj.lang.ProceedingJoinPoint;

public class LoggerAspect{
public Object log(ProceedingJoinPoint joinPoint) throws Throwable{
System.out.println("start log:"+joinPoint.getSignature().getName());
//将来与某个核心功能编织之后,用来执行核心功能的代码
Object object=joinPoint.proceed();//编织 System.out.println("end log:"+joinPoint.getSignature().getName());
return object;
}
}

6、配置applicationContext.xml

配置ProductService业务对象。

<bean name="s" class="com.demo.service.ProductService"></bean>

配置日志切面

<bean id="loggerAspect" class="com.demo.aspect.LoggerAspect"></bean>

声明日志切面

<aop:pointcut id="loggerCutPoint" expression="execution(* com.demo.service.ProductService.*(..))"
/>

execution(* com.demo.service.ProductService.*(..))表示进行切面操作:

*:表示返回任意类型

com.demo.service.ProductService.*:表示包名以com.demo.service.ProductService开头的类的任意方法。

(..):表示参数是任意数量和类型。

指定核心功能:

<aop:aspect id="logAspect" ref="loggerAspect">
<aop:around pointcut-ref="loggerCutpoint" method="log"/>
</aop:aspect>

具体的applicationContext.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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema.context"
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
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <bean name="category" class="com.demo.pojo.Category">
<property name="name" value="Apple"/>
</bean> <bean name="product" class="com.demo.pojo.Product">
<property name="name" value="Fruit"/>
<prpperty name="category" ref="category"/>
</bean>
<!--配置ProductService核心业务功能-->
<bean name="service" class="com.demo.service.ProductService"></bean>
<!--配置切面功能-->
<bean id="loggerAspect" class="com.demo.aspect.LoggerAspect"></bean> <aop:config>
<aop:pointcut id="loggerCutpoint" expression="(* com.demo.service.ProductService.*(..))"/>
<!--将切面功能编织在ProductService核心业务功能中-->
<aop:aspect id="logAspect" ref="loggerAspect">
<aop:around pointcut-ref="loggerCutpoint" method="log"/>
</aop:aspect>
</aop:config>
</beans>

7、测试切面功能编织到核心业务功能后

TestSpring:

package com.demo.test;

import org.springframework.context.ApplicationContext;
import org.springframework.support.ClassPathXmlApplicationContext; import com.demo.service.ProductService; public class TestSpring{
public static void main(String[] args){
//加载applicationContext.xml配置文件
ApplicationContext context=new ClassPathXmlApplicationContext(new String[]{"applicationContext.xml"});
//根据ApplicationContext对象提供的getBean()方法,获取配置bean的name属性值。
ProductService service=(ProductService)context.getBean("service");
service.doService();//doService
}
}

二、AOP(基于注解方式AOP)

1、注解配置业务类

使用@Component("service")注解ProductService类,表明这个类是业务类

package com.demo.service;

import org.springframework.stereotype.Component;

@Component("service")
public class ProductService{
public void doService(){
System.out.println("doService");
}
}

2、注解配置切面

@Aspect注解:表示这是一个切面

@Component注解:表示这是一个bean,由Spring进行管理

@Around(value="execution(* com.demo.service.ProductService.*(..))")注解:表示对com.demo.service.ProductService这个类中的所有方法进行切面操作。

  2.1、编写LoggerAspect(日志记录类)切面

package com.demo.aspect;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Compoent; @Aspect
@Component
publc class LoggerAspect{
@Around(value="execute(* com.demo.service.ProductService.*(..))")
publc Object log(ProceedingJoinPoint joinPoint) throws Throwable{
System.out.println("start log:"+joinPoint.getSignature().getName()); Object object=joinPoint.proceed(); System.out.println("end log:"+joinPoint.getSignature().getName()); return object;
}
}

3、修改applicationContext.xml配置文件

去掉原来的bean的配置,添加如下:

<!--作用:告诉Spring,实体类bean都放在com.demo.pojo这个包下,进行扫描-->
<context:component-scan base-package="com.demo.pojo"> <!--作用:告诉Spring,业务类都放在com.demo.pojo这个包下,进行扫描-->
<context:component-scan base-package="com.demo.service">
<!--作用:告诉Spring,切面类都放在com.demo.aspect这个包下,进行扫描-->
<context:component-scan base-package="com.demo.aspect">
<aop:aspectj-autoproxy/><!--进行业务类和切面类的编织-->

applicationContext.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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
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
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <context:component-scan base-package="com.demo.aspect"/>
<context:component-scan base-package="com.demo.service"/>
<aop:aspectj-autoproxy/> </beans>

4、TestSpring测试

package com.demo.test;

import org.springframework.context.ApplicationContext;
import org.springframework.support.ClassPathXmlApplicationContext; import com.demo.service.ProductService; public class TestSpring{
public static void main(String[] args){
//加载applicationContext.xml配置文件
ApplicationContext context=new ClassPathXmlApplicationContext(new String[]{"applicationContext.xml"});
//根据ApplicationContext对象提供的getBean()方法,通过在ProductService业务类的Component("service")注解获取到业务对象。
ProductService service=(ProductService)context.getBean("service");
service.doService();//doService
}
}

十八、Spring框架(AOP)的更多相关文章

  1. spring框架 AOP核心详解

    AOP称为面向切面编程,在程序开发中主要用来解决一些系统层面上的问题,比如日志,事务,权限等待,Struts2的拦截器设计就是基于AOP的思想,是个比较经典的例子. 一 AOP的基本概念 (1)Asp ...

  2. 跟着刚哥学习Spring框架--AOP(五)

    AOP AOP(Aspect Oriented Programming),即面向切面编程,可以说是OOP(Object Oriented Programming,面向对象编程)的补充和完善.OOP引入 ...

  3. spring框架aop用注解形式注入Aspect切面无效的问题解决

    由于到最后我的项目还是有个邪门的错没解决,所以先把文章大概内容告知: 1.spring框架aop注解扫描默认是关闭的,得手动开启. 2.关于Con't call commit when autocom ...

  4. Spring框架——AOP代理

    我们知道AOP代理指的就是设计模式中的代理模式.一种是静态代理,高效,但是代码量偏大:另一种就是动态代理,动态代理又分为SDK下的动态代理,还有CGLIB的动态代理.Spring AOP说是实现了AO ...

  5. Spring框架-AOP详细学习[转载]

    参考博客:https://blog.csdn.net/qq_22583741/article/details/79589910#4-%E4%BE%9D%E8%B5%96%E6%B3%A8%E5%85% ...

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

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

  7. 10 Spring框架 AOP (三) Spring对AspectJ的整合

    上两节我们讲了Spring对AOP的实现,但是在我们的开发中我们不太使用Spring自身的对AOP的实现,而是使用AspectJ,AspectJ是一个面向切面的框架,它扩展了Java语言.Aspect ...

  8. Spring框架AOP学习总结(下)

    目录 1. AOP 的概述 2. Spring 基于AspectJ 进行 AOP 的开发入门(XML 的方式): 3.Spring 基于AspectJ 进行 AOP 的开发入门(注解的方式): 4.S ...

  9. 08 Spring框架 AOP (一)

    首先我们先来介绍一下AOP: AOP(Aspect Orient Programming),面向切面编程,是面向对象编程OOP的一种补充.面向对象编程是从静态角度考虑程序的结构,面向切面编程是从动态的 ...

  10. 09 Spring框架 AOP (二) 高级用法

    上一篇文章我们主要讲了一点关于AOP编程,它的动态考虑程序的运行过程,和Spring中AOP的应用,前置通知,后置通知,环绕通知和异常通知,这些都是Spring中AOP最简单的用法,也是最常用的东西, ...

随机推荐

  1. nsswitch & pam

    nsswitch & pam nsswitch是名称解析框架服务,pam是认证框架服务 对主机来说,有两个功能可能用到框架性服务 1.名称解析: name: id 2.认证服务:验证当前请求获 ...

  2. 【Selenium2】【问题】

    [iframe 和 HTML 相互嵌套] 比如126登录页,我的几个方法都不好用 1. iframeFather = driver.find_element(By.XPATH,"//div[ ...

  3. JSON平铺功能的实现(JS操作JSON数据)

    一.什么是JSON平铺 JSON平铺分成两种: 平铺的json转树结构的json 例如: { a: 'value', b: 'b1.value' } // 转换成=> { a: 'value', ...

  4. ImgNoGoodWindow

    using System;using System.Collections.Generic;using System.Linq;using System.Text;using UnityEditor; ...

  5. django网站地图sitemap

    网站地图是根据网站的结构.框架.内容,生成的导航网页,是一个网站所有链接的容器.很多网站的连接层次比较深,蜘蛛很难抓取到,网站地图可以方便搜索引擎或者网络蜘蛛抓取网站页面,了解网站的架构,为网络蜘蛛指 ...

  6. IPC 之 ContentProvider 的使用

    一.概述 ContentProvider 是 Android 中提供的专门用于不同应用间进行数据共享的方式.和 Messenger 一样,ContentProvider 的底层实现同样也是 Binde ...

  7. 《剑指offer》第六十五题(不用加减乘除做加法)

    // 面试题65:不用加减乘除做加法 // 题目:写一个函数,求两个整数之和,要求在函数体内不得使用+.-.×.÷ // 四则运算符号. #include <iostream> int A ...

  8. 学习笔记47—PhotoShop技巧

    1.photoshop里怎么给画布画对角线? photoshop里给画布画对角线有二种方法: 1) 选直线工具 从一角拉向另一对角 就OK了 非常简单: 2) 选钢笔工具 鼠标先点击某一角 然后再点击 ...

  9. 优秀的web工具网址

    1.百度开源的工具 https://www.baidu.com/home/news/data/newspage?nid=3868911095318333105&n_type=0&p_f ...

  10. DirectX学习之第一个可运行的工程

    学习一门开发语言的第一件事情当然是搭建一个可运行的环境,然后运行一个可成功执行的案例. 本人在学习DirectX的第一个工程的时候,参考了雨凇MoMo的一篇文章(https://www.xuanyus ...