介绍以下类接口:
BeanPostProcessor接口、BeanFactoryPostProcessor接口、ResourceBundleMessageSource类、FactoryBean接口
七、BeanPostProcessor接口
当需要对受管bean进行预处理时,可以新建一个实现BeanPostProcessor接口的类,并将该类配置到Spring容器中。
实现BeanPostProcessor接口时,需要实现以下两个方法:
postProcessBeforeInitialization 在bean初始化之前调用
postProcessAfterInitialization 在bean初始化之后调用
,容器中的每个Bean在创建时都会调用它们。代码展示如下:
public class SeasyBeanPostProcessor implements BeanPostProcessor, BeanFactoryAware { private BeanFactory beanFactory; @Override public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { try{ //实现类似 @autowired 注解类的功能 for(Field field : bean.getClass().getDeclaredFields()) { //字段有 @Log 注解类 Log annotation = field.getAnnotation(Log.class); if(null != annotation) { //获取字段类型对应的Bean对象 Object obj = beanFactory.getBean(field.getType()); //为bean的field字段设置值 field.setAccessible(true); field.set(bean, obj); } } }catch(Exception ex){ ex.printStackTrace(); } return bean; } @Override public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { return bean; } @Override public void setBeanFactory(BeanFactory beanFactory) throws BeansException { this.beanFactory = beanFactory; } }
public class CountryMapperImpl implements CountryMapper { @Log private User user; }
<bean id="customBeanPostProcessor" class="com.seasy.spring.SeasyBeanPostProcessor"/>
多个BeanPostProcessor实例可以通过设置order属性或实现ordered接口来控制它们的运行顺序。
八、BeanFactoryPostProcessor接口
当需要对Bean工厂进行预处理时,可以新建一个实现BeanFactoryPostProcessor接口的类,并将该类配置到Spring容器中。
BeanFactoryPostProcessor是在spring容器加载了bean的定义文件之后,在bean实例化之前执行的。
使用BeanFactoryPostProcessor对BeanFactory中的BeanDefinition进行变化修改,或者创建BeanDefinition到BeanFactory中。
代码展示如下:
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" />
相关推荐
普通javaweb项目调用springCloud接口(超级实用,很详细)但是要注意的事一定要对springboot和springcloud的有所了解,不然的话你也是看不懂的,大家一起努力共同学习
本文来自程序猿,本文主要介绍了SpringCloud微服务下服务接口调试及管理,什么样方式可以让微服务的接口管理变得更加容易些...而有些微服务则会提供一些公共性质的服务接口,如短信接口统一认证接口之类。而这些微服务往
{"code":200,"data":[{"age":12,"name":"12","sex":"男","username":"张三","userpassword":"123"},{"age":22,"name":"12","sex":"女","username":"李白","userpassword":"4576"}]}
CXF整合spring实现SOAP接口开发
利用spring如何实现接口限流 1.创建自定义注解 ** * 限流注解 */ @Inherited @Documented @Target({ElementType.FIELD, ElementType.TYPE, ElementType.METHOD}) @Retention(RetentionPolicy.RUNTIME) public @...
Memcached在windows下JAVA使用。有服务器安装文件。有需要使用的Jar包。有源码。以及和spring集成的配置文件。还有封装好的接口和类。直接使用即可。而且也便于扩展。
springRMI接口实现
使用了ApplicationContextAware接口,获取spring管理的bean; 多项目整合夸spring容器获取bean的实现方式。
spring boot接口项目,集成了token验证,jsp页面的支持等等
基于java以及spring boot构建的接口工程,实现高效的线上部署及restful形式访问
基于struts+hibernate+spring银行开发的接口小系统
在网上找了很久的资料,接口已经跟客户对接,特编写了代码文档和附带jar包,并含其他网站的参考地址,注意jdk环境要1.6或以上版本
spring security http接口鉴权使用示范项目
java Spring mvc 新增和修改的泛型帮助类,用同一接口修改相同实体类的属性
而Swagger是目前最流行的接口文档解决方案,本文主要通过代码实战的方式讲解Spring Boot 和Swagger集成生成Restful接口文档。教程参见 http://blog.csdn.net/zjx2016/article/details/74407832
SpringCloud微服务,服务查询,接口查询,可以简单查看微服务接口提供者和消费者查询列表,可以简单知晓接口使用情况
适合有spring框架的javaEE平台,出自spring的HttpInvokerServiceExporter导出器,依赖Spring.jar
下面小编就为大家带来一篇spring的几个重要类和接口(详解)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
SpringCloud尚硅谷-OpenFeign服务接口调用及代码
Spring各种回调接口[定义].pdf