Spring - 从属性文件中检索值 [英] Spring - Retrieve value from properties file

查看:56
本文介绍了Spring - 从属性文件中检索值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 applicationContext.xml 中有以下配置:

I have the following configuration in my applicationContext.xml:

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

现在,在我的 java 类中,如何从文件 app.properties 中读取值?

Now, in my java class, how can I read the values from the file app.properties?

推荐答案

实际上,PropertyPlaceholderConfigurer 对使用属性将值注入到 spring 上下文很有用.

Actually PropertyPlaceholderConfigurer is useful to inject values to spring context using properties.

示例 XML 上下文定义:

Example XML context definition:

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
   <property name="driverClassName"><value>${driver}</value></property>
   <property name="url"><value>jdbc:${dbname}</value></property>
</bean>`

示例属性文件:

driver=com.mysql.jdbc.Driver
dbname=mysql:mydb

或者你可以像创建bean一样

Or you can create bean like

<bean name="myBean" value="${some.property.key}" /> 

然后将此 bean 注入您的类

and then inject this bean into your class

这篇关于Spring - 从属性文件中检索值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆