默认情况下,Spring Boot 中是不包含任何的 Spring 配置文件的,即使我们手动添加 Spring 配置文件到项目中,也不会被识别。那么 Spring Boot 项目中真的就无法导入 Spring 配置吗?答案是否定的。
Spring Boot 为了我们提供了以下 2 种方式来导入 Spring 配置:
使用 @ImportResource 注解加载 Spring 配置文件
使用全注解方式加载 Spring 配置
@ImportResource 导入 Spring 配置文件
在主启动类上使用 @ImportResource 注解可以导入一个或多个 Spring 配置文件,并使其中的内容生效。
- 以 helloworld 为例,在 cgmx9880.service 包下创建一个名为 PersonService 的接口,代码如下。
package cgmx9880.service;
import cgmx9880.bean.Person;
public interface PersonService {
public Person getPersonInfo();
}
- 在 cgmx9880.service.impl 包下创建一个名为 PersonServiceImpl 的类,并实现 PersonService 接口,代码如下。
package cgmx9880.service.impl;
import ncgmx9880.bean.Person;
import cgmx9880.service.PersonService;
import org.springframework.beans.factory.annotation.Autowired;
public class PersonServiceImpl implements PersonService {
@Autowired
private Person person;
@Override
public Person getPersonInfo() {