[英]junit/spring properties not loading with application context
While running a junit test, I cannot get the application context to load properties from external properties files. 在运行junit测试时,我无法获取应用程序上下文来从外部属性文件加载属性。
Given the following: 鉴于以下内容:
TestClass 识别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 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. 我正在使用eclipse helios,spring 3.0.5,最新版本m2eclipse和4.4 junit。 I had to downgrade junit for a different maven/junit bug.
我不得不降级junit以获得不同的maven / junit bug。
When published within tomcat, the properties are read and properly used. 在tomcat中发布时,将读取并正确使用这些属性。 I only see the problem when running a junit test.
我只在运行junit测试时看到问题。
According to your exception: 根据你的例外情况:
org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection;
org.springframework.jdbc.CannotGetJdbcConnectionException:无法获取JDBC连接; nested exception is org.apache.commons.dbcp.SQLNestedException: Cannot create PoolableConnectionFactory (ORA-01017: invalid username/password; logon denied
嵌套异常是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>
You may separate your test configuration files, spring context, jbdc.properties, into src/test/resources dir to respect maven structure files. 您可以将测试配置文件,spring上下文,jbdc.properties分隔为src / test / resources目录,以尊重maven结构文件。 To configure special properties files for test you have to define them in test spring application context using propertyPlaceHolderConfigurer as Ralph said.
要配置用于测试的特殊属性文件,您必须使用propertyPlaceHolderConfigurer在测试弹簧应用程序上下文中定义它们,如Ralph所说。
Property files must be in src/test/resources and load them with the slash and file name /a.properties . 属性文件必须位于src / test / resources中 ,并使用斜杠和文件名/a.properties加载它们。 Place the file in the same directory as the spring configuration file to load it.
将文件放在与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>
It appears you're using maven. 看来你正在使用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.
按照惯例,属性文件的测试版本应该放在src / test / resources /和src / main / resources中的生产版本中。 They should resolve automatically.
他们应该自动解决。
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.
我下载了Eclipse 3.6 for Java EE的全新副本,并通过更新站点方法遵循了泉源工具套件安装实例。
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. 我会把它用来遮蔽侏儒。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.