【问题标题】:Spring framework: load properties files, is the file read everytime a bean referencing it is created?Spring框架:加载属性文件,每次创建引用它的bean时都会读取文件吗?
【发布时间】:2014-07-10 19:43:46
【问题描述】:

我对春天还很陌生,还没有时间参加关于它的短期课程。我边走边学。我有一个 spring-app.xml 具有以下导入:

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 
    <property name="locations">
        <list>
            <value>classpath:jdbc.properties</value>
        </list>
    </property>
</bean>

我在我的 maven pom 文件中定义了类路径,它可以工作。现在当我定义一个 bean 时说

<bean id="DataLoader" class="com.abc.DataLoader" scope="prototype">
    <property name="dbDriver" value="${database.driver}"/>
    <property name="dbUrl" value="${database.url}"/>
    <property name="dbUserName" value="${database.username}"/>
    <property name="dbPassword" value="${database.password}"/>
</bean>

我需要 bean 作为原型。每次创建这个bean时spring会读取属性文件'jdbc.properties'还是只读取一次并注入值?我确信这是一个非常基本的问题,但我没有找到答案。这将极大地影响我的程序的性能,因为我每分钟(几乎)创建这个 bean。

【问题讨论】:

    标签: java spring properties-file spring-bean


    【解决方案1】:

    由于PropertyPlaceholderConfigurersingleton,它只会在创建上下文时初始化一次,然后进一步使用它的引用来解析值

    【讨论】:

    • 我需要将其定义为单例还是默认为单例? (根据您的评论,默认情况下它看起来是单例,但只是确认)
    猜你喜欢
    • 2011-11-23
    • 1970-01-01
    • 2014-01-31
    • 2021-02-13
    • 1970-01-01
    • 1970-01-01
    • 2018-05-24
    • 2021-01-14
    • 2018-06-18
    相关资源
    最近更新 更多