SpringBoot加载自定义配置文件

零、学习目标

一、为什么要使用自定义配置

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

(1).创建Spring Boot Web项目ConfigDemo01

请添加图片描述

请添加图片描述
–** 为了在属性文件中使用中文,设置一下idea如下

请添加图片描述

(2)在resources下创建myconfig.properties文件

请添加图片描述

说明:如果在配置文件里使用user.name,通过配置文件获取的值可能是操作系统中的用户名,因为操作系统中也是有user.name属性的。

请添加图片描述

(3)创建自定义配置类

–**在net.dw.boot.config包里创建配置类StudentConfig

package net.dw.boot.config;

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

@Component
@PropertySource("classpath:myconfig.properties") //加载自定义配置文件
@ConfigurationProperties(prefix = "student")
public class StudentConfig {
    private String id; //学号
    private String name; //姓名
    private String gender; //性别
    private int age;//年龄

    @Override
    public String toString() {
        return "StudentConfig{" +
                "id='" + id + '\'' +
                ", name='" + name + '\'' +
                ", gender='" + gender + '\'' +
                ", age=" + 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 String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public int getAge() {
        return age;
    }

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

请添加图片描述

(4)编写测试方法

编写测试方法,注入学生配置实体,创建testStudentConfig()测试方法,在里面输出学生配置实体信息
请添加图片描述

(5)修改测试方法代码

说明:注入的StudentConfig名称不必是studentConfig,在Spring Boot 2.4.5里,StudentConfig的注解@Component默认是单例的,因此不会因为注入名称是studentConfig1而产生的两个StudentConfig实例。

请添加图片描述

可以看到,StudentConfig注入名称改成studentConfig1之后,测试结果依然相同,不受注入名称变化的任何影响。

(6)在web上显示

创建controller子包,在子包里控制器ConfigDemo01Controller

package net.dw.boot.controller;

import net.dw.boot.config.StudentConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ConfigDemo01Controller {

    @Autowired
    private StudentConfig studentConfig;
    @RequestMapping(value = "/student",method = RequestMethod.GET)
    public String student(){

        return studentConfig.toString();

    }

}

请添加图片描述

在浏览器里访问http://localhost:8080/student

请添加图片描述

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

(1)、建创建Spring Boot Web项目ConfigDemo02

请添加图片描述

请添加图片描述

(2)在net.dw.boot包里创建service子包,在子包里创建CustomService类

package net.dw.boot.service;







public class CustomService {
    public void welcome(){
        System.out.println("welcome to this place");

    }


}

请添加图片描述

(3)在resources目录里创建配置文件spring-config.xml

请添加图片描述

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">



        <!--    定义一个Bean,指定Bean的名称及类所在的路径-->
        <bean name="customService" class="net.dw.boot.service.CustomService"/>
</beans>

请添加图片描述

(4).在启动类上添加注解,加载自定义Spring配置文件

在启动类上添加注解@ImportResource(“classpath:spring-config.xml”)

请添加图片描述

在Spring Boot启动后,Spring容器中就会自动实例化一个名为customService的Bean对象

(5)打开测试类,编写测试方法

点开测试类ConfigDemo02ApplicationTests

package net.dw.boot;

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

@SpringBootTest
class ConfigDemo02ApplicationTests {
    @Autowired //注入自定义服务实体
    private CustomService customService;


    @Test
    void contextLoads() {
    }

    @Test
    public void TestCustomService(){


        customService.welcome();
    }
}

请添加图片描述

请添加图片描述

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

使用@Configuration编写自定义配置类,这是Spring Bboot的推荐方式

(1)创建Spring Boot Web项目ConfigDemo03

请添加图片描述

(2)新建service包并创建自定义服务类CustomService类请添加图片描述

package net.dw.boot.service;

public class CustomService {

    public void welcome() {

        System.out.println("welcome to the famous place ");
    }

}

(3)创建自定义配置类CustomConfig

在net.dw.boot包里创建config子包,在子包里创建自定义配置类CustomConfig

添加注解@Configuration,指定配置类

package net.dw.boot.config;

import net.dw.boot.service.CustomService;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


@Configuration
public class CustomConfig {
    @Bean(name = "cs") //指定Bean的名称为“cs” ,否则采用默认的customService 作为名称
    public CustomService getCustomService(){
        return new CustomService();

    }

}

请添加图片描述

(4)打开测试类,编写测试方法

–** 注入在CustomConfig配置类里定义的Bean,创建测试方法testCustomService(),然后调用自定义Bean的方法**

package net.dw.boot;

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

@SpringBootTest
class ConfigDome03ApplicationTests {


    @Autowired
    private CustomService cs;
    @Test

    void contextLoads() {
    }

    @Test
    public void testCustomService() {

        cs.welcome();

    }
}

请添加图片描述

请添加图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值