题目
Spring Boot中不能使用XML配置文件。A. 正确B. 错误
Spring Boot中不能使用XML配置文件。
A. 正确
B. 错误
题目解答
答案
B. 错误
解析
本题考查Spring Boot中配置文件的使用方式相关知识点。解题思路是明确Spring Boot虽然提倡使用Java配置和属性文件(如application.properties或application.yml)进行配置,但并不意味着不能使用XML配置文件。
在Spring Boot里,XML配置文件依然是可以使用的。Spring Boot提供了多种方式来集成XML配置。例如,我们可以通过@ImportResource注解来导入XML配置文件。以下是一个简单示例:
假设我们有一个XML配置文件beans.xml,内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="
xmlns:xsi="
xsi:schemaLocation=" /spring-beans.xsd">
<bean id="exampleBean" class="com.example.ExampleBean">
<property name="message" value="Hello, World!"/>
</bean>
</beans>
然后在Spring Boot的配置类中使用@ImportResource注解导入该XML文件:
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
@Configuration
@ImportResource("classpath:beans.xml")
public class AppConfig {
// 其他配置代码
}
通过这种方式,Spring Boot就可以加载并使用XML配置文件中的Bean定义。所以“Spring Boot中不能使用XML配置文件”这一说法是错误的。