Spring FileSystemXmlApplicationContext getBeanDefinitionNames返回空数组

时间:2016-05-06 20:38:10

标签: java spring debugging

我有一个活跃的个人资料问题。 决定在我的上下文中查看调试模式
(在我的情况下,配置文件独立的上下文以这种方式工作 -
getBeanDefinitionNames返回空数组。)

带有上述文件的新FileSystemXmlApplicationContext在代码片段模式下无异常地工作。

代码

new FileSystemXmlApplicationContext(
            "D:\\Projects\\opti\dao\\src\\main\\resources\\contexts\\" +
"\\profiles\\test.xml")

此上下文与

getBeanDefinitionNames()

返回空数组。

d:\项目\ OPTI \ DAO \ SRC \主\资源\上下文\型材\的test.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:context="http://www.springframework.org/schema/context"
   xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/context     http://www.springframework.org/schema/context/spring-context.xsd">
<beans profile="test">
    <context:property-placeholder properties-ref="properties" ignore-resource-not-found="false"/>
    <bean id="properties" lazy-init="false"
              class="org.springframework.beans.factory.config.PropertiesFactoryBean">
        <property name="locations">
            <list>
                <value>classpath*:properties/test-database.properties</value>
            </list>
        </property>
    </bean>
</beans>
</beans>

春季4.2.5

d:\项目\ OPTI \ DAO \ SRC \主\资源\特性\ test-database.properties

connection.driverClassName=org.hsqldb.jdbc.JDBCDriver
connection.url=jdbc:hsqldb:mem:test
connection.userName=sa
connection.password=

1 个答案:

答案 0 :(得分:1)

对于特定配置文件的使用bean,您应该使用-Dspring.profiles.active = test启动jvm,或者将spring.profiles.active = test设置为env变量System.setProperties(props)可以是一种配置它的方法

我希望这可以帮到你