eclipse junit/spring 属性未随应用程序上下文加载
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4215530/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
junit/spring properties not loading with application context
提问by camada
While running a junit test, I cannot get the application context to load properties from external properties files.
在运行 junit 测试时,我无法获取应用程序上下文以从外部属性文件加载属性。
Given the following:
鉴于以下情况:
TestClass
测试类
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:spring/app-config.xml")
public class JdbcWatsonDaoTests {
@Autowired
JdbMyDao jdbcMyDao;
@Before
public void setUp() throws Exception {
}
@Test
public void testMethod() {
doSomeStuff();
}
}
app-config.xml
应用程序配置文件
<util:properties id="aProperties" location="classpath:spring/a.properties" />
<util:properties id="bProperties" location="classpath:spring/b.properties" />
<bean id="oracleDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/>
<property name="url" value="${oracle.url}"/>
<property name="username" value="${oracle.username}"/>
<property name="password" value="${oracle.password}"/>
</bean>
and the a.properties and b.properties files are in the same location as app-config.xml...
并且 a.properties 和 b.properties 文件与 app-config.xml 位于同一位置...
I've found that when running the test, the properties placeholders (the literal "${property}" )are what is being sent to the oracle server instead of the values in the properties files.
我发现在运行测试时,属性占位符(文字 "${property}" )是发送到 oracle 服务器的内容,而不是属性文件中的值。
I've also tried using a bean configuration using PropertyPlaceholderConfigurer instead of , but it still doesn't find/include properties.
我还尝试使用使用 PropertyPlaceholderConfigurer 而不是 的 bean 配置,但它仍然没有找到/包含属性。
I am using eclipse helios, spring 3.0.5, newest release m2eclipse and 4.4 junit. I had to downgrade junit for a different maven/junit bug.
我正在使用 eclipse helios、spring 3.0.5、最新版本的 m2eclipse 和 4.4 junit。我不得不为不同的 maven/junit 错误降级 junit。
When published within tomcat, the properties are read and properly used. I only see the problem when running a junit test.
在 tomcat 中发布时,这些属性会被读取并正确使用。我只在运行 junit 测试时看到问题。
回答by Ralph
According to your exception:
根据您的例外:
org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is org.apache.commons.dbcp.SQLNestedException: Cannot create PoolableConnectionFactory (ORA-01017: invalid username/password; logon denied
org.springframework.jdbc.CannotGetJdbcConnectionException: 无法获得 JDBC 连接;嵌套异常是 org.apache.commons.dbcp.SQLNestedException:无法创建 PoolableConnectionFactory(ORA-01017:无效的用户名/密码;登录被拒绝
Your probelm is NOT that the properties are not found, if the properties are not found the exception would be something like org.springframework.beans.factory.BeanDefinitionStoreException: ... Could not resolve placeholder 'oracle.username'
您的问题不是找不到属性,如果找不到属性,则异常将类似于 org.springframework.beans.factory.BeanDefinitionStoreException: ... Could not resolve placeholder 'oracle.username'
And this is because you need to configure a PropertyPlaceholderConfigurer instead of a PropertiesFactoryBean(this is what util:properties does http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/htmlsingle/spring-framework-reference.html#xsd-config-body-schemas-util-properties)
这是因为您需要配置 PropertyPlaceholderConfigurer 而不是 PropertiesFactoryBean(这是 util:properties 所做的http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/htmlsingle/spring- framework-reference.html#xsd-config-body-schemas-util-properties)
<bean id="propertyPlaceHolderConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:spring/a.properties</value>
<value>classpath:spring/a.properties</value>
</list>
</property>
</bean>
回答by Ricardo García
You may separate your test configuration files, spring context, jbdc.properties, into src/test/resourcesdir to respect maven structure files. To configure special propertiesfiles for test you have to define them in test spring application context using propertyPlaceHolderConfigureras Ralphsaid.
您可以将测试配置文件、spring 上下文、jbdc.properties 分离到src/test/resources目录中以尊重 maven 结构文件。要配置特殊的属性文件测试你在使用测试Spring应用程序上下文来定义它们propertyPlaceHolderConfigurer为拉尔夫说。
Property files must be in src/test/resourcesand load them with the slash and file name /a.properties. Place the file in the same directory as the spring configuration file to load it.
属性文件必须在src/test/resources 中,并使用斜杠和文件名/a.properties加载它们。将文件放在与spring配置文件相同的目录下进行加载。
<bean id="propertyPlaceHolderConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>/a.properties</value>
<value>/a.properties</value>
</list>
</property>
</bean>
回答by camada
I gave up. I downloaded a fresh copy of Eclipse 3.6 for Java EE and followed the springsource tool suite installation instuctions via the update site method.
我放弃。我为 Java EE 下载了 Eclipse 3.6 的新副本,并通过更新站点方法遵循 springsource 工具套件安装说明。
I imported my project to the new environment with a new workspace and everythign works just fine.
我将我的项目导入到具有新工作区的新环境中,并且一切正常。
I'll chalk it up to eclipse gnomes.
我会把它归结为日食侏儒。
回答by LeonT
It appears you're using maven. It would help to know where you put the files. By convention, the test version of the properties files should go in src/test/resources/ and production version in src/main/resources. They should resolve automatically.
看来您正在使用 maven。知道你把文件放在哪里会很有帮助。按照惯例,属性文件的测试版本应该在 src/test/resources/ 中,生产版本应该在 src/main/resources 中。他们应该自动解决。