Spring BootV04:Spring Boot加载自定义配置文件

一、使用@PropertySource加载自定义配置文件
1、创建Spring Boot Web项目ConfigDemo01
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
2、创建自定义配置文件
*在resources下创建myconfig.properties文件
在这里插入图片描述
在这里插入图片描述

3、创建自定义配置类
在net.tjl.lesson04包里创建配置类StudentConfig
在这里插入图片描述

package net.tjl.lesson04.config;

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

/**
 * 功能:学生配置类
 * 作者:谭金兰
 * 日期:2021年05月07日
 */
@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 +
                '}';
    }
}
      

4、编写测试方法

package net.tjl.lesson04;

import net.tjl.lesson04.config.StudentConfig;
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() {

    }
    @Test

        public void testStudentConfig(){
        // 输出学生配置实体信息
        System.out.println(studentConfig);
    }
}

5、运行测试方法
在这里插入图片描述
6、修改测试方法代码
在这里插入图片描述
7、再次运行测试方法
在这里插入图片描述
二、使用@ImportResource加载XML配置文件
1、创建创建Spring Boot Web项目ConfigDemo02
在这里插入图片描述
在这里插入图片描述
2、创建Bean - 自定义服务类
在net.tjl.lesson04包里创建CustomService类
在这里插入图片描述
在这里插入图片描述
3、创建自定义配置文件
*在resources目录里创建配置文件spring-config.xml
在这里插入图片描述
在元素里添加子元素
在这里插入图片描述
4、在启动类上添加注解,加载自定义spring-config配置文件
在这里插入图片描述
5、打开测试类,编写测试方法
在这里插入图片描述
6、运行测试方法testCustomService() (),查看结果
在这里插入图片描述
三、使用@Configuration编写自定义配置类
使用@Configuration编写自定义配置类,这是Spring Bboot的推荐方式
1、创建Spring Boot Web项目ConfigDemo03
在这里插入图片描述
在这里插入图片描述
2、创建 -自定义服务类
在net.tjl.lesson04包里创建CustomService类
在这里插入图片描述

package net.tjl.lesson04.service;
/**
 * 功能:自定义服务类
 * 作者:谭金兰
 * 日期:2021年05月07日
 */
public class CustomService {
    public void welcome() {
        System.out.println("欢迎您访问泸职院信息工程学院");
    }
}

3、创建自定义配置类MyConfig
在net.tjl.lesson04包里创建自定义配置类CustomConfig
在这里插入图片描述
创建获取Bean的方法getCustomService()
在这里插入图片描述
4、打开测试类,编写测试方法
在这里插入图片描述
运行测试方法testCustomService(),查看结果
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值