Spring中AOP的初窥和入门小案例
AOP:面向切面编程
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();
}
}
运行结果如下:

04.Spring的DI的构造注入,P命名注入,和集合注入
DI和IOC相比,DI更偏向于实现
DI的set方式注入在前面入门案例里有写,所以此处不多啰嗦,直接开搞,先说构造注入和P命名注入
构造方式,理所当然要有带参构造,这儿值得注意的是,你最好再补全一个无参构造,因为你写了带参构造,系统就不再会为你默认补全一个无参构造了,当你在不经意或者不知情的情况下被调用了,就会报错
P命名则有注意的是那个头文件 xmlns xsi需要你去配置一道,我下面有,你直接copy就可以
package cn.dawn.day05diup;
/**
* Created by Dawn on 2018/3/5.
*/public class Car {
private String color;
private String type; public String getColor() {
return color;
} public void setColor(String color) {
this.color = color;
} public String getType() {
return type;
} public void setType(String type) {
this.type = type;
}
}
package cn.dawn.day05diup;
/**
* Created by Dawn on 2018/3/5.
*///student类public class Student {
private String name;
private Integer age;
private Car car; //带参构造
public Student(String name, Integer age, Car car) {
this.name = name;
this.age = age;
this.car = car;
} //无参构造
public Student() {
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public Integer getAge() {
return age;
} public void setAge(Integer age) {
this.age = age;
} public Car getCar() {
return car;
} public void setCar(Car car) {
this.car = car;
}
}
在大配置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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd "> <bean id="car" class="cn.dawn.day05diup.Car">
<property name="color" value="黑色"></property>
<property name="type" value="奥迪"></property>
</bean>
<!--di构造注入-->
<!--<bean id="student" class="cn.dawn.day05diup.Student">
<constructor-arg index="0" value="孟六"></constructor-arg>
<constructor-arg index="1" value="20"></constructor-arg>
<constructor-arg index="2" ref="car"></constructor-arg>
</bean>--> <!--p命名注入-->
<bean id="student" class="cn.dawn.day05diup.Student" p:name="孟小六" p:age="8" p:car-ref="car"></bean> </beans>
没有什么好讲的,带参构造的注入方法,index索引从0开始,对应的是那个带参构造的index
单测方法:
@Test
/*diP命名注入*/
public void t02(){
ApplicationContext context=new ClassPathXmlApplicationContext("ApplicationContext-day05diup.xml");
Student student = (Student) context.getBean("student");
System.out.println("学生"+student.getName()+"开着"+student.getCar().getColor()+"的"+student.getCar().getType());
} @Test
/*di构造注入*/
public void t01(){
ApplicationContext context=new ClassPathXmlApplicationContext("ApplicationContext-day05diup.xml");
Student student = (Student) context.getBean("student");
System.out.println("学生"+student.getName()+"开着"+student.getCar().getColor()+"的"+student.getCar().getType());
}
集合注入:
数组,List,Set,Map,Properties
实体类
package cn.dawn.day05diup; import java.util.*;
/**
* Created by Dawn on 2018/3/5.
*/public class MyCollection {
private String[] array;
private List<String> list;
private Set<String> set;
private Map<String,String> map;
private Properties properties; @Override
public String toString() {
return "MyCollection{" +
"array=" + Arrays.toString(array) +
", list=" + list +
", set=" + set +
", map=" + map +
", properties=" + properties +
'}';
} public String[] getArray() {
return array;
} public void setArray(String[] array) {
this.array = array;
} public List<String> getList() {
return list;
} public void setList(List<String> list) {
this.list = list;
} public Set<String> getSet() {
return set;
} public void setSet(Set<String> set) {
this.set = set;
} public Map<String, String> getMap() {
return map;
} public void setMap(Map<String, String> map) {
this.map = map;
} public Properties getProperties() {
return properties;
} public void setProperties(Properties properties) {
this.properties = properties;
}
}
大配置中的bean节点
<!--di的集合注入-->
<bean id="mycollection" class="cn.dawn.day05diup.MyCollection">
<!--数组注入-->
<property name="array">
<array>
<value>孟六</value>
<value>孟六十六</value>
<value>孟六百六十六</value>
</array>
</property>
<!--list集合注入-->
<property name="list">
<list>
<value>奥迪</value>
<value>奥小迪</value>
<value>奥迪迪</value>
</list>
</property>
<!--set集合注入-->
<property name="set">
<set>
<value>set1</value>
<value>set2</value>
<value>set3</value>
</set>
</property>
<!--map集合注入-->
<property name="map">
<map>
<entry key="姓名">
<value>孟五</value>
</entry>
<entry key="年龄">
<value>555</value>
</entry>
</map>
</property>
<!--properties-->
<property name="properties">
<props>
<prop key="key1">v1</prop>
<prop key="key2">v2</prop>
<prop key="key3">v3</prop>
</props>
</property>
</bean>
单测
@Test
/*di集合注入*/
public void t03(){
ApplicationContext context=new ClassPathXmlApplicationContext("ApplicationContext-day05diup.xml");
MyCollection mycollection = (MyCollection) context.getBean("mycollection");
System.out.println(mycollection);
}
由于重写了toString,可以直接一览无余
这里我扩充一下 java 中的tostring();
toString()方法 相信大家都用到过,一般用于以字符串的形式返回对象的相关数据。
翻译一下官方解释:
1、返回一个对于这个Object 简明的、可读的 的字符串
2、Object类的子类被鼓励去重写这个方法来提供一个实现用于描述对象的类型和数据
自己的理解
写这个方法的用途就是为了方便操作,所以在文件操作里面可用可不用
就是一个类中有集合 当你输出这个集合时它会以

这种可以直接换算成字符串 十分的方便
如果没有 toString() 将会打印出来内存地址

更方便咱们程序员
Spring中AOP的初窥和入门小案例的更多相关文章
- SSM-Spring-03:Spring中AOP的初窥和入门小案例
------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥------------- AOP:面向切面编程 AOP的主要作用:是为了程序员更好的关注"业务",专心"做 ...
- Spring中AOP原理,源码学习笔记
一.AOP(面向切面编程):通过预编译和运行期动态代理的方式在不改变代码的情况下给程序动态的添加一些功能.利用AOP可以对应用程序的各个部分进行隔离,在Spring中AOP主要用来分离业务逻辑和系统级 ...
- Spring中AOP简介与切面编程的使用
Spring中AOP简介与使用 什么是AOP? Aspect Oriented Programming(AOP),多译作 "面向切面编程",也就是说,对一段程序,从侧面插入,进行操 ...
- 框架源码系列十:Spring AOP(AOP的核心概念回顾、Spring中AOP的用法、Spring AOP 源码学习)
一.AOP的核心概念回顾 https://docs.spring.io/spring/docs/5.1.3.RELEASE/spring-framework-reference/core.html#a ...
- Spring 中aop切面注解实现
spring中aop的注解实现方式简单实例 上篇中我们讲到spring的xml实现,这里我们讲讲使用注解如何实现aop呢.前面已经讲过aop的简单理解了,这里就不在赘述了. 注解方式实现aop我们 ...
- Spring中AOP相关源码解析
前言 在Spring中AOP是我们使用的非常频繁的一个特性.通过AOP我们可以补足一些面向对象编程中不足或难以实现的部分. AOP 前置理论 首先在学习源码之前我们需要了解关于AOP的相关概念如切点切 ...
- AOP 与 Spring中AOP使用(上)
AOP简介 在软件业,AOP为Aspect Oriented Programming的缩写,意为:面向切面编程, 通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术. AOP是OOP的延续 ...
- JAVA高级架构师基础功:Spring中AOP的两种代理方式:动态代理和CGLIB详解
在spring框架中使用了两种代理方式: 1.JDK自带的动态代理. 2.Spring框架自己提供的CGLIB的方式. 这两种也是Spring框架核心AOP的基础. 在详细讲解上述提到的动态代理和CG ...
- 浅析Spring中AOP的实现原理——动态代理
一.前言 最近在复习Spring的相关内容,刚刚大致研究了一下Spring中,AOP的实现原理.这篇博客就来简单地聊一聊Spring的AOP是如何实现的,并通过一个简单的测试用例来验证一下.废话不 ...
随机推荐
- 学习 Unix 常用命令
第一个是 man 命令,作用是:"Display system documentation",我是 manual 的缩写.通过这个命令,我们能了解接下来要学习的命令的文档. ls, ...
- 使用国内源(brew, pip, npm)
如果网络不行,安装依赖包的速度小于 100k/s 或者丢包严重导致安装很慢,我认为就应该使用国内源了.今天因为国内源的问题弄了很久,我觉得在国内服务器部署,全部应该从国内下载资源.分为两种情况, 存在 ...
- TFS二次开发-基线文件管理器(5)-源码文件的读取
在上一节中,我们在保存标签之前,已经将勾选的文件路径保存到了Listbox中,这里只需要将保存的数据输出去为txt文档就可以做版本控制了. 版本文件比较复杂的是如何读取,也就是如何通过文件路径 ...
- 基于Mesos和Docker的分布式计算平台
基于Mesos和Docker的分布式计算平台 http://www.csdn.net/article/2015-06-09/2824906
- 前端之 JS 实现全选、反选、取消选中
需求 制作如下可选表格,实现“全选”.“反选”.“取消”功能 代码示例 <!DOCTYPE html> <html lang="zh-CN"> <he ...
- Java性能监控之javassist探索
https://www.cnblogs.com/orionhp/p/6362615.html ApplicationContext实现的默认行为就是在启动时将所有singleton bean提前进行实 ...
- 002-基本业务搭建【日志,工具类dbutils,dbcp等使用】
一.需求分析 1.1.概述 1.用户进入“客户管理”,通过列表方式查看用户: 2.客户名称,模糊查询用户列表 3.客户名称,可查看客户详细信息 4.新增.编辑.删除功能等 二.系统设计 需要对原始需求 ...
- 中文价格识别为数字 java代码
运行效果: public class VoicePriceRecognition { private final static String NOT_HAS_PRICE_CONTENT="n ...
- virt-viewer的简单使用
virt-viewer 简介: virt-viewer是一个用于显示虚拟机的图形控制台的最小工具. 控制台使用VNC或SPICE访问协议. 可以基于其名称,ID或UUID来引用guest虚拟机.如果 ...
- 2015.7.7——跌停板做T,就算要搏跌停反弹也要看卖一档压力啊
1.今天中色股份和以往不同买卖盘为正!————今天要重新观察这种新常态下的盘面运作 是否会出现如2015.7.6中描述的“前期错过了皮城中的黄金买点,...其实只要买卖盘为负,后期还会有再次尝试该低点 ...