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

  1. #bean1
  2. propBean.(class) = spring.beans.properties.PropBean
  3. propBean.(parent) = commonBean
  4. propBean.name = name1
  5. propBean.childBean(ref) = childBean
  6. propBean.(singleton) = true
  7. propBean.(lazy-init) = true
  8. #bean2
  9. childBean.(class) = spring.beans.properties.ChildBean
  10. childBean.$0 = cid1
  11. chlldBean.(scope) = singleton
  12. #abstract bean
  13. commonBean.(class) = spring.beans.properties.CommonBean
  14. commonBean.id = 1
  15. 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

  1. public void test() {
  2. GenericApplicationContext ctx = new GenericApplicationContext();
  3. Resource res = new ClassPathResource(
  4. "spring/beans/properties/bean.properties");
  5. PropertiesBeanDefinitionReader propReader = new PropertiesBeanDefinitionReader(
  6. ctx);
  7. propReader.loadBeanDefinitions(res);
  8. PropBean propBean = (PropBean) ctx.getBean("propBean");
  9. assertNotNull(propBean);
  10. assertNotNull(propBean.getId());
  11. assertNotNull(propBean.getChildBean());
  12. assertNotNull(propBean.getChildBean().getCid());
  13. }

也可以完全不用单独定义个properties文件,只需要把相关的key-value放到一个Map中,再通过PropertiesBeanDefinitionReader加载这个map中的key-value。

通过这种方式,使用者可以根据需要自定义些bean的定义规则,比如可以把bean定义放在数据库中,把数据库中的信息读取出来拼接成满足properties规则的bean定义,在Spring中就定义了一个org.springframework.jdbc.core.support.JdbcBeanDefinitionReader来完成这种需求,看下这个类的代码。

[java] view plain copy

  1. public class JdbcBeanDefinitionReader {
  2. private final PropertiesBeanDefinitionReader propReader;
  3. private JdbcTemplate jdbcTemplate;
  4. /**
  5. * Create a new JdbcBeanDefinitionReader for the given bean factory,
  6. * using a default PropertiesBeanDefinitionReader underneath.
  7. * <p>DataSource or JdbcTemplate still need to be set.
  8. * @see #setDataSource
  9. * @see #setJdbcTemplate
  10. */
  11. public JdbcBeanDefinitionReader(BeanDefinitionRegistry beanFactory) {
  12. this.propReader = new PropertiesBeanDefinitionReader(beanFactory);
  13. }
  14. /**
  15. * Create a new JdbcBeanDefinitionReader that delegates to the
  16. * given PropertiesBeanDefinitionReader underneath.
  17. * <p>DataSource or JdbcTemplate still need to be set.
  18. * @see #setDataSource
  19. * @see #setJdbcTemplate
  20. */
  21. public JdbcBeanDefinitionReader(PropertiesBeanDefinitionReader beanDefinitionReader) {
  22. Assert.notNull(beanDefinitionReader, "Bean definition reader must not be null");
  23. this.propReader = beanDefinitionReader;
  24. }
  25. /**
  26. * Set the DataSource to use to obtain database connections.
  27. * Will implicitly create a new JdbcTemplate with the given DataSource.
  28. */
  29. public void setDataSource(DataSource dataSource) {
  30. this.jdbcTemplate = new JdbcTemplate(dataSource);
  31. }
  32. /**
  33. * Set the JdbcTemplate to be used by this bean factory.
  34. * Contains settings for DataSource, SQLExceptionTranslator, NativeJdbcExtractor, etc.
  35. */
  36. public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
  37. Assert.notNull(jdbcTemplate, "JdbcTemplate must not be null");
  38. this.jdbcTemplate = jdbcTemplate;
  39. }
  40. /**
  41. * Load bean definitions from the database via the given SQL string.
  42. * @param sql SQL query to use for loading bean definitions.
  43. * The first three columns must be bean name, property name and value.
  44. * Any join and any other columns are permitted: e.g.
  45. * {@code SELECT BEAN_NAME, PROPERTY, VALUE FROM CONFIG WHERE CONFIG.APP_ID = 1}
  46. * It's also possible to perform a join. Column names are not significant --
  47. * only the ordering of these first three columns.
  48. */
  49. public void loadBeanDefinitions(String sql) {
  50. Assert.notNull(this.jdbcTemplate, "Not fully configured - specify DataSource or JdbcTemplate");
  51. final Properties props = new Properties();
  52. this.jdbcTemplate.query(sql, new RowCallbackHandler() {
  53. public void processRow(ResultSet rs) throws SQLException {
  54. String beanName = rs.getString(1);
  55. String property = rs.getString(2);
  56. String value = rs.getString(3);
  57. // Make a properties entry by combining bean name and property.
  58. props.setProperty(beanName + "." + property, value);
  59. }
  60. });
  61. this.propReader.registerBeanDefinitions(props);
  62. }
  63. }

此外,通过理解PropertiesBeanDefinitionReader的实现方式,发现也可以通过扩展BeanDefinitionReader来扩展bean定义,我们可以通过继承AbstractBeanDefinitionReader来完成这种扩展。

转载于:https://my.oschina.net/xiaominmin/blog/1611342

Spring properties定义bean相关推荐

  1. Spring注解定义 bean 的12种方法

    前言 在庞大的java体系中,spring有着举足轻重的地位,它给每位开发者带来了极大的便利和惊喜.我们都知道spring是创建和管理bean的工厂,它提供了多种定义bean的方式,能够满足我们日常工 ...

  2. spring框架中Bean的基本属性及调用外部properties等配置文件的方法介绍

    Bean的基本属性 id属性: Bean的唯一标识名.它必须是合法的XML ID,在配置文件中,不能有重复id的Bean,因为容器在获取Bean的实例时都用它来做唯一索引. name属性: 用来为id ...

  3. 惊呆了,Spring中竟然有12种定义bean的方法

    前言 在庞大的 Java 技术体系中,Spring 有着举足轻重的地位,它给每位开发者带来了极大的便利和惊喜. 我们都知道 Spring 是创建和管理bean的工厂,它提供了多种方式定义 bean,能 ...

  4. 零配置 之 Spring注解实现Bean定义

    转载自  零配置 之 12.3 注解实现Bean定义 --跟我学spring3 12.3  注解实现Bean定义 12.3.1  概述 前边介绍的Bean定义全是基于XML方式定义配置元数据,且在[1 ...

  5. 零配置 之Spring基于Java类定义Bean配置元数据

    转载自  [第十二章]零配置 之 12.4 基于Java类定义Bean配置元数据 --跟我学spring3 12.4  基于Java类定义Bean配置元数据 12.4.1  概述 基于Java类定义B ...

  6. Spring定义Bean的方式

    声明式 1. <bean/> 使用xml的方式定义bean 1)先引入spring的依赖 <dependency><groupId>org.springframew ...

  7. Spring中的Bean配置、属性配置、装配内容详细叙述

    文章目录 1.Bean的配置 1.1.配置方式 2.Bean的实例化 2.1.构造器实例化 2.2.静态工厂方式实例化 2.3.实例工厂方式实例化 3.Bean的作用域 3.1.作用域的种类 4.Be ...

  8. Spring IOC容器-Bean管理——基于XML方式

    Spring IOC容器-Bean管理--基于XML(续集) 1.IOC 操作 Bean 管理(FactoryBean) ​ 1).Spring 有两种类型 bean,一种普通 bean,另外一种工厂 ...

  9. spring扩展点之二:spring中关于bean初始化、销毁等使用汇总,ApplicationContextAware将ApplicationContext注入...

    <spring扩展点之二:spring中关于bean初始化.销毁等使用汇总,ApplicationContextAware将ApplicationContext注入> <spring ...

最新文章

  1. Metasploit设置HttpTrace参数技巧
  2. APUE读书笔记-12线程控制-04同步属性
  3. Service Fabric 用 Powershell 部署应用到本地
  4. python网络爬虫系列(十一)——JS的解析
  5. javafor循环打印图案_C程序使用循环打印盒子图案
  6. 引入外部js如何通知页面其编码格式
  7. 光纤 matlab,matlab – 均衡光纤通道的最小均方
  8. css实现自适应正方形
  9. instanceof java 原理_JAVA中 instanceof 和 getClass() 区别小结
  10. foobar2000中文版官方下载【多功能的音频播放器】
  11. fragment 淡入淡出_一种模型淡入淡出时透明面重叠问题的解决方案
  12. datatables分页,排序,ajax请求等参数设置
  13. cesium分屏对比
  14. 台式计算机无线接入,台式电脑可以无线连接wifi吗
  15. 最长不含重复字符的字符串
  16. 三、【VUE基础】数据绑定
  17. 2020-04-12
  18. USB协议学习笔记 - CUSTOM HID 设备
  19. linux服务器新装hba卡,Linux更换HBA卡后重新扫盘指令|或者新增HBA卡
  20. LED背光源运用在温控设备上

热门文章

  1. 三个等价c语言表达式,C语言习题综合(20页)-原创力文档
  2. java校招面试题_java校招面试编程题及答案.docx
  3. 未定义变量: data_三、变量声明
  4. matlab求RMSECV,CARS 用于matlab模式识别(分类和回归)的特征变量提取方法 联合开发网 - pudn.com...
  5. 按钮点击打开新页面_PDF怎么打开?如何制作一个PDF格式的文档?
  6. python3.6程序_python3.6如何生成exe程序
  7. 各层作用_终于弄明白了 Singleton,Transient,Scoped 的作用域是如何实现的
  8. mac远程linux的ide,Jupyter notebook在mac:linux上的配置和远程访问
  9. 防抖 节流_【前端面试】节流与防抖
  10. 《软件项目管理(第二版)》第 9 章——项目监督与控制 重点部分总结