一、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. Javascript 高级程序设计(第3版) - 第01章

    2017-05-10 js简介 一个叫“不难登”的人发明的.js的流行是因为 ajax 的关系. js分为三个部分: 核心: ECMAScript 文档对象模型: DOM 浏览器对象模型: BOM 核 ...

  2. HDU 4313 Matrix(并查集)

    http://acm.hdu.edu.cn/showproblem.php?pid=4313 题意: 给出一棵树,每条边都有权值,其中有几个点是特殊点,现在破坏边还使得这几个特殊点互相不可达,需要使得 ...

  3. 【MySQL】【一】shell

    进入MySQL:mysql -u root -p 查看当前数据库下所有库:mysql> show databases; 切换到某库:mysql> use sys; 查看sys库下所有表:m ...

  4. 使用ajax无法跨源问题总结

    参考文章: 浏览器同源政策及其规避方法 跨域资源共享 CORS 详解 使用jQuery实现跨域提交表单数据 <form action="http://v.juhe.cn/weather ...

  5. js code

    //在页面增加一个放置图标的区块 if(!document.getElementById('_span_jiucuo')) document.write("<span id='_spa ...

  6. 设计模式之组合模式(composite)

    概念: 将对象组合成树形结构以表示“部分-整体”的层次结构.使用户对单个对象和组合对象的使用更具有一致性. 适用性:想表示对象的部分-整体层次结构.

  7. JavaSE 字符串和正则表达式

    根据不懂的自己整理一下,跟着老师进度刷一遍课本,记录琐碎不懂知识 1.StringTokenizer类 String[] mess= {"整数部分","小数部分" ...

  8. SqlServer中exists和in的区别

    1.in 2.exists

  9. week02 课堂作业

    测试一:(点此看原题目) 运行结果: 测试二:(点此看原题目) 运行结果: 测试三:(点此看原题目) 运行结果:

  10. VPC配置介绍

    VPC(Virtual Port-Channel)是Cisco Nexus系列交换机中的一个特性.它支持一个跨机箱的二层Port-Channel.对于第三方设备来说(交换机或服务器)物理上是连接到了两 ...