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文件

#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定义,这个解析器的原理很简单,在此不做详细分析,下面是实例代码。

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来完成这种需求,看下这个类的代码。

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来完成这种扩展。



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值