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 的 ...
随机推荐
- 正则表达式备忘录-Regular Expressions Cheatsheet中文版
正则表达式备忘录Regular Expressions Cheatsheet中文版原文:https://www.maketecheasier.com/cheatsheet/regex/ 测试文件a.t ...
- AngularJS Toaster的简单介绍
github地址: https://github.com/jirikavi/AngularJS-Toaster 参考博客: https://www.cnblogs.com/youzhuxiaoyao/ ...
- Network Embedding 论文小览
Network Embedding 论文小览 转自:http://blog.csdn.net/Dark_Scope/article/details/74279582,感谢分享! 自从word2vec横 ...
- matlab练习程序(模糊集图像增强)
算法有很多变种.不过主要就是以下三步. 1.设计隶属度函数将图像从空间域变换到模糊集域. 2.设计模糊增强算子,在模糊集域对图像进行处理. 3.根据第1步的隶属度函数重新将图像从模糊集域变换到空间域. ...
- JSTL核心库
1 out和set(重点) out <c:out value=”aaa”/> 输出aaa字符串常量 <c:out value=”${aaa}”/> 与${aaa}相同 < ...
- 【邀请函】小投入 大产出—微软智能云(Azure)之CDN 专题
会议时间 2016 年 11 月 24 日 14:00-16:00 会议介绍 尊敬的客户: 您是否还在为如何提高网站的响应速度以及用户增长造成的源站压力烦恼?是否还在担心源站 IP 暴露存在安全隐患? ...
- Oracle的sql语句的两种判断形式
Oracle的sql语句的两种判断形式 判断当前列同时改动当前列 判断一个情况改动其他值 一类情况详解:实现的是当num这一列的值为3时,就显示好 以此类推 1)case num when 3 the ...
- tensorflow报错 Key Conv/biases not found in checkpoint
可能的解决方法: 删除训练文件夹中的旧模型
- java--内存管理的几点小技巧
今天看一本书,书上提到了内存泄露,后面也提到了内存管理的小技巧,在这里记下来,以免以后忘记. 1.尽量使用直接量.比如:String str = "I can play!";而不是 ...
- [原]Android打包之Eclipse打包
Android自动打包流程详细图: 步骤一: 在工程中新建一个build.xml. 步骤二: 给工程配置ant工具. 选择ant工具的步骤如下: Windows->Shown view-> ...