Spring Boot学习4——Spring Boot加载自定义配置文件

本文介绍了三种在Spring Boot中加载自定义配置的方法:@PropertySource加载properties文件,@ImportResource加载XML配置文件,以及使用@Configuration编写自定义配置类。通过详细步骤展示了如何创建项目、配置文件,以及编写测试类进行验证。

几种方法

  • 使用@PropertySource加载配置文件
  • 使用@ImportResource加载XML配置文件
  • 使用@Configuration编写自定义配置类

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

一、创建项目ConfigDemo01

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

二、创建自定义配置文件-myconfig.properties

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

  • 编辑属性文件值的时候不允许出现user.name,会与系统属性冲突。如果属性值含有中文,必须采用Unicode编码,否则乱码。

转码: 打开Terminal面板,输入以下命令,将转码结果粘贴至你创建的properties文件中即可(事先IDE编码为utf-8)

cd src/main/resources

native2ascii -encoding utf8 配置文件名.properties

三、创建学生配置类-StudentConfig

  • 创建config子包
    在这里插入图片描述
package net.lj.lesson4.config;

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

/**
 * 学生配置类
 */
@Component//交给Spring容器管理
@PropertySource("classpath:myconfig.properties")//加载自定义配置文件
@ConfigurationProperties(prefix = "student")//配置属性,设置前缀
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 +
                '}';
    }
}

四、打开测试类编辑测试方法

在这里插入图片描述

package net.lj.lesson4;

import net.lj.lesson4.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
    public void testStudentConfig(){
        //输出学生配置实体信息
        System.out.println(studentConfig);
    }

}

运行测试

在这里插入图片描述
将结果输出到网页界面

  • 创建ConfigDemoController.java
    在这里插入图片描述
package net.lj.lesson4.controller;

import net.lj.lesson4.config.StudentConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
//@Controller //使用此注释必须同时加上@ResponseBody注释
public class ConfigDemoController {
    @Autowired
    private StudentConfig studentConfig;

//    @ResponseBody
    @GetMapping("/student")
    public String student() {
          return studentConfig.toString();
    }
}
  • 运行项目,在浏览器地址栏输入http://localhost:8080/student
    在这里插入图片描述

使用@ImportResource加载XML配置文件

一、创建项目ConfigDemo02

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

二、创建自定义服务类

  • 创建service子包
    在这里插入图片描述
package net.lj.lesson04.service;

/**
 * 自定义服务类
 */
public class CustomService {
    public void welcome() {
        System.out.println("欢迎来到Spring Boot世界");
    }
}

三、创建自定义Bean配置文件

在这里插入图片描述

  • 定义一个bean并指明路径
<bean name="customService" class="net.lj.lesson04.service.CustomService"></bean>

四、在启动类上加载自定义Bean配置文件的注释

  • 使Spring容器中自动实例化一个名为customService的Bean
    在这里插入图片描述

五、编写测试类

package net.lj.lesson04;

import net.lj.lesson04.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
    public void testCustomService(){
       customService.welcome();
   }
}

运行测试方法

在这里插入图片描述

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

一、创建项目ConfigDemo03

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

二、创建自定义服务类

  • 创建service子包
    在这里插入图片描述
package net.lj.lesson04.service;

/**
 * 自定义服务类
 */
public class CustomService {
    public void welcome() {
        System.out.println("欢迎来到Spring Boot世界");
    }
}

三、创建自定义配置类CustomConfig

  • 创建config子包
    在这里插入图片描述
package net.lj.lesson04.config;

/*
*自定义配置类
*/
import net.lj.lesson04.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();
    }
}

四、编写测试方法

package net.lj.lesson04;

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

@SpringBootTest
class ConfigDemo03ApplicationTests {
    @Autowired//自动注入实体
    private CustomService customService;

    @Test
    public void testCustomService(){
        customService.welcome();
    }

}

运行测试方法

在这里插入图片描述

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值