------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥-------------

AOP:面向切面编程

  AOP的主要作用:是为了程序员更好的关注"业务",专心"做事"

    加上双引号的意思:所谓业务,是指他的核心,各行业中需要处理的核心事务,核心啊

    像日志的记录,事务的管理,权限分配等这些交叉业务,同一个项目中使用多次,直接提取出来成为公共的比较好,再用面向切面的方式,进行代码的编辑,业务的实现

  AOP的思想:

    

    正如上图所示的,最基本的方式实现的业务和AOP方式实现的业务,是不同的,或许你会说,我在没有aop的时候,我也不像上面那个最low的方式一样啊,是,有比那个最low的方式好的多的是,我们要学习新的思想,aop,面向切面编程

  AOP的原理

    

--------------------------------------------------------------------------------------------------------------------------------

入门案例:

  用最基本的方式模拟一道日志的记录和最后执行完业务的操作

    DAO层(一个接口,一个他的实现类,模拟操作修改数据库)

package cn.dawn.day04aop.dao;

/**
* Created by Dawn on 2018/3/5.
*/
/*dao层接口*/
public interface IHellowDAO {
/*aop入门案例*/
public void doSome();
} package cn.dawn.day04aop.dao.impl; import cn.dawn.day04aop.dao.IHellowDAO; /**
* Created by Dawn on 2018/3/5.
*/
/*dao层实现类*/
public class HellowDAOImpl implements IHellowDAO{ public void doSome() {
System.out.println("数据已经成功写入到DB");
}
}

    service层(也是一个接口,一个实现类,主要做的aop的增强操作,操作的是service层,业务逻辑处理层操作的)

package cn.dawn.day04aop.service;

/**
* Created by Dawn on 2018/3/5.
*/
/*service层接口*/
public interface IHellowService {
/*aop入门案例*/
public void doSome();
} package cn.dawn.day04aop.service.impl; import cn.dawn.day04aop.dao.IHellowDAO;
import cn.dawn.day04aop.service.IHellowService; /**
* Created by Dawn on 2018/3/5.
*/
/*service层实现类 */
public class HellowServiceImpl implements IHellowService {
IHellowDAO dao;
public void doSome() {
dao.doSome();
} public IHellowDAO getDao() {
return dao;
} public void setDao(IHellowDAO dao) {
this.dao = dao;
}
}

    新开多的一层,叫aop层,他就存放了增强的操作(也就是交叉业务,例如日志记录等),此处我放了俩个类,一个执行前置增强,一个后置增强

package cn.dawn.day04aop.aop;

import org.springframework.aop.MethodBeforeAdvice;

import java.lang.reflect.Method;

/**
* Created by Dawn on 2018/3/5.
*/
/*前置增强*/
public class LoggerBefore implements MethodBeforeAdvice {
public void before(Method method, Object[] objects, Object o) throws Throwable {
System.out.println("日志记录");
}
} package cn.dawn.day04aop.aop; import org.springframework.aop.AfterReturningAdvice; import java.lang.reflect.Method; /**
* Created by Dawn on 2018/3/5.
*/
/*后置增强*/
public class LoggerAfter implements AfterReturningAdvice {
public void afterReturning(Object o, Method method, Object[] objects, Object o1) throws Throwable {
System.out.println("===============after==================");
}
}

    前置增强,需要实现MethodBeforeAdvice,后置增强,需要实现AfterReturningAdvice

    接下来就是书写大配置xml文件

      有个注意的点,由于我用的idea,他会自动生成上面的beans的 xmlns和xsi,如果你不是用idea的话,手动配置一道吧

<?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"
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"> <!--aop入门案例起-->
<!--dao-->
<bean id="dao" class="cn.dawn.day04aop.dao.impl.HellowDAOImpl"></bean>
<!--service-->
<bean id="service" class="cn.dawn.day04aop.service.impl.HellowServiceImpl">
<property name="dao" ref="dao"></property>
</bean>
<!--通知-->
<bean id="afterAdvice" class="cn.dawn.day04aop.aop.LoggerAfter"></bean>
<bean id="beforeAdvice" class="cn.dawn.day04aop.aop.LoggerBefore"></bean>
<!--aop-->
<aop:config>
<!--切点-->
<aop:pointcut id="mypointcut" expression="execution(* *..service.*.*(..))"></aop:pointcut>
<!--<aop:pointcut id="mypointcut" expression="execution(public void cn.dawn.day04aop.service.IHellowService.doSome())"></aop:pointcut>-->
<!--<aop:pointcut id="mypointcut" expression="execution(* *..service.*.*(..))">-->
<!--顾问,织入-->
<aop:advisor advice-ref="beforeAdvice" pointcut-ref="mypointcut"></aop:advisor>
<aop:advisor advice-ref="afterAdvice" pointcut-ref="mypointcut"></aop:advisor>
</aop:config>
<!--aop入门案例完毕-->
</beans>

    这儿有一个切点pointcut,我说一下他的expression的属性吧,他就是里面放一个匹配的(可以说叫公式?)方法的公式

    他的使用规则我放在下面

              

    接下来单测方法

package cn.dawn.day04aop;

import cn.dawn.day03printer.printer.Printer;
import cn.dawn.day04aop.service.IHellowService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; /**
* Created by Dawn on 2018/3/3.
*/
public class test20180305 {
@Test
/*aop入门案例*/
public void t01(){
ApplicationContext context=new ClassPathXmlApplicationContext("ApplicationContext-day04aop.xml");
IHellowService service = (IHellowService) context.getBean("service");
service.doSome();
}
}

    运行结果如下:

SSM-Spring-03:Spring中AOP的初窥和入门小案例的更多相关文章

  1. Spring中AOP的初窥和入门小案例

    AOP:面向切面编程 AOP的主要作用:是为了程序员更好的关注"业务",专心"做事" 加上双引号的意思:所谓业务,是指他的核心,各行业中需要处理的核心事务,核心 ...

  2. spring boot入门小案例

    spring boot 入门小案例搭建 (1) 在Eclipse中新建一个maven project项目,目录结构如下所示: cn.com.rxyb中存放spring boot的启动类,applica ...

  3. Spring和SpringMVC父子容器关系初窥

    一.背景 最近由于项目的包扫描出现了问题,在解决问题的过程中,偶然发现了Spring和SpringMVC是有父子容器关系的,而且正是因为这个才往往会出现包扫描的问题,我们在此来分析和理解Spring和 ...

  4. Spring中AOP实现

    1.什么是SpringAOP 什么是aop:Aspect Oriented Programming的缩写,面向切面编程,通过预编译和动态代理实现程序功能的 统一维护的一种技术 主要功能:日志记录,性能 ...

  5. 为啥Spring和Spring MVC包扫描要分开?

    背景:       最近在搭建新工程的时候发现有些Spring的配置不是很了解,比如Spring 配置里面明明配置了component-scan,为啥Spring MVC配置文件还需要配置一下,这样岂 ...

  6. 为啥Spring和Spring MVC包扫描要分开

    开始学习springmvc各种小白问题 根据例子配置了spring扫描包,但是一直提示404错误,经过大量搜索,发现,扫描包的配置应该写在springmvc的配置文件中,而不是springmvc 配置 ...

  7. DUBBO+Zookeeper在Centos7中本地搭建及小案例

    环境: 1.centos7 2.jdk-7u76-linux-x64.tar.gz 2.tomcat:apache-tomcat-7.0.59.tar.gz 3.zookeeper-3.4.6.tar ...

  8. 04 Spring:01.Spring框架简介&&02.程序间耦合&&03.Spring的 IOC 和 DI&&08.面向切面编程 AOP&&10.Spring中事务控制

    spring共四天 第一天:spring框架的概述以及spring中基于XML的IOC配置 第二天:spring中基于注解的IOC和ioc的案例 第三天:spring中的aop和基于XML以及注解的A ...

  9. Spring中AOP原理,源码学习笔记

    一.AOP(面向切面编程):通过预编译和运行期动态代理的方式在不改变代码的情况下给程序动态的添加一些功能.利用AOP可以对应用程序的各个部分进行隔离,在Spring中AOP主要用来分离业务逻辑和系统级 ...

随机推荐

  1. Cocos2D粒子发射器的纹理

    每个例子发射器只能使用单个纹理发射粒子. 如果你需要在相同粒子效果中组合多重纹理,你将不得不创建多重的发射器节点并且决定谁的粒子将在其它粒子之上或之下显示.

  2. Ubuntu ROS Arduino Gazebo学习镜像iso说明(indigo版)

    ROS机器人程序设计(原书第2版)学习镜像分享及使用说明 新版已经发布,请参考: http://blog.csdn.net/zhangrelay/article/details/53324759 Ub ...

  3. ViewPager切换动画PageTransformer的使用

    Android从3.0开始添加了属性动画后,诸多难以实现的动画都可以轻松解决了,v4包下的ViewPager控件当然也不例外,相对于非常平庸的默认切换动画,Google官方给我们展示了两个动画例子:D ...

  4. Android NFC开发(二)——Android世界里的NFC所具备的条件以及使用方法

    Android NFC开发(二)--Android世界里的NFC所具备的条件以及使用方法 NFC的应用比较广泛,而且知识面也是比较广的,所以就多啰嗦了几句,我还还是得跟着官方文档:http://dev ...

  5. Linux权限与命令间的关系

    极重要!权限与命令间的关系: 我们知道权限对於使用者帐号来说是非常重要的,因为他可以限制使用者能不能读取/创建/删除/修改文件或目录! 在这一章我们介绍了很多文件系统的管理命令,第六章则介绍了很多文件 ...

  6. 【Android 应用开发】BluetoothClass详解

    一. BluetoothClass简介 1. 继承关系 public final class BluetoothClass extends Object implements Parcelable 该 ...

  7. PS 滤镜算法原理——浮雕效果

    clc; clear all; Image=imread('4.jpg');Image=double(Image);p=3;  %% 控制浮雕的强度 %% 控制浮雕的方向 H=[0 0 p      ...

  8. MurmurHash

    public int hash(byte[] data, int length, int seed) {     int m = 0x5bd1e995;     int r = 24;     int ...

  9. [Zabbix3.0] 添加MySQL监控

    zabbix3.0 server已经自带MySQL的模板了,只要修改agent端,然在web端给主机添加模板就好了. Agent端操作 /etc/zabbix/zabbix_agentd.d/user ...

  10. 熊猫猪新系统测试之二:Mac OS X 10.10 优胜美地

    在第一篇windows 10技术预览版测试之后,本猫为大家呈现另一个刚刚才更新的mac操作系统:"优胜美地".苹果同样一改以猫科动物为代号命名的传统,在10.9的Mavericks ...