Spring Boot基础学习笔记04:Spring Boot加载自定义配置文件

一、使用@PropertySource加载自定义配置文件

1、创建自定义配置文件

  • 在resources下创建myconfig.properties文件
    -在这里插入图片描述
    在这里插入图片描述

2、创建自定义配置类

在这里插入图片描述

package lesson4;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;


@Component //让Spring容器来管理Bean
@PropertySource("classpath:myconfig.properties") //加载自定义配置文件
@ConfigurationProperties(prefix = "student") // 此注解必须要Component注解
public class StudentConfig {
    private String id;
    private String name;
    private int age;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "StudentConfig{" +
                "id='" + id + '\'' +
                ", name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

3、编写测试方法

package lesson4;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class ConfigDemo01ApplicationTests {
    @Autowired // 注入学生配置实体
    private StudentConfig studentConfig;

    @Test
    void contextLoads() {
        // 输出学生配置实体信息
        System.out.println(studentConfig.toString());
    }
}

4、运行测试方法

在这里插入图片描述

二、使用@ImportResource加载XML配置文件

1、创建Bean - 自定义服务类

在这里插入图片描述

package lesson4;

public class CustomService {
    public void welcome() {
        System.out.println("欢迎您访问泸州职业技术学院");
    }
}

2、创建自定义JavaBean配置文件

在这里插入图片描述
在这里插入图片描述

3、在启动类上添加注解,加载自定义JavaBean配置文件

  • 在启动类上添加注解@ImportResource(“classpath:beans.xml”)
    在这里插入图片描述

4、编写测试方法

package lesson4;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class ConfigDemo02ApplicationTests {
    // 注入自定义Bean
    @Autowired
    private CustomService customService;

    @Test
    void contextLoads() {
        // 调用自定义Bean的方法
        customService.welcome();
    }
}

  • 运行测试方法结果
    在这里插入图片描述

三、使用@Configuration编写自定义配置类

1、创建Bean - 自定义服务类

在这里插入图片描述

package lesson4;

public class CustomService {
    public void welcome() {
        System.out.println("欢迎您访问泸职院信息工程学院");
    }
}

2、创建自定义配置类MyConfig

在这里插入图片描述

  • 添加注解@Configuration,指定配置类
package lesson4;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class  MyConfig {
    @Bean(name = "cs")
    public CustomService getCustomService() {
        return new CustomService();
    }
}

3、打开测试类,编写测试方法

在这里插入图片描述

package lesson4;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class ConfigDemo03ApplicationTests {
    // 注入自定义Bean
    @Autowired
    private CustomService cs;

    @Test
    void contextLoads() {
        // 调用自定义Bean的方法
        cs.welcome();
    }
}

  • 运行结果
    在这里插入图片描述
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值