Spring properties定义bean
2019独角兽企业重金招聘Python工程师标准>>>
Spring提供了丰富的标签和注解来进行bean的定义,除此之外框架来提供了扩展机制让使用可以通过properties来定义bean,与强大的标签式和注解式的bean定义相比,properties提供的规则要简单许多。
key部分用.分隔即通过A.B来进行相关的属性定义,其中A表示bean名称,B通过不同的值还表达不同的含义:
- (class),bean的类型
- (parent),bean的父bean
- name,bean的name属性,name是一个普通属性
- childBean(ref),bean的childBean属性,childBean是一个引用属性
- (singleton),是否单例
- (lazy-init),是否懒加载
- $0,第一个构造子参数
- (scope),作用域
- (abstract),是否是抽象bean
看一个例子:
bean.properties文件
[java] view plain copy
- #bean1
- propBean.(class) = spring.beans.properties.PropBean
- propBean.(parent) = commonBean
- propBean.name = name1
- propBean.childBean(ref) = childBean
- propBean.(singleton) = true
- propBean.(lazy-init) = true
- #bean2
- childBean.(class) = spring.beans.properties.ChildBean
- childBean.$0 = cid1
- chlldBean.(scope) = singleton
- #abstract bean
- commonBean.(class) = spring.beans.properties.CommonBean
- commonBean.id = 1
- commonBean.(abstract) = true
上面的properties文件定义了三个bean:
- commonBean,类型是spring.beans.properties.CommonBean,注入值1到id属性,这是一个抽象bean
- childBean,类型spring.beans.properties.ChildBean,构造器注入cid1,作用域是singleton
- propBean,类型是spring.beans.properties.PropBean,父bean是commonBean,注入一个普通属性name,和引用属性childBean,引用的bean是childBean,bean是单例并且懒加载。
bean定义文件写好之后,通过PropertiesBeanDefinitionReader来加载解析bean定义,这个解析器的原理很简单,在此不做详细分析,下面是实例代码。
[java] view plain copy
- public void test() {
- GenericApplicationContext ctx = new GenericApplicationContext();
- Resource res = new ClassPathResource(
- "spring/beans/properties/bean.properties");
- PropertiesBeanDefinitionReader propReader = new PropertiesBeanDefinitionReader(
- ctx);
- propReader.loadBeanDefinitions(res);
- PropBean propBean = (PropBean) ctx.getBean("propBean");
- assertNotNull(propBean);
- assertNotNull(propBean.getId());
- assertNotNull(propBean.getChildBean());
- assertNotNull(propBean.getChildBean().getCid());
- }
也可以完全不用单独定义个properties文件,只需要把相关的key-value放到一个Map中,再通过PropertiesBeanDefinitionReader加载这个map中的key-value。
通过这种方式,使用者可以根据需要自定义些bean的定义规则,比如可以把bean定义放在数据库中,把数据库中的信息读取出来拼接成满足properties规则的bean定义,在Spring中就定义了一个org.springframework.jdbc.core.support.JdbcBeanDefinitionReader来完成这种需求,看下这个类的代码。
[java] view plain copy
- public class JdbcBeanDefinitionReader {
- private final PropertiesBeanDefinitionReader propReader;
- private JdbcTemplate jdbcTemplate;
- /**
- * Create a new JdbcBeanDefinitionReader for the given bean factory,
- * using a default PropertiesBeanDefinitionReader underneath.
- * <p>DataSource or JdbcTemplate still need to be set.
- * @see #setDataSource
- * @see #setJdbcTemplate
- */
- public JdbcBeanDefinitionReader(BeanDefinitionRegistry beanFactory) {
- this.propReader = new PropertiesBeanDefinitionReader(beanFactory);
- }
- /**
- * Create a new JdbcBeanDefinitionReader that delegates to the
- * given PropertiesBeanDefinitionReader underneath.
- * <p>DataSource or JdbcTemplate still need to be set.
- * @see #setDataSource
- * @see #setJdbcTemplate
- */
- public JdbcBeanDefinitionReader(PropertiesBeanDefinitionReader beanDefinitionReader) {
- Assert.notNull(beanDefinitionReader, "Bean definition reader must not be null");
- this.propReader = beanDefinitionReader;
- }
- /**
- * Set the DataSource to use to obtain database connections.
- * Will implicitly create a new JdbcTemplate with the given DataSource.
- */
- public void setDataSource(DataSource dataSource) {
- this.jdbcTemplate = new JdbcTemplate(dataSource);
- }
- /**
- * Set the JdbcTemplate to be used by this bean factory.
- * Contains settings for DataSource, SQLExceptionTranslator, NativeJdbcExtractor, etc.
- */
- public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
- Assert.notNull(jdbcTemplate, "JdbcTemplate must not be null");
- this.jdbcTemplate = jdbcTemplate;
- }
- /**
- * Load bean definitions from the database via the given SQL string.
- * @param sql SQL query to use for loading bean definitions.
- * The first three columns must be bean name, property name and value.
- * Any join and any other columns are permitted: e.g.
- * {@code SELECT BEAN_NAME, PROPERTY, VALUE FROM CONFIG WHERE CONFIG.APP_ID = 1}
- * It's also possible to perform a join. Column names are not significant --
- * only the ordering of these first three columns.
- */
- public void loadBeanDefinitions(String sql) {
- Assert.notNull(this.jdbcTemplate, "Not fully configured - specify DataSource or JdbcTemplate");
- final Properties props = new Properties();
- this.jdbcTemplate.query(sql, new RowCallbackHandler() {
- public void processRow(ResultSet rs) throws SQLException {
- String beanName = rs.getString(1);
- String property = rs.getString(2);
- String value = rs.getString(3);
- // Make a properties entry by combining bean name and property.
- props.setProperty(beanName + "." + property, value);
- }
- });
- this.propReader.registerBeanDefinitions(props);
- }
- }
此外,通过理解PropertiesBeanDefinitionReader的实现方式,发现也可以通过扩展BeanDefinitionReader来扩展bean定义,我们可以通过继承AbstractBeanDefinitionReader来完成这种扩展。
转载于:https://my.oschina.net/xiaominmin/blog/1611342
Spring properties定义bean相关推荐
- Spring注解定义 bean 的12种方法
前言 在庞大的java体系中,spring有着举足轻重的地位,它给每位开发者带来了极大的便利和惊喜.我们都知道spring是创建和管理bean的工厂,它提供了多种定义bean的方式,能够满足我们日常工 ...
- spring框架中Bean的基本属性及调用外部properties等配置文件的方法介绍
Bean的基本属性 id属性: Bean的唯一标识名.它必须是合法的XML ID,在配置文件中,不能有重复id的Bean,因为容器在获取Bean的实例时都用它来做唯一索引. name属性: 用来为id ...
- 惊呆了,Spring中竟然有12种定义bean的方法
前言 在庞大的 Java 技术体系中,Spring 有着举足轻重的地位,它给每位开发者带来了极大的便利和惊喜. 我们都知道 Spring 是创建和管理bean的工厂,它提供了多种方式定义 bean,能 ...
- 零配置 之 Spring注解实现Bean定义
转载自 零配置 之 12.3 注解实现Bean定义 --跟我学spring3 12.3 注解实现Bean定义 12.3.1 概述 前边介绍的Bean定义全是基于XML方式定义配置元数据,且在[1 ...
- 零配置 之Spring基于Java类定义Bean配置元数据
转载自 [第十二章]零配置 之 12.4 基于Java类定义Bean配置元数据 --跟我学spring3 12.4 基于Java类定义Bean配置元数据 12.4.1 概述 基于Java类定义B ...
- Spring定义Bean的方式
声明式 1. <bean/> 使用xml的方式定义bean 1)先引入spring的依赖 <dependency><groupId>org.springframew ...
- Spring中的Bean配置、属性配置、装配内容详细叙述
文章目录 1.Bean的配置 1.1.配置方式 2.Bean的实例化 2.1.构造器实例化 2.2.静态工厂方式实例化 2.3.实例工厂方式实例化 3.Bean的作用域 3.1.作用域的种类 4.Be ...
- Spring IOC容器-Bean管理——基于XML方式
Spring IOC容器-Bean管理--基于XML(续集) 1.IOC 操作 Bean 管理(FactoryBean) 1).Spring 有两种类型 bean,一种普通 bean,另外一种工厂 ...
- spring扩展点之二:spring中关于bean初始化、销毁等使用汇总,ApplicationContextAware将ApplicationContext注入...
<spring扩展点之二:spring中关于bean初始化.销毁等使用汇总,ApplicationContextAware将ApplicationContext注入> <spring ...
最新文章
- Metasploit设置HttpTrace参数技巧
- APUE读书笔记-12线程控制-04同步属性
- Service Fabric 用 Powershell 部署应用到本地
- python网络爬虫系列(十一)——JS的解析
- javafor循环打印图案_C程序使用循环打印盒子图案
- 引入外部js如何通知页面其编码格式
- 光纤 matlab,matlab – 均衡光纤通道的最小均方
- css实现自适应正方形
- instanceof java 原理_JAVA中 instanceof 和 getClass() 区别小结
- foobar2000中文版官方下载【多功能的音频播放器】
- fragment 淡入淡出_一种模型淡入淡出时透明面重叠问题的解决方案
- datatables分页,排序,ajax请求等参数设置
- cesium分屏对比
- 台式计算机无线接入,台式电脑可以无线连接wifi吗
- 最长不含重复字符的字符串
- 三、【VUE基础】数据绑定
- 2020-04-12
- USB协议学习笔记 - CUSTOM HID 设备
- linux服务器新装hba卡,Linux更换HBA卡后重新扫盘指令|或者新增HBA卡
- LED背光源运用在温控设备上
热门文章
- 三个等价c语言表达式,C语言习题综合(20页)-原创力文档
- java校招面试题_java校招面试编程题及答案.docx
- 未定义变量: data_三、变量声明
- matlab求RMSECV,CARS 用于matlab模式识别(分类和回归)的特征变量提取方法 联合开发网 - pudn.com...
- 按钮点击打开新页面_PDF怎么打开?如何制作一个PDF格式的文档?
- python3.6程序_python3.6如何生成exe程序
- 各层作用_终于弄明白了 Singleton,Transient,Scoped 的作用域是如何实现的
- mac远程linux的ide,Jupyter notebook在mac:linux上的配置和远程访问
- 防抖 节流_【前端面试】节流与防抖
- 《软件项目管理(第二版)》第 9 章——项目监督与控制 重点部分总结