Spring properties定义bean

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.   
  9. #bean2  
  10. childBean.(class) = spring.beans.properties.ChildBean  
  11. childBean.$0 = cid1  
  12. chlldBean.(scope) = singleton  
  13.   
  14. #abstract bean  
  15. commonBean.(class) = spring.beans.properties.CommonBean  
  16. commonBean.id = 1  
  17. 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.   
  12.     assertNotNull(propBean.getChildBean());  
  13.     assertNotNull(propBean.getChildBean().getCid());  
  14. }  

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

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

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值