SSM-Spring-03:Spring中AOP的初窥和入门小案例
------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥-------------
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的初窥和入门小案例的更多相关文章
- Spring中AOP的初窥和入门小案例
AOP:面向切面编程 AOP的主要作用:是为了程序员更好的关注"业务",专心"做事" 加上双引号的意思:所谓业务,是指他的核心,各行业中需要处理的核心事务,核心 ...
- spring boot入门小案例
spring boot 入门小案例搭建 (1) 在Eclipse中新建一个maven project项目,目录结构如下所示: cn.com.rxyb中存放spring boot的启动类,applica ...
- Spring和SpringMVC父子容器关系初窥
一.背景 最近由于项目的包扫描出现了问题,在解决问题的过程中,偶然发现了Spring和SpringMVC是有父子容器关系的,而且正是因为这个才往往会出现包扫描的问题,我们在此来分析和理解Spring和 ...
- Spring中AOP实现
1.什么是SpringAOP 什么是aop:Aspect Oriented Programming的缩写,面向切面编程,通过预编译和动态代理实现程序功能的 统一维护的一种技术 主要功能:日志记录,性能 ...
- 为啥Spring和Spring MVC包扫描要分开?
背景: 最近在搭建新工程的时候发现有些Spring的配置不是很了解,比如Spring 配置里面明明配置了component-scan,为啥Spring MVC配置文件还需要配置一下,这样岂 ...
- 为啥Spring和Spring MVC包扫描要分开
开始学习springmvc各种小白问题 根据例子配置了spring扫描包,但是一直提示404错误,经过大量搜索,发现,扫描包的配置应该写在springmvc的配置文件中,而不是springmvc 配置 ...
- 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 ...
- 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 ...
- Spring中AOP原理,源码学习笔记
一.AOP(面向切面编程):通过预编译和运行期动态代理的方式在不改变代码的情况下给程序动态的添加一些功能.利用AOP可以对应用程序的各个部分进行隔离,在Spring中AOP主要用来分离业务逻辑和系统级 ...
随机推荐
- Jumpstart for Oracle Service Bus Development
http://www.oracle.com/technetwork/articles/jumpstart-for-osb-development-page--097357.html Tutorial ...
- linux内核中的C语言常规算法(前提:你的编译器要支持typeof和type)
学过C语言的伙伴都知道,曾经比较两个数,输出最大或最小的一个,或者是比较三个数,输出最大或者最小的那个,又或是两个数交换,又或是绝对值等等,其实这些算法在linux内核中通通都有实现,以下的代码是我从 ...
- Learning ROS for Robotics Programming Second Edition学习笔记(三) 补充 hector_slam
中文译著已经出版,详情请参考:http://blog.csdn.net/ZhangRelay/article/category/6506865 Learning ROS for Robotics Pr ...
- LeetCode之“字符串”:ZigZag Conversion
题目链接 题目要求: The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of ...
- RTMPdump(libRTMP) 源代码分析 6: 建立一个流媒体连接 (NetStream部分 1)
===================================================== RTMPdump(libRTMP) 源代码分析系列文章: RTMPdump 源代码分析 1: ...
- SharePoint 门户网站的图片轮播-页面定制
这个想法是自己突然的一个想法,想想我们经常用SharePoint做门户网站,不知道你们多数项目都是怎么完成的,我们客户要求的效果都还是很严格的,所有展现起来,还是很漂亮的,但是很多时候的效果,还是难以 ...
- mybatis ----数据级联查询(多对一)
工程的目录结构: 有两个表,一个文章表article ,一个用户表user. create table article (id int(11) not null auto_increment, use ...
- java中文拼音字母排序
package com.yputil.util; import java.text.CollationKey;import java.text.Collator;import java.util.Ar ...
- 01_Linux学习_基础知识
学Linux就学命令行 === cd / 转到根目录,相对当前路径 cd dev 转到dev目录 whoami 查阅当前用户 pwd 查阅当前目录 ls 查阅当前目录下的目录和文件 === Linux ...
- 下载Github上某个项目的子文件夹和单个文件
preface Github下的项目可能很大,里面有很多的子文件夹,我们可能只需要使用某个子目录下的资源,可以不用下载完整的repo就能使用. 例如,我想下载这个repo中的字典文件:https:// ...