Spring笔记2
Bean生命周期
1 实例化
2 注入属性
3 BeanNameAware
4 BeanFactoryAware
5 ApplicationContextAware
6 BeanPostProcessor,ProcessBeforeInitialization
7 Initilalization
8 BeanPostProcessor,ProcessAfterInitialization
9 以上工作完成后就可以使用bean
10 DisposableBean destroy(或者配置destroy-method)
https://www.cnblogs.com/kenshinobiy/p/4652008.html
AOP
AOP(Aspect Oriented Programming),通常称为面向切面编程。它利用一种称为"横切"的技术,剖解开封装的对象内部,并将那些影响了多个类的公共行为封装到一个可重用模块,并将其命名为"Aspect",即切面。所谓"切面",简单说就是那些与业务无关,却为业务模块所共同调用的逻辑或责任封装起来,便于减少系统的重复代码,降低模块之间的耦合度,并有利于未来的可操作性和可维护性。
AOP 关键术语
- 目标类target:就是我们需要增强的那个类,target
- 代理类proxy:就是我们自定义的那个代理的对象proxy
- 连接点joinPoint:连接点说白了就是我们的目标类里面所有的方法。
- 切入点pointCut:切入点说白了就是那些在目标类中我们实际上增强的方法。
- 织入weave:说白了就是将我们的代理类中需要增强的方法放入到目标类中去执行的过程,就叫织入
- 引介Introduction:是对我们的类中的方法或者属性进行一些创建的过程
- 通知advice:说白了就是将我们代理对象中的方法应用到目标类的过程中产生的结果。
- 切面aspect:说白了就是我们的所有的连接点和代理对象的方法组成在一起构成了我们的切面
jdk的代理方式来进行增强
public class CustomerServiceImpl implements CustomerService {
@Override
public void rent() {
System.out.println("I want to rent my house");
}
@Override
public void hello(String str) {
System.out.println("hello: " + str);
}
}
/*Proxy.newProxyInstance(loader, interfaces, h);
loader:一个ClassLoader对象,定义了由哪个ClassLoader对象来对生成的代理对象进行加载
interfaces:一个Interface对象的数组,表示的是我将要给我需要代理的对象提供一组什么接口,如果我提供了一组接口给它,那么这个代理对象就宣称实现了该接口(多态),这样我就能调用这组接口中的方法了
h:一个InvocationHandler对象,表示的是当我这个动态代理对象在调用方法的时候,会关联到哪一个InvocationHandler对象上*/
/*public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {}
proxy:指代我们所代理的那个真实对象
method:指代的是我们所要调用真实对象的某个方法的Method对象
args:指代的是调用真实对象某个方法时接受的参数*/
public class CustomerProxy implements InvocationHandler {
private CustomerService customerService;
public CustomerProxy(CustomerService customerService){
this.customerService=customerService;
}
public CustomerService createCustomerJdkProxy(){
Object newProxyInstance = Proxy.newProxyInstance(customerService.getClass().getClassLoader(),
customerService.getClass().getInterfaces(), this);
return (CustomerService)newProxyInstance;
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
System.out.println("Method:" + method.getName());
if(method.getName().equals("rent")){
//在代理真实对象前我们可以添加一些自己的操作
System.out.println("before rent house");
Object invoke = method.invoke(customerService, args);
//在代理真实对象后我们也可以添加一些自己的操作
System.out.println("after rent house");
return invoke;
}
return method.invoke(customerService, args);
}
}
<bean id="customerService" class="com.itheima.spring.demo3.jdkproxy.CustomerServiceImpl"></bean>
public class JdkProxy {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
CustomerService customerService= (CustomerService)context.getBean("customerService");
CustomerService jdkProxy = new CustomerProxy(customerService).createCustomerJdkProxy();
jdkProxy.rent();
jdkProxy.hello("World");
}
}
cglib的代理方式来进行增强
public class CustomerServiceImpl {
public void rent() {
System.out.println("I want to rent my house");
}
public void hello(String str) {
System.out.println("hello: " + str);
}
}
public class CustomerProxy implements MethodInterceptor{
private CustomerServiceImpl customerServiceImpl;
public CustomerProxy(CustomerServiceImpl customerServiceImpl) {
this.customerServiceImpl = customerServiceImpl;
}
public CustomerServiceImpl createCustomerCglibProxy(){
//1.创建Enhancer对象
Enhancer enhancer = new Enhancer();
//2.设置需要增强的父类
enhancer.setSuperclass(customerServiceImpl.getClass());
//3.设置回调
enhancer.setCallback(this);
//4.执行代理类
Object create = enhancer.create();
return (CustomerServiceImpl)create;
}
@Override
public Object intercept(Object proxy, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {
System.out.println("Method:" + method.getName());
if(method.getName().equals("rent")){
//在代理真实对象前我们可以添加一些自己的操作
System.out.println("before rent house");
Object invokeSuper = methodProxy.invokeSuper(proxy, args);
//在代理真实对象后我们也可以添加一些自己的操作
System.out.println("after rent house");
return invokeSuper;
}
return methodProxy.invokeSuper(proxy, args);
}
}
public class CglibProxy {
@Test
public void testCglibProxy(){
CustomerServiceImpl customerServiceImpl = new CustomerServiceImpl();
CustomerServiceImpl cglibProxy = new CustomerProxy(customerServiceImpl).createCustomerCglibProxy();
cglibProxy.rent();
cglibProxy.hello("World");
}
}
AOP基于xml配置的方式实现
public class CustomerServiceImpl {
public void rent() {
System.out.println("I want to rent my house");
}
public void hello(String str) {
System.out.println("hello: " + str);
}
}
public class CustomerProxy {
public void strongMethod(){
System.out.println("$100 rent house");
}
}
<bean id="customerService" class="com.itheima.spring.demo3.xml.CustomerServiceImpl"></bean>
<bean id="customerProxyAspect" class="com.itheima.spring.demo3.xml.CustomerProxy"></bean>
<aop:config>
<aop:pointcut expression="execution(* com.itheima.spring.demo3.xml.*.rent*(..))" id="pointcut1"/>
<aop:aspect ref="customerProxyAspect">
<aop:after-returning method="strongMethod" pointcut-ref="pointcut1" />
</aop:aspect>
</aop:config>
public class XmlAspect {
@Test
public void testXmlAspect(){
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
CustomerService bean= (CustomerService)context.getBean("customerService");
bean.rent();
bean.hello("World");
}
}
AOP基于注解的方式实现
@Service
public class CustomerServiceImpl implements CustomerService {
@Override
public String rent() {
System.out.println("I want to rent my house");
return "张三";
}
@Override
public void hello(String str) {
System.out.println("hello: " + str);
}
}
@Component
@Aspect
public class CustomerProxy {
@AfterReturning(value="execution(* com.itheima.spring.demo3.xml.*.rent*(..))",returning="object")
public void strongMethod(Object object){
System.out.println(object + " $100 rent house");
}
}
<context:component-scan base-package="com.itheima.spring.demo3.xml"></context:component-scan>
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
public class XmlAspect {
@Test
public void testXmlAspect(){
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
CustomerService bean= (CustomerService)context.getBean("customerServiceImpl");
bean.rent();
bean.hello("World");
}
}
Spring 切面可以应用五种类型的通知:
- before:前置通知,在一个方法执行前被调用。
- after: 在方法执行之后调用的通知,无论方法执行是否成功。
- after-returning: 仅当方法成功完成后执行的通知。
- after-throwing: 在方法抛出异常退出时执行的通知。
- around: 在方法执行之前和之后调用的通知。
Spring笔记2的更多相关文章
- Spring笔记02_注解_IOC
目录 Spring笔记02 1. Spring整合连接池 1.1 Spring整合C3P0 1.2 Spring整合DBCP 1.3 最终版 2. 基于注解的IOC配置 2.1 导包 2.2 配置文件 ...
- Spring笔记01_下载_概述_监听器
目录 Spring笔记01 1.Spring介绍 1.1 Spring概述 1.2 Spring好处 1.3 Spring结构体系 1.4 在项目中的架构 1.5 程序的耦合和解耦 2. Spring ...
- Spring 笔记 -06- 从 MySQL 建库到 登录验证数据库信息(maven)
Spring 笔记 -06- 从 MySQL 建库到 登录验证数据库信息(maven) 本篇和 Spring 没有什么关系,只是学习 Spring,必备一些知识,所以放在这里了. 本篇内容: (1)M ...
- Spring笔记:事务管理
Spring笔记:事务管理 事务管理 Spring事务管理是通过SpringAOP去实现的.默认情况下Spring在执行方法抛出异常后,引发事务回顾,当然你可以用拦截器或者配置去改变它们. 这部门内容 ...
- Spring笔记:AOP基础
Spring笔记:AOP基础 AOP 引入AOP 面向对象的开发过程中,我们对软件开发进行抽象.分割成各个模块或对象.例如,我们对API抽象成三个模块,Controller.Service.Comma ...
- Spring:笔记整理(1)——HelloWorld
Spring:笔记整理(1)——HelloWorld 导入JAR包: 核心Jar包 Jar包解释 Spring-core 这个jar 文件包含Spring 框架基本的核心工具类.Spring 其它组件 ...
- Spring笔记:IOC基础
Spring笔记:IOC基础 引入IOC 在Java基础中,我们往往使用常见关键字来完成服务对象的创建.举个例子我们有很多U盘,有金士顿的(KingstonUSBDisk)的.闪迪的(SanUSBDi ...
- Spring笔记(6) - Spring的BeanFactoryPostProcessor探究
一.背景 在说BeanFactoryPostProcessor之前,先来说下BeanPostProcessor,在前文Spring笔记(2) - 生命周期/属性赋值/自动装配及部分源码解析中讲解了Be ...
- spring笔记----看书笔记
上周末看了一章以前javaee轻量级的书spring部分,简单做了一些笔记 // ApplicationContext ac=new ClassPathXmlApplicationContext(&q ...
- Spring 笔记(三)Bean 装配
前言 Spring 有两大核心,也就分成两份笔记分别记录. 其一是管理应用中对象之间的协作关系,实现方式是依赖注入(DI),注入依赖的过程也被称为装配(Wiring). 基于 JavaConfig 的 ...
随机推荐
- javascript实现数据结构: 树和二叉树的应用--最优二叉树(赫夫曼树),回溯法与树的遍历--求集合幂集及八皇后问题
赫夫曼树及其应用 赫夫曼(Huffman)树又称最优树,是一类带权路径长度最短的树,有着广泛的应用. 最优二叉树(Huffman树) 1 基本概念 ① 结点路径:从树中一个结点到另一个结点的之间的分支 ...
- STROME --realtime & online parallel computing
Data Collections ---> Stream to Channel (as source input) ----> Parallel Computing---> Resu ...
- java面试题之----数据库事务的四大特性及隔离级别
本篇讲诉数据库中事务的四大特性(ACID),并且将会详细地说明事务的隔离级别. 如果一个数据库声称支持事务的操作,那么该数据库必须要具备以下四个特性: ⑴ 原子性(Atomicity) 原子性是指事务 ...
- 解决Image在canvas以及audio、video在AudioContext下跨域问题
媒体元素嘛,在对应的标签或对象上加上 crossOrigin = 'anonymous' 即可. 例如 Image 在canvas 上绘制时会跨域: <img crossOrigin=" ...
- css渲染(三)颜色与背景
颜色的应用主要分为前景色.背景色和透明三个部分. 一.前景色 color color前景色 值: <color> | inherit 初始值: 用户代理特定的值 应用于: 所有元素 继承性 ...
- 从产品展示页面谈谈Hybris系列之二: DTO, Converter和Populator
文章作者:张健(Zhang Jonathan) 上一篇文章 从产品展示页面谈谈Hybris的特有概念和设计结构 我们讲解了Hybris一些特有的概念以及大体架构,并且介绍了Facade层里是如何定义D ...
- selenium使用js进行点击
WebElement button = driver.findElement(By.xpath("/html/body/div[1]/div[3]/h2/div[2]")); Ja ...
- 郑州Day6
今天考了毕姥爷的一套题,差点保龄 题目 挺良心的一套题,至少我不用再搬一遍题面了 T1.B君的第一题 我为什么当时去写了一个树形\(dp\)还妄图\(A\)掉啊 这题保龄感觉舒爽 首先如果我们要求的是 ...
- 【转】Mac本地生成SSH Key 的方法
1. 查看秘钥是否存在 打开终端查看是否已经存在SSH密钥:cd ~/.ssh 如果没有密钥则不会有此文件夹,有则备份删除, 也可以直接删除, 2.生成新的秘钥, 命令如下 $ssh-keygen ...
- ROS编译工作区缺少cv_bridge的问题解决
cv_bridge是OpenCV与ROS之间的格式转换桥梁,编译工作区时遇到报错(目标不存在),直接将cv_bridge包复制到指定的目录即可. 下载地址:https://github.com/ros ...