论坛首页 Java企业应用论坛

奇技淫巧Spring--属性格式的配置文件

浏览 7957 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2005-02-18  
挖spring的代码发现的
spring自带读取属性格式的方法
用到两个不常用的类
PropertiesBeanDefinitionReader ,GenericApplicationContext
/*
 * Created on 2005-2-18
 * <p>Copyright: Copyright (c); 2004</p>
 * <p>Company: Hygrand</p> 
 */
package demo;

import java.util.ResourceBundle;

import org.springframework.beans.factory.support.PropertiesBeanDefinitionReader;
import org.springframework.context.support.GenericApplicationContext;

/**
 * @author gongy
 * @version $Id$
 */
public class PropertyBDDemo {

    public static void main(String[] args); {
        GenericApplicationContext ctx = new GenericApplicationContext();;
        PropertiesBeanDefinitionReader pdb = new PropertiesBeanDefinitionReader(
                ctx);;

        ResourceBundle rb = ResourceBundle.getBundle("bd");;
        pdb.registerBeanDefinitions(rb);;        
        System.out.println(((World);ctx.getBean("world"););.getHello();.getI(););;
    }

}


bd.properties内容
hello.class=demo.Hello
hello.i=1
world.class=demo.World
world.hello(ref);=hello
   发表时间:2005-02-19  
你这个忒麻烦了,有现成的,spring文档41页.
引用

The PropertyPlaceholderConfigurer has already been described, as used with a BeanFactory. It is worth
mentioning here though, that it is generally more convenient to use it with an ApplicationContext, since the
context will automatically recognize and apply any bean factory post-processors, such as this one, when they
are simply deployed into it like any other bean. There is no need for a manual step to execute it.
<!-- property placeholder post-processor -->
<bean id="placeholderConfig"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location"><value>jdbc.properties</value></property>
</bean>

不用在程序里写
jdbc.properties的位置是按classpath来的,我想把配置文件放到classpath外面去,不晓得怎么搞,哪位知道?
0 请登录后投票
   发表时间:2005-02-19  
求人不如求己,暴力方法解决,你不是要resource吗?我给你个FileSystemResource.
什么?没setPath()?继续暴力,给构造参数........
<!--about config-->
    <bean id="placeholderConfig" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="location"><ref bean="configResource"/></property>
	</bean>    
    
    <bean id="configResource" class="org.springframework.core.io.FileSystemResource" singleton="false">
    	<constructor-arg type="java.lang.String"><value>conf/jdbc.properties</value></constructor-arg>    	
    </bean>
0 请登录后投票
   发表时间:2005-02-20  
Xiaohanne 写道
你这个忒麻烦了,有现成的,spring文档41页.
引用

The PropertyPlaceholderConfigurer has already been described, as used with a BeanFactory. It is worth
mentioning here though, that it is generally more convenient to use it with an ApplicationContext, since the
context will automatically recognize and apply any bean factory post-processors, such as this one, when they
are simply deployed into it like any other bean. There is no need for a manual step to execute it.
<!-- property placeholder post-processor -->
<bean id="placeholderConfig"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location"><value>jdbc.properties</value></property>
</bean>

不用在程序里写
jdbc.properties的位置是按classpath来的,我想把配置文件放到classpath外面去,不晓得怎么搞,哪位知道?


大哥误会了不是
怪我没有说清楚

我上面写的方法是把原来spring的ioc配置文件(就是常写的那个xml)写成properties文件
请再仔细看看
0 请登录后投票
   发表时间:2005-02-20  
晕,我看花了,我刚好在找怎么把${}提出去的方法。。。
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics