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 的 ...
随机推荐
- ASP.NET MVC 音乐商店 - 5 通过支架创建编辑表单 续
查看 StoreManager 控制器的代码 现在,Store Manager 控制器中已经包含了一定数量的代码,我们从头到尾重新过一下. 首先,在控制器中包含了标准的 MVC 控制器的代码,为了使用 ...
- office转换为html在线预览
/// <summary> /// word 转换为html /// </summary> /// <param name="path">要转换 ...
- Filter学习总结,顺便提及点servlet3.0异步filter和异步监听
Filter介绍: Filter在项目中经常可以用到,通常配置在web.xml中.是服务器端的一个组件,对于用户的请求和响应数据进行过滤操作,控制是否让用户访问到对应的web资源.常用于编 ...
- UML视频
https://www.bilibili.com/video/av34973179/?p=1 北京 圣思园 UML视频
- Flask入门模板过滤器与测试器(五)
1 模板引擎之过滤器 概念 : 过滤器本质上是个转换函数,第一个参数是待过滤的变量.如果它有第二个参数,模板中就必须传进去. 过滤器使用管道符| 放在{{ }} Jinja2模板引擎提供了丰富的内置过 ...
- 进程状态转换及其PCB的变化
代码实现了模拟进程状态转换及其相应PCB内容.组织结构的变化. #include<iostream> using namespace std; typedef struct pcb{ in ...
- 转一个csdn看到的帖子:而立之年的程序猿失业了 [问题点数:0分,结帖人jinxingfeng_cn]
http://bbs.csdn.net/topics/390612263?page=1#post-395768948
- Android进阶笔记09:Android 万能适配器
1. Android 万能适配器 项目中Listview GridView几乎是必用的组件,Android也提供一套机制,为这些控件绑定数据,那就是Adapter.用起来虽然还不错,但每次都 ...
- POJ 最小球覆盖 模拟退火
最小球覆盖:用半径最小的球去覆盖所有点. 纯粹的退火算法,是搞不定的,精度不够,不然就会TLE,根本跑不出答案来. 任取一点为球心,然后一点点靠近最远点.其实这才是最主要的. 因为:4个点确定一个球, ...
- LA 3353 最优巴士线路设计
给出一个 n 个点的有向图,找若干个圈,是的每个结点恰好属于一个圈.要求总长度尽量小. 三倍经验题 Uva 12264,HDU 1853 这题有两种解法,一是匹配: 每个点只在一个圈中,则他有唯一的前 ...