将Maven属性传递给Spring

将Maven属性传递给Spring

问题描述:

我知道这可能是一个愚蠢的问题,但我无法终生解决.基本上,我使用maven来设置我的dataSource用户名,密码和驱动程序类名称.当我查看有效的Pom.xml时,一切看起来都很好,如下所示

I know this is probably a dumb question but I can't figure it out for the life of me. Basically I am using maven to set my dataSource username, password, and driver class name. When I look in the effective Pom.xml it all appears fine as follows

<dataSource.driverClassName>oracle.jdbc.driver.OracleDriver</dataSource.driverClassName>
<dataSource.username>someUsername</dataSource.username>
<dataSource.password>somePassword</dataSource.password>

我在声明spring数据源时尝试使用此信息.代码显示如下.

I am trying to use this information when declaring a spring datasource. The code appears as follows.

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    <property name="driverClassName" value="${dataSource.driverClassName}"/>
    <property name="url" value="${dataSource.url}"/>
    <property name="username" value="${dataSource.username}"/>
    <property name="password" value="${dataSource.password}"/>
</bean>

然后我将数据源传递到jdbcTemplate中,但是当我使用该模板在代码中运行sql语句时,出现一条错误消息,提示找不到名为$ {dataSource.driverClassName}的驱动程序.这显然是因为要传递字符串常量而不是变量.我想念什么?

I then pass the datasource into a jdbcTemplate but when I use the template to run sql statements in my code I get an error saying that no driver with the name ${dataSource.driverClassName} can be found. This is obviously because the string constant is being passed rather than the variable. What am I missing?

谢谢

Spring希望在.properties文件中找到这些值.

Spring is expecting to find those values in a .properties file.

注入具有这些值的PropertyConfigurationPlaceholder,Spring会找到它们.

Inject a PropertyConfigurationPlaceholder with those values and Spring will find them.

http://static.springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework/beans/factory/config/PropertyPlaceholderConfigurer.html

例如下面的3.8.2.1:

Look for example 3.8.2.1 below:

http://static. springsource.org/spring/docs/3.0.x/spring-framework-reference/html/beans.html