访问Spring配置文件中的属性值(Access property values inside Spring config file)

编程入门 行业动态 更新时间:2024-10-25 10:32:48
访问Spring配置文件中的属性值(Access property values inside Spring config file)

我在WEB-INF/local.db.properties有一个简单的属性文件:

db.driverClassName=org.postgresql.Driver db.url=jdbc:postgresql://localhost:5432/db_name db.username=postgres db.password=password

我试图在我的Spring配置文件中访问这些属性(到最后):

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:util="http://www.springframework.org/schema/util"
    xsi:schemaLocation="
         http://www.springframework.org/schema/beans
         http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
         http://www.springframework.org/schema/util
         http://www.springframework.org/schema/util/spring-util-3.1.xsd
         http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd
         http://www.springframework.org/schema/context
         http://www.springframework.org/schema/context/spring-context-3.0.xsd">

 <context:component-scan base-package="controllers" />

 <mvc:annotation-driven/>
 <mvc:default-servlet-handler/>

 <bean id="tilesConfigurer" class="org.springframework.web.servlet.view.tiles3.TilesConfigurer">
     <property name="definitions">
         <list>
             <value>/WEB-INF/tiles-config.xml</value>
         </list>
     </property>
 </bean>

 <bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
     <property name="viewClass" value="org.springframework.web.servlet.view.tiles3.TilesView"/>
 </bean>

 <util:properties id="dbProperties" location="WEB-INF/local.db.properties" />
 <bean id="dbDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
     <property name="driverClassName" value="${db.driverClassName}" /> <!--ERROR -->
     <property name="url" value="${db.url}" />
     <property name="username" value="${db.username}" />
     <property name="password" value="${db.password}" />
 </bean>
</beans>
 

错误是Could not load JDBC driver class [${db.driverClassName}] 。 错误对我来说非常清楚 - 它不是试图查找它只是使用原始字符串的属性。

我该如何解决?

这是一个Maven 3,Spring 4,Postgresql项目。

I have a simple properties file at WEB-INF/local.db.properties:

db.driverClassName=org.postgresql.Driver db.url=jdbc:postgresql://localhost:5432/db_name db.username=postgres db.password=password

I am trying to access these properties inside of my Spring configuration file (towards the end):

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:util="http://www.springframework.org/schema/util"
    xsi:schemaLocation="
         http://www.springframework.org/schema/beans
         http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
         http://www.springframework.org/schema/util
         http://www.springframework.org/schema/util/spring-util-3.1.xsd
         http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd
         http://www.springframework.org/schema/context
         http://www.springframework.org/schema/context/spring-context-3.0.xsd">

 <context:component-scan base-package="controllers" />

 <mvc:annotation-driven/>
 <mvc:default-servlet-handler/>

 <bean id="tilesConfigurer" class="org.springframework.web.servlet.view.tiles3.TilesConfigurer">
     <property name="definitions">
         <list>
             <value>/WEB-INF/tiles-config.xml</value>
         </list>
     </property>
 </bean>

 <bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
     <property name="viewClass" value="org.springframework.web.servlet.view.tiles3.TilesView"/>
 </bean>

 <util:properties id="dbProperties" location="WEB-INF/local.db.properties" />
 <bean id="dbDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
     <property name="driverClassName" value="${db.driverClassName}" /> <!--ERROR -->
     <property name="url" value="${db.url}" />
     <property name="username" value="${db.username}" />
     <property name="password" value="${db.password}" />
 </bean>
</beans>
 

The error is Could not load JDBC driver class [${db.driverClassName}]. The error is pretty clear to me - it is not trying to look up the property it is just using the raw string.

How do I fix this?

This is a Maven 3, Spring 4, Postgresql project.

最满意答案

您没有在Spring配置中加载属性文件。 添加此行:

<context:property-placeholder location="classpath:/WEB-INF/local.db.properties"/>
 

要么

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="location" value="/WEB-INF/local.db.properties"/>
</bean>
 

如果您有多个属性文件,请更改以上配置:

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <!-- locations, not location, check the S at the end -->
    <property name="locations">
        <list>
            <value>
                /WEB-INF/local.db.properties
            </value>
            <value>
                <!-- Path of another properties file -->
            </value>
        </list>
    </property>
</bean>
 

更多信息: PropertySourcesPlaceholderConfigurer

You're not loading the properties file in Spring configuration. Add this line:

<context:property-placeholder location="classpath:/WEB-INF/local.db.properties"/>
 

or

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="location" value="/WEB-INF/local.db.properties"/>
</bean>
 

If you have several properties file, change the above configuration for:

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <!-- locations, not location, check the S at the end -->
    <property name="locations">
        <list>
            <value>
                /WEB-INF/local.db.properties
            </value>
            <value>
                <!-- Path of another properties file -->
            </value>
        </list>
    </property>
</bean>
 

More info: PropertySourcesPlaceholderConfigurer

更多推荐

db,org,springframework,电脑培训,计算机培训,IT培训"/> <meta name="descri

本文发布于:2023-08-01 15:46:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1361293.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:配置文件   属性   Access   Spring   config

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!