Spring常用接口和类
一、ApplicationContextAware接口
当一个类需要获取ApplicationContext实例时,可以让该类实现ApplicationContextAware接口。代码展示如下:

public class Animal implements ApplicationContextAware, BeanNameAware{
private String beanName;
private ApplicationContext applicationContext;
public void setBeanName(String name) {
this.beanName = name;
}
/**
* @param applicationContext 该参数将由Spring容器自动赋值
*/
public void setApplicationContext(ApplicationContext applicationContext)throws BeansException {
this.applicationContext = applicationContext;
}
public void run(){
System.out.println(beanName);
//发布自定义事件
AnimalEvent event = new AnimalEvent(this, "老虎");
applicationContext.publishEvent(event);
}
}

通过@Autowired注解可以自动装配一些常用对象实例:

@Autowired
private MessageSource messageSource; @Autowired
private ResourceLoader resourceLoader; @Autowired
private ApplicationContext applicationContext;

二、ApplicationEvent抽象类
当需要创建自定义事件时,可以新建一个继承自ApplicationEvent抽象类的类。代码展示如下:

/**
* 自定义事件
*/
public class AnimalEvent extends ApplicationEvent {
private String name; public String getName() {
return name;
} /**
* @param source 事件源对象
*/
public AnimalEvent(Object source){
super(source);
} public AnimalEvent(Object source, String name){
super(source);
this.name = name;
}
}

三、ApplicationListener接口
当需要监听自定义事件时,可以新建一个实现ApplicationListener接口的类,并将该类配置到Spring容器中。代码展示如下:

/**
* 自定义事件监听器
*/
public class CustomEventListener implements ApplicationListener {
public void onApplicationEvent(ApplicationEvent event) {
if(event instanceof AnimalEvent){
AnimalEvent animalEvent = (AnimalEvent)event;
System.out.println("触发自定义事件:Animal name is " + animalEvent.getName());
}
}
}

<!-- 自定义事件监听器:Spring容器自动注册它 -->
<bean id="customEventListener" class="com.cjm.spring.CustomEventListener"/>
要发布自定义事件,需要调用ApplicationContext的publishEvent方法,具体用法请看Animal类的源码。
四、BeanNameAware接口
当bean需要获取自身在容器中的id/name时,可以实现BeanNameAware接口。
五、InitializingBean接口
当需要在bean的全部属性设置成功后做些特殊的处理,可以让该bean实现InitializingBean接口。 效果等同于bean的init-method属性的使用或者@PostContsuct注解的使用。 三种方式的执行顺序:先注解,然后执行InitializingBean接口中定义的方法,最后执行init-method属性指定的方法。
六、DisposableBean接口 当需要在bean销毁之前做些特殊的处理,可以让该bean实现DisposableBean接口。 效果等同于bean的destroy-method属性的使用或者@PreDestory注解的使用。 三种方式的执行顺序:先注解,然后执行DisposableBean接口中定义的方法,最后执行destroy-method属性指定的方法
七、BeanPostProcessor接口
当需要对受管bean进行预处理时,可以新建一个实现BeanPostProcessor接口的类,并将该类配置到Spring容器中。
实现BeanPostProcessor接口时,需要实现以下两个方法:
postProcessBeforeInitialization 在受管bean的初始化动作之前调用
postProcessAfterInitialization 在受管bean的初始化动作之后调用容器中的每个Bean在创建时都会恰当地调用它们。代码展示如下:

public class CustomBeanPostProcessor implements BeanPostProcessor {
/**
* 初始化之前的回调方法
*/
public Object postProcessBeforeInitialization(Object bean, String beanName)throws BeansException {
System.out.println("postProcessBeforeInitialization: " + beanName);
return bean;
}
/**
* 初始化之后的回调方法
*/
public Object postProcessAfterInitialization(Object bean, String beanName)throws BeansException {
System.out.println("postProcessAfterInitialization: " + beanName);
return bean;
}
}

<!-- 自定义受管Bean的预处理器:Spring容器自动注册它 -->
<bean id="customBeanPostProcessor" class="com.cjm.spring.CustomBeanPostProcessor"/>
八、BeanFactoryPostProcessor接口
当需要对Bean工厂进行预处理时,可以新建一个实现BeanFactoryPostProcessor接口的类,并将该类配置到Spring容器中。代码展示如下:
public class CustomBeanFactoryPostProcessor implements BeanFactoryPostProcessor {
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
System.out.println(beanFactory.getClass().getSimpleName());
}
}
<!-- 自定义Bean工厂的预处理器:Spring容器自动注册它 -->
<bean id="customBeanFactoryPostProcessor" class="com.cjm.spring.CustomBeanFactoryPostProcessor"/>
Spring内置的实现类:
1、PropertyPlaceholderConfigurer类
用于读取Java属性文件中的属性,然后插入到BeanFactory的定义中。

<bean id="propertyPlaceholderConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>jdbc.properties</value>
</list>
</property>
</bean> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName"><value>${jdbc.driverClassName}</value></property>
<property name="url"><value>${jdbc.url}</value></property>
<property name="username"><value>${jdbc.username}</value></property>
<property name="password"><value>${jdbc.password}</value></property>
</bean>

PropertyPlaceholderConfigurer的另一种精简配置方式(context命名空间):
<context:property-placeholder location="classpath:jdbc.properties, classpath:mails.properties"/>
Java属性文件内容:
jdbc.driverClassName=oracle.jdbc.driver.OracleDriver
jdbc.url=jdbc:oracle:thin:@localhost:1521:orcl
jdbc.username=qycd
jdbc.password=qycd
除了可以读取Java属性文件中的属性外,还可以读取系统属性和系统环境变量的值。
读取系统环境变量的值:${JAVA_HOME}
读取系统属性的值:${user.dir}
2、PropertyOverrideConfigurer类
用于读取Java属性文件中的属性,并覆盖XML配置文件中的定义,即PropertyOverrideConfigurer允许XML配置文件中有默认的配置信息。
Java属性文件的格式:
beanName.property=value
beanName是属性占位符企图覆盖的bean名,property是企图覆盖的数姓名。

<bean id="propertyOverrideConfigurer" class="org.springframework.beans.factory.config.PropertyOverrideConfigurer">
<property name="locations">
<list>
<value>jdbc.properties</value>
</list>
</property>
</bean> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="11"/>
<property name="url" value="22"/>
<property name="username" value="33"/>
<property name="password" value="44"/>
</bean>

Java属性文件内容:
dataSource.driverClassName=oracle.jdbc.driver.OracleDriver
dataSource.url=jdbc:oracle:thin:@localhost:1521:orcl
dataSource.username=qycd
dataSource.password=qycd
九、ResourceBundleMessageSource类
提供国际化支持,bean的名字必须为messageSource。此处,必须存在一个名为jdbc的属性文件。

<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basenames">
<list>
<value>jdbc</value>
</list>
</property>
</bean>

jdbc.properties属性文件的内容:
welcome={0}, welcome to guangzhou!
AbstractApplicationContext ctx = new FileSystemXmlApplicationContext("applicationContext.xml");
ctx.getMessage("welcome", new String[]{"张三"}, "", Locale.CHINA);
十、FactoryBean接口
用于创建特定的对象,对象的类型由getObject方法的返回值决定。

public class MappingFactoryBean implements FactoryBean {
/**
* 获取mapping配置对象
* @return mapping配置
*/
public Object getObject() throws Exception {
List<String> configs = ApplicationContext.getContext().getApplication().getMappingConfigs();
return configs.toArray(new String[configs.size()]);
}
/**
* 返回Bean的类型
* @return Bean的类型
*/
public Class<?> getObjectType() {
return String[].class;
}
/**
* 返回Bean是否是单例的
* @return true表示是单例的
*/
public boolean isSingleton() {
return true;
}
}


public class MappingAutowiring implements BeanPostProcessor {
/**
* 映射配置
*/
private String[] mappingResources;
/**
* 获取映射配置信息
* @return 映射配置
*/
public String[] getMappingResources() {
return mappingResources;
}
/**
* 设置映射配置信息
* @param mappingResources 映射配置
*/
public void setMappingResources(String[] mappingResources) {
this.mappingResources = mappingResources;
}
/**
* 自动装配
* @param bean Spring容器托管的bean
* @param beanName Bean名称
* @return 装配了映射文件后的对象
*/
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
if (bean instanceof LocalSessionFactoryBean) {
((LocalSessionFactoryBean) bean).setMappingResources(mappingResources);
}
return bean;
}
public Object postProcessAfterInitialization(Object bean, String beanName)
throws BeansException {
return bean;
}
}


<bean id="mappingAutowiring" class="com.achievo.framework.server.core.deploy.MappingAutowiring">
<property name="mappingResources" ref="mappingResources" />
</bean> <bean id="mappingResources" class="com.achievo.framework.server.core.deploy.MappingFact
oryBean" />
一、CustomEditorConfigurer类
CustomEditorConfigurer可以读取实现java.beans.PropertyEditor接口的类,将字符串转为指定的类型。更方便的可以使用PropertyEditorSupport。PropertyEditorSupport实现PropertyEditor接口,必须重新定义setAsText。

public class Hello {
private String message;
private User user;
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
}

自定义属性编辑器继承PropertyEditorSupport类,重写setAsText方法。

public class UserEditor extends PropertyEditorSupport{
@Override
public void setAsText(String text) throws IllegalArgumentException {
//类型为User的变量声明了自定义属性编辑器,其值规定为逗号分割的字符串
String[] arr = text.split(",");
Integer age = new Integer(arr[1]);
User user = new User();
user.setName(arr[0]);
user.setAge(age);
setValue(user);
}
}

bean配置

<bean id="configBean"
class="org.springframework.beans.factory.config.CustomEditorConfigurer">
<property name="customEditors">
<map>
<!-- 类型为User的变量都通过UserEditor间接设值 -->
<entry key="User">
<bean id="userEditor" class="UserEditor"/>
</entry>
</map>
</property>
</bean> <bean id="hello" class="Hello">
<property name="message" value="hello" />
<property name="user" value="chenjumin,20"/><!-- 类型为User的变量声明了自定义属性编辑器,其值规定为逗号分割的字符串 -->
</bean>
Spring常用接口和类的更多相关文章
- Servlet API遍程常用接口和类
本文主要总结Servlet API遍程常用接口和类 Servlet API http://tomcat.apache.org/tomcat-5.5-doc/servletapi/index.html ...
- JDBC 学习笔记(三)—— JDBC 常用接口和类,JDBC 编程步骤
1. JDBC 常用接口和类 DriverManager 负责管理 JDBC 驱动的服务类,程序中主要的功能是获取连接数据库的 Connection 对象. Connection 代表一个数据库连接对 ...
- JDBC常用接口、类介绍
JDBC常用接口.类介绍 JDBC提供对独立于数据库统一的API,用以执行SQL命令.API常用的类.接口如下: DriverManager 管理JDBC驱动的服务类,主要通过它获取Connectio ...
- spring常用接口 InitializingBean的作用
工作中遇到spring接口中的InitializingBean接口.浅浅的解说一下. --------------------------------------------------------- ...
- spring常用的工具类
spring给我们提供了很多的工具类, 应该在我们的日常工作中很好的利用起来. 它可以大大的减轻我们的平时编写代码的长度. 因我们只想用spring的工具类, 而不想把一个大大的spring工程给引入 ...
- Java实战之02Hibernate-01简介、常用接口、CRUD操作
一.Hibernate简介 1.Hibernate在开发中所处的位置 2.ORM映射 Object :面向对象领域的 Relational:关系数据库领域的 Mapping:映射 Object: Re ...
- Spring常用的接口和类(一)
一.ApplicationContextAware接口 当一个类需要获取ApplicationContext实例时,可以让该类实现ApplicationContextAware接口.代码展示如下: p ...
- Spring常用的接口和类(二)
七.BeanPostProcessor接口 当需要对受管bean进行预处理时,可以新建一个实现BeanPostProcessor接口的类,并将该类配置到Spring容器中. 实现BeanPostPro ...
- Spring常用的接口和类(三)
一.CustomEditorConfigurer类 CustomEditorConfigurer可以读取实现java.beans.PropertyEditor接口的类,将字符串转为指定的类型.更方便的 ...
随机推荐
- 解决IE8下CSS3选择器 :nth-child() 不兼容的问题
1.定义和用法 :nth-child(n) 选择器匹配属于其父元素的第 N 个子元素,不论元素的类型. n 可以是数字.关键词或公式. <ul> <li>1</li> ...
- Python内置函数(2)——divmod
英文文档: divmod(a, b) Take two (non complex) numbers as arguments and return a pair of numbers consisti ...
- 阿里云API网关(2)开放 API 并接入 API 网关
网关指南: https://help.aliyun.com/document_detail/29487.html?spm=5176.doc48835.6.550.23Oqbl 网关控制台: https ...
- kafka HA
1. replication 如图.1所示,同一个 partition 可能会有多个 replica(对应 server.properties 配置中的 default.replication.fac ...
- VS2013 工程属性配置
1. 配置属性设置 设置工程编译输出目录 2. 设置第三方库的头文件的位置 3.设置第三方库(动态库或者静态库链接的搜寻的目录) 4.设置链接的第三方库的名称 注: 第三方库的链接可以通过配置文件来实 ...
- 理解JavaScript中的call和apply方法
call方法 总的来说call()有这几种作用:1.可以借用另一个对象的方法.2.改变this的指向(重要).3.将arguments数组化.下面详细介绍这三种作用: 1.可以借用另一个对象的方法:当 ...
- AWS stolen CPU
故事的开头是这样的: 一天我正在吃饭,突然就收到了服务器告警(cpu high load),吓的我饭也没吃好,只吃了三碗就回去处理故障了,我在监控上看到了这样子的图: 看见了吧,吃饭那段时间cpu一下 ...
- JQuery开发报错集锦
报错一:JQuery $.each遍历JSON字符串报Uncaught TypeError:Cannot use 'in' operator to search for "70". ...
- Bank方案SQL
用于演示的Bank方案对应的SQL: /* 1.branch 开展银行交易业务的场所 */ DROP TABLE IF EXISTS branch; CREATE TABLE branch -- 开展 ...
- 涨薪必备Javascript,快点放进小口袋!
摘要: 嗨,新一年的招聘季,你找到更好的工作了吗?小姐姐最近刚换的工作,来总结下面试必备小技能,从this来看看javascript,让我们更深入的了解它. 前言 在JavaScript中,被吐槽最多 ...