Spring Boot导入Spring配置

默认情况下,Spring Boot 中是不包含任何的 Spring 配置文件的,即使我们手动添加 Spring 配置文件到项目中,也不会被识别。那么 Spring Boot 项目中真的就无法导入 Spring 配置吗?答案是否定的。

Spring Boot 为了我们提供了以下 2 种方式来导入 Spring 配置:
使用 @ImportResource 注解加载 Spring 配置文件
使用全注解方式加载 Spring 配置
@ImportResource 导入 Spring 配置文件
在主启动类上使用 @ImportResource 注解可以导入一个或多个 Spring 配置文件,并使其中的内容生效。

  1. 以 helloworld 为例,在 cgmx9880.service 包下创建一个名为 PersonService 的接口,代码如下。
package cgmx9880.service;
import cgmx9880.bean.Person;
public interface PersonService {
   
    public Person getPersonInfo();
}
  1. 在 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() {
   
        
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值