我正在尝试以编程方式将一些XML配置导入到Spring应用程序中的当前环境中。这是一些代码:
@SpringBootApplication
public class Application {
public static void main(String[] args) {
ConfigurableApplicationContext app = SpringApplication.run(Application.class, args);
try {
app.getEnvironment().getPropertySources().addLast(new ResourcePropertySource("classpath:conf.xml"));
} catch (IOException e) {
System.out.println("There was an error" + e.getMessage());
} catch (Exception e) {
System.out.println("There was an error" + e.getMessage());
}
}
}
这是我的conf.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"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:defaults.properties</value>
</list>
</property>
<property name="ignoreUnresolvablePlaceholders" value="true"/>
</bean>
我的类路径中也有一个defaults.properties
文件。
当我运行应用程序时,它会使用错误
命中IOException catch块There was an errororg.xml.sax.SAXParseException;
lineNumber: 5; columnNumber: 115; Document root element "beans",
must match DOCTYPE root "null".
我尝试过的事情:
<!DOCTYPE>
声明中将根元素指定为“beans”。然后我得到一个错误,说“bean”元素没有定义有谁知道错误在哪里?
我已经分配了一个Spring指南来显示问题。我的代码示例来自那里:https://github.com/jogjayr/gs-rest-service/tree/master/complete