Spring常用的接口和类(二)
七、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.MappingFactoryBean" />
Spring常用的接口和类(二)的更多相关文章
- Spring常用的接口和类(一)
一.ApplicationContextAware接口 当一个类需要获取ApplicationContext实例时,可以让该类实现ApplicationContextAware接口.代码展示如下: p ...
- Spring常用的接口和类(三)
一.CustomEditorConfigurer类 CustomEditorConfigurer可以读取实现java.beans.PropertyEditor接口的类,将字符串转为指定的类型.更方便的 ...
- Spring 常用的一些工具类
学习Java的人,或者开发很多项目,都需要使用到Spring 这个框架,这个框架对于java程序员来说.学好spring 就不怕找不到工作.我们时常会写一些工具类,但是有些时候 我们不清楚,我们些的工 ...
- JavaWeb学习之JDBC API中常用的接口和类
JDBC API中包含四个常用的接口和一个类分别是: 1.Connection接口 2.Statement接口 3.PreparedStatement接口 4.ResultSet接口 5.Driver ...
- Servlet常用的接口和类
使用接口和类的作用:Servlet也是依靠继承父类和实现接口来实现的.使用Servlet必须要引入两个包:javax.servlet和javax.servlet.http.所有的Servlet应用都是 ...
- Spring:Spring项目多接口实现类报错找不到指定类
spring可以通过applicationContext.xml进行配置接口实现类 applicationContext.xml中可以添加如下配置: 在application.properties中添 ...
- 07.Hibernate常用的接口和类---Session接口☆☆☆☆☆
一.特点 Session是在Hibernate中使用最频繁的接口.也被称之为持久化管理器.它提供了和持久化有关的操作,比如添加.修改.删除.加载和查询实体对象 Session 是应用程序与数据库之间交 ...
- servlet学习之servletAPI编程常用的接口和类
ServletConfig接口: SevletConfig接口位于javax.servlet包中,它封装了servlet配置信息,在servlet初始化期间被传递.每一个Servlet都有且只有一个S ...
- 04.Hibernate常用的接口和类---SessionFactory类和作用
是一个生成Session的工厂类 特点: 1.由Configuration通过加载配置文件创建该对象. SessionFactory factory = config.buildSessionFact ...
随机推荐
- 通俗易懂------this指向
因为JavaScript 中this 是在运行期进行绑定的,因此JavaScript 中this 关键字具备多重含义. 具体在实际应用中,this的指向大致可以分为下面4种. 作为对象的方法调用 ...
- 一起写一个Android图片加载框架
本文会从内部原理到具体实现来详细介绍如何开发一个简洁而实用的Android图片加载缓存框架,并在内存占用与加载图片所需时间这两个方面与主流图片加载框架之一Universal Image Loader做 ...
- xml基本操作
在实际项目中遇到一些关于xml操作的问题,被逼到无路可退的时候终于决定好好研究xml一番.xml是一种可扩展标记语言,可跨平台进行传输,因此xml被广泛的使用在很多地方. 本文由浅入深,首先就xml的 ...
- Redis——分布式简单使用
Redis简介:Redis是一个开源的使用ANSI C语言编写.支持网络.可基于内存亦可持久化的日志型.Key-Value数据库,并提供多种语言的API. Redis安装:参考博客http://www ...
- Json-转换
js转换 引用json.js(将json格式转换成字符串 var name = document.getElementById("name").value; var retries ...
- Spring MVC实现防止表单重复提交(转)
Spring MVC拦截器+注解方式实现防止表单重复提交
- 【bzoj1191】 HNOI2006—超级英雄Hero
http://www.lydsy.com/JudgeOnline/problem.php?id=1191 (题目链接) 题意 有m个问题,n个锦囊妙计,每个锦囊妙计可以解决一个问题,每个问题有两个锦囊 ...
- PowerDesigner反向数据库时遇到[Microsoft][ODBC SQL Server Driver][SQL Server]无法预定义语句。SQLSTATE = 37错误解决方法
逆向工程中,有时会出现如下错误 ... [Microsoft][ODBC SQL Server Driver][SQL Server]无法预定义语句 SQLSTATE = 37000 解决方案: 1. ...
- Linux System Log Collection、Log Integration、Log Analysis System Building Learning
目录 . 为什么要构建日志系统 . 通用日志系统的总体架构 . 日志系统的元数据来源:data source . 日志系统的子安全域日志收集系统:client Agent . 日志系统的中心日志整合系 ...
- schemaLocation value = 'xxxxxxxxxxxx' must have even number of URI's
这是因为没有加上Spring的版本号,加上就行了,如: http://www.springframework.org/schema/beans/spring-beans.xsd -3.2.2 http ...