Spring 中的接口知识整理
本想每个小知识一篇随笔,但是那样,看起来有些单薄,所以,就放在一片文章里了。而且,以后还会慢慢在最后不断的追加。
目录:
FactoryBean
BeanPostProcessor
1.FactoryBean
FactoryBean接口,它在Spring框架源码内部,被大量使用,如使用AOP创建bean的代理时,使用了ProxyFactoryBean;从JNDI中查找对象时,使用了JndiObjectFactoryBean。
它在框架外,很少使用。但是为了学习,也得研究研究,您说是不?
它怎么使用?首先看看构造。
public interface FactoryBean<T> {
// 返回的是这个工厂管理的对象的实例
T getObject() throws Exception;
// 返回的是工厂创建的对象的类型
Class<?> getObjectType();
boolean isSingleton();
}
上个例子,看具体东西。
public class Tool {
private int id;
public Tool(int id) {
this.id = id;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
public class ToolFactory implements FactoryBean<Tool> {
private int factoryId;
private int toolId;
// 创建的是Tool的对象
@Override
public Tool getObject() throws Exception {
8 return new Tool(toolId);
9 }
@Override
public Class<?> getObjectType() {
13 return Tool.class;
14 }
@Override
public boolean isSingleton() {
return false;
}
public int getFactoryId() {
return factoryId;
}
public int getToolId() {
return toolId;
}
public void setFactoryId(int factoryId) {
this.factoryId = factoryId;
}
public void setToolId(int toolId) {
this.toolId = toolId;
}
}
public class Test {
public static void main(String[] args) {
@SuppressWarnings("resource")
ApplicationContext context = new ClassPathXmlApplicationContext("beaFactoryTest.xml");
Tool tool = (Tool) context.getBean("tool");
// 在名字前面加 & 表示要得到工厂类的bean
ToolFactory tool2 = (ToolFactory) context.getBean("&tool");
System.out.println("Tool Id is " + tool.getId());
System.out.println("ToolFactory Id is " + tool2.getFactoryId());
}
}
<bean id="tool" class="com.lee.demo.beanfactory.ToolFactory">
<property name="factoryId" value="9090"/>
<property name="toolId" value="108"/>
</bean>
最后执行结果:
Tool Id is 108
ToolFactory Id is 9090
2.BeanPostProcessor
它叫bean后处理器,也就是处理bean,它针对所有的bean,在初始化前后,对bean进行操作。主要内容我会在例子中描述。
例子:
public class HelloWorld {
private String message;
public void setMessage(String message) {
this.message = message;
}
public void getMessage() {
System.out.println("Your Message : " + message);
}
// 加上init方法和destory方法,是为了在后面验证他们和postProcessBeforeInitialization postProcessAfterInitialization 执行的先后顺序
public void init() {
System.out.println("Bean is going through init.");
}
public void destroy() {
System.out.println("Bean will destroy now.");
}
}
public class InitHelloWorld implements BeanPostProcessor {
// BeanPostProcessor 接口中主要是下面两个方法 初始化前的后处理 初始化后的后处理
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
System.out.println("BeforeInitialization : " + beanName);
return bean; // you can return any other object as well
}
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
System.out.println("AfterInitialization : " + beanName);
return bean; // you can return any other object as well
}
}
Test.java
public class Test {
public static void main(String[] args) {
@SuppressWarnings("resource")
AbstractApplicationContext context = new ClassPathXmlApplicationContext("beaFactoryTest.xml");
HelloWorld obj = (HelloWorld) context.getBean("helloWorld");
obj.getMessage();
// 注册钩子方法
context.registerShutdownHook();
}
}
查看源码:
@Override
// 在JVM运行期间,注册一个钩子关闭方法,关闭掉这个context。实际的关闭程序的操作,被代理给doClose方法。
public void registerShutdownHook() {
if (this.shutdownHook == null) {
// No shutdown hook registered yet.
this.shutdownHook = new Thread() {
@Override
public void run() {
doClose();
}
};
Runtime.getRuntime().addShutdownHook(this.shutdownHook);
}
}
xml文件
<bean id = "helloWorld" class = "com.lee.demo.beanfactory.HelloWorld"
init-method = "init" destroy-method = "destroy">
<property name = "message" value = "Hello World!"/>
</bean> <bean class = "com.lee.demo.beanfactory.InitHelloWorld" />
执行结果:
BeforeInitialization : helloWorld
Bean is going through init.
AfterInitialization : helloWorld
Your Message : Hello World!
[org.springframework.context.support.ClassPathXmlApplicationContext] - Closing org.springframework.context.support.ClassPathXmlApplicationContext@6ae40994: startup date [Thu May 17 23:31:53 JST 2018]; root of context hierarchy
Bean will destroy now.
在这里大致说一下Spring的实例化过程:
实例化Bean对象→设置对象属性→检查Aware相关接口并设置相关依赖→BeanPostProcessor前置处理→检查是否是InitializingBean以决定是否调用afterPropertiesSet方法→
检查是否配置有自定义的init-method方法→BeanPostProcessor后置处理→是否实现DisposableBean接口→是否配置有自定义的destroy方法
3.Aware接口
它是干什么用的呢?作用就是通过上下文(Context)可以获得当前环境。看看它的接口:

我们拿BeanNameAware来举个例子。
public class User implements GetNameAware {
private String id;
private String name;
private String address;
6 @Override
7 public void setBeanName(String name) {
8 this.id = name;
9 }
public String getId() {
return id;
}
public String getName() {
return name;
}
public String getAddress() {
return address;
}
public void setId(String id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public void setAddress(String address) {
this.address = address;
}
}
public class User2 {
private String id;
private String name;
private String address;
public String getId() {
return id;
}
public String getName() {
return name;
}
public String getAddress() {
return address;
}
public void setId(String id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public void setAddress(String address) {
this.address = address;
}
}
public interface GetNameAware extends BeanNameAware {
}
public class Test {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("beaFactoryTest.xml");
User user = (User) ctx.getBean("user1");
User2 user2 = (User2) ctx.getBean("user2");
System.out.println("user id " + user.getId() + " user name " + user.getName() + " user address " + user.getAddress());
System.out.println("========================================");
System.out.println("user2 id " + user2.getId() + " user2 name " + user2.getName() + " user2 address " + user2.getAddress());
}
}
<bean id="user1" class="com.lee.demo.aware.User">
<property name="name" value="lee"/>
<property name="address" value="China"/>
</bean> <bean id="user2" class="com.lee.demo.aware.User2">
<property name="name" value="lin"/>
<property name="address" value="Tokyo"/>
</bean>
执行结果(user id 因为继承了GetNameAware接口,对BeanName进行了设置):
user id user1 user name lee user address China
========================================
user2 id null user2 name lin user2 address Tokyo
Spring 中的接口知识整理的更多相关文章
- Spring中Ordered接口简介
目录 前言 Ordered接口介绍 Ordered接口在Spring中的使用 总结 前言 Spring中提供了一个Ordered接口.Ordered接口,顾名思义,就是用来排序的. Spring是一个 ...
- 转:spring中InitailizingBean接口的简单理解
转自:https://www.cnblogs.com/wxgblogs/p/6849782.html spring中InitializingBean接口使用理解 InitializingBean接 ...
- spring中InitializingBean接口使用理解
InitializingBean接口为bean提供了初始化方法的方式,它只包括afterPropertiesSet方法,凡是继承该接口的类,在初始化bean的时候会执行该方法. 测试程序如下: imp ...
- spring中ApplicationContextAware接口描述
项目中使用了大量的工厂类,采用了简单工厂模式: 通过该工厂类可以获取指定的处理器bean,这些处理器bean我们是从spring容器中获取的,如何获取,是通过实现ApplicationContextA ...
- spring中InitializingBean接口使用理解(转)
InitializingBean接口为bean提供了初始化方法的方式,它只包括afterPropertiesSet方法,凡是继承该接口的类,在初始化bean的时候会执行该方法. 测试程序如下: imp ...
- spring(五):spring中Aware接口的使用
spring中自定义组件需要使用spring的底层组件时,可以通过自定义组件实现相关XxxAware接口,重写其中的方法进而实现 例如:自定义一个组件,该组件中需要使用ApplicationConte ...
- Spring中ApplicationContextAware接口的用法
1.为什么使用AppplicationContextAware? ApplicationContext的BeanFactory 的子类, 拥有更强大的功能,ApplicationContext可以在服 ...
- Spring中的接口BeanFactory和FactoryBean的学习
BeanFactory: 相当于对象工厂,可以获取对象的实例以及相应的属性.BeanFactory定义了IOC容器的最基本形式,并提供了IOC容器应遵守的的最基本的接口,也就是Spring IOC所遵 ...
- spring中ApplicationContextAware接口使用理解
一.这个接口有什么用?当一个类实现了这个接口(ApplicationContextAware)之后,这个类就可以方便获得ApplicationContext中的所有bean.换句话说,就是这个类可以直 ...
随机推荐
- Uboot启动流程分析(转载)
最近一段时间一直在做uboot移植相关的工作,需要将uboot-2016-7移植到单位设计的ARMv7的处理器上.正好元旦放假三天闲来无事,有段完整的时间来整理下最近的工作成果.之前在学习uboot时 ...
- Kafka.net使用编程入门(三)
这个世界既不是有钱人的世界,也不是有权人的世界,它是有心人的世界. 一些有用的命令 1.列出主题:kafka-topics.bat --list --zookeeper localhost:2181 ...
- JavaScrip(二)JavaScrip语法基础
一:标识符 标识符是指变量,函数的名字,或函数的参数名: 1.命名规则 1.1第一个字符必须是一个字母.下划线(_).或一个美元符号($) 1.2其他字母可以是字母,下划线.美元符号或数字 1.3不能 ...
- 每天CSS学习之border-collapse
border-collapse是CSS2的一个属性,其作用是折叠表格(table)的边框.collapse翻译过来又折叠的意思. border-collapse有三个值: collapse:将表格和单 ...
- 每天CSS学习之white-space
white-space是CSS的属性,其作用是规定文本不进行换行. white-space有以下几个值: 1.normal:该值为默认值,段落前后的空白会被浏览器忽略.如下所示: <div st ...
- (C/C++学习笔记) 四. 运算符
四. 运算符 运算符优先级和结合性 Operator precedence and associativity (or fixity) 注意: ① 成员运算符MemberOperators可以称为点运 ...
- eclipse server和tomcat的区别,将server的部署目录改到自己安装的tomcat中及如何设置tomcat用户
转:http://www.cnblogs.com/Yogurshine/archive/2013/06/05/3118525.html 一.发现问题(如果不把项目部署到tomcat的webapp目录下 ...
- 3.10 C++虚基类 虚继承
参考:http://www.weixueyuan.net/view/6367.html 总结: 本例即为典型的菱形继承结构,类A中的成员变量及成员函数继承到类D中均会产生两份,这样的命名冲突非常的棘手 ...
- 7series 逻辑单元理解(更新中)
7series 逻辑单元理解 ug768和ug799文档介绍了7系列芯片中包含的基本逻辑单元,对其中常用的单元,进行下分析. 1.IOBUF单元 (1)真值表 (2)用途 the design e ...
- 201621123001《Java程序设计》第4周学习总结
1. 本周学习总结 1.1 写出你认为本周学习中比较重要的知识点关键词 关键字:继承,多态,覆盖(Override),重载(Overload),抽象类(abstract)等. 1.2 尝试使用思维导图 ...