AOP 即 Aspect Oriented Program 面向切面编程 
首先,在面向切面编程的思想里面,把功能分为核心业务功能,和周边功能。 
所谓的核心业务,比如登陆,增加数据,删除数据都叫核心业务 
所谓的周边功能,比如性能统计,日志,事务管理等等

周边功能在Spring的面向切面编程AOP思想里,即被定义为切面

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

第一步:思路图

1. 功能分两大类,辅助功能和核心业务功能
2. 辅助功能和核心业务功能彼此独立进行开发
3. 比如登陆功能,即便是没有性能统计和日志输出,也可以正常运行
4. 如果有需要,就把"日志输出" 功能和 "登陆" 功能 编织在一起,这样登陆的时候,就可以看到日志输出了
5. 辅助功能,又叫做切面,这种能够选择性的,低耦合的把切面和核心业务功能结合在一起的编程思想,就叫做切面编程

第二步:准备业务类

ProductService.java

 package com.spring.service;

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

第三步:测试(TestSpring)

没有引入切面之前,调用该业务类

 package com.spring.test;

 import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import com.spring.service.ProductService; public class TestSpring { public static void main(String[] args) {
// TODO Auto-generated method stub
ApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "applicationContext.xml" }); ProductService s = (ProductService) context.getBean("Pservice");
s.doSomeService();
} }

第四步:准备日志切面 LoggerAspect

该日志切面的功能是 在调用核心功能之前和之后分别打印日志,切面就是原理图中讲的那些辅助功能。

 Object object = joinPoint.proceed();

这一句代码就是将来与某个核心功能编织之后,用于执行核心功能的代码

LoggerAspect.java

 package com.spring.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;
}
}

第五步:修改applicationContext.xml

1.声明业务对象

 <bean name="Pservice" class="com.spring.service.ProductService">
</bean>

2.声明日志切面

 <bean name="loggerAspect" class="com.spring.aspect.LoggerAspect">
</bean>

3.结合

  <1>指定右边的核心业务功能

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

  <2>指定左边的辅助功能

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

  <3>然后通过aop:config把业务对象与辅助功能编织在一起。

 <aop:config>
<aop:pointcut expression="execution(* com.spring.service.ProductService.*(..))" id="loggerCutpoint"/>
<aop:aspect id="logAspect" ref="loggerAspect">
<aop:around pointcut-ref="loggerCutpoint" method="log"/>
</aop:aspect>
</aop:config>

注:(注意“*”后面的空格)

execution(* com.spring.service.ProductService.*(..))

这表示对满足如下条件的方法调用,进行切面操作:

  *         返回任意类型

  com.spring.service.ProductService.*   

      包名以com.spring.service.ProductService 开头的类的任意方法
  (..)    参数是任意数量和类型

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:annotation-config/>-->
<bean name="category" class="com.spring.cate.Category">
<property name="name" value="category 3333" />
</bean>
<bean name="product" class="com.spring.cate.Product">
<property name="name" value="product 3333" />
<property name="category" ref="category" />
</bean>
<bean name="Pservice" class="com.spring.service.ProductService">
</bean> <bean name="loggerAspect" class="com.spring.aspect.LoggerAspect">
</bean>
<aop:config>
<aop:pointcut expression="execution(* com.spring.service.ProductService.*(..))" id="loggerCutpoint"/>
<aop:aspect id="logAspect" ref="loggerAspect">
<aop:around pointcut-ref="loggerCutpoint" method="log"/>
</aop:aspect>
</aop:config>
</beans>

第六步:TestSpring

TestSpring 代码没有发生任何变化,通过配置的方式,把切面和核心业务类编制在了一起。

运行测试,可以发现在编织之后,业务方法运行之前和之后分别会打印日志

TestSpring.java

 package com.spring.test;

 import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import com.spring.service.ProductService; public class TestSpring { public static void main(String[] args) {
// TODO Auto-generated method stub
ApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "applicationContext.xml" });
ProductService s = (ProductService) context.getBean("Pservice");
s.doSomeService();
} }

Spring学习笔记4——AOP的更多相关文章

  1. Spring学习笔记之aop动态代理(3)

    Spring学习笔记之aop动态代理(3) 1.0 静态代理模式的缺点: 1.在该系统中有多少的dao就的写多少的proxy,麻烦 2.如果目标接口有方法的改动,则proxy也需要改动. Person ...

  2. [Spring学习笔记 4 ] AOP 概念原理以及java动态代理

    一.Spring IoC容器补充(1) Spring IoC容器,DI(依赖注入): 注入的方式:设值方法注入setter(属性注入)/构造子注入(构造函数传入依赖的对象)/字段注入Field(注解) ...

  3. Spring学习笔记2—AOP

    1.AOP概念 AOP(Aspect Oriented Programming):面向切面编程,AOP能够将那些与业务无关,却为业务模块所共同调用的应用(例如事务处理.日志管理.权限控制等)封装起来, ...

  4. Spring学习笔记之AOP配置篇(一)

    [TOC] 1. 创建并声明一个切面 首先,创建一个类,添加@Component注解使其添加到IoC容器 然后,添加@Aspect注解,使其成为一个切面 最后,在配置文件里面,使用<aop:as ...

  5. spring学习笔记(一) Spring概述

    博主Spring学习笔记整理大部分内容来自Spring实战(第四版)这本书.  强烈建议新手购入或者需要电子书的留言. 在学习Spring之前,我们要了解这么几个问题:什么是Spring?Spring ...

  6. Java框架spring 学习笔记(十八):事务管理(xml配置文件管理)

    在Java框架spring 学习笔记(十八):事务操作中,有一个问题: package cn.service; import cn.dao.OrderDao; public class OrderSe ...

  7. 不错的Spring学习笔记(转)

    Spring学习笔记(1)----简单的实例 ---------------------------------   首先需要准备Spring包,可从官方网站上下载.   下载解压后,必须的两个包是s ...

  8. 【Spring学习笔记-MVC-5】利用spring MVC框架,实现ajax异步请求以及json数据的返回

    作者:ssslinppp      时间:2015年5月26日 15:32:51 1. 摘要 本文讲解如何利用spring MVC框架,实现ajax异步请求以及json数据的返回. Spring MV ...

  9. 【Spring学习笔记-MVC-3】SpringMVC返回Json数据-方式1

    <Spring学习笔记-MVC>系列文章,讲解返回json数据的文章共有3篇,分别为: [Spring学习笔记-MVC-3]SpringMVC返回Json数据-方式1:http://www ...

随机推荐

  1. CSS你所不知的伪元素的用法

    你所不知的 CSS ::before 和 ::after 伪元素用法 博客分类: Div / Css / XML / HTML5   CSS 有两个说不上常用的伪类 :before 和 :after, ...

  2. Mybatis-select-返回值类型错误理解

    Mybatis :Cause: java.lang.UnsupportedOperationException异常: 今天在写一个练手项目,作为初学Mybatis的小白,想着这里findByEmp_i ...

  3. VMware vCenter Server 6.5.0 U1

    VMware vCenter Server 6.5.0 U1gName: VMware-VCSA-all-6.5.0-8024368.iso Release Date: 2018-03-20 Buil ...

  4. Nginx+Tomcat高性能负载均衡集群搭建

    转载请注明出处:http://www.cnblogs.com/Joanna-Yan/p/8745794.html Nginx是一个高性能的HTTP服务器/反向代理服务器及电子邮件(IMAP/POP3) ...

  5. SiteMesh在项目中的配置

    SiteMesh在项目中的配置 首先在web.xml里面增加siteMesh的配置: <filter> <filter-name>sitemesh</filter-nam ...

  6. Docker学习笔记 - Docker的仓库

  7. Spring Security 入门(1-8)Spring Security 的配置文件举例

  8. android- 远程调试

    最近由于要在另外一台android设备上调试代码,在本机PC上查看其log.两台机器离的比较远, 无法用usb直接连接,于是在网上找了很多资料,最找使用adb connect方法解决了该问题.解决过程 ...

  9. maven环境变量的配置及+eclipse的配置使用

    1. 环境搭建(Maven+eclipse) 进入CMD 输入: mvn  –v   查看是否配置好 输入: mvn  -version 可以查看其安装的版本 在eclipse中配置maven: 在h ...

  10. spring mvc跨域(ajax post json)--filter方案

    @RequestMapping(value = "/login.do",method = RequestMethod.POST) public Message login(Http ...