第七课 从零开始学Spring boot 之 (自定义配置文件资源获取,扫描指定包下的文件)

本文主要是介绍第七课 从零开始学Spring boot 之 (自定义配置文件资源获取,扫描指定包下的文件),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

1、自定义配置文件资源获取

    spring boot使用application.properties默认了很多配置。但需要自己添加一些配置的时候,我们应该怎么做呢。

 (1)、继续在application.ppoperties中添加

gh.username=gongh
gh.password=123456

    定义配置类:

package com.gongh.config;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;/*** 自定义的配置文件* @author gh*/
@ConfigurationProperties(prefix = "gh") 
public class GhCustom {private String username;private String password;public String getUsername() {return username;}public void setUsername(String username) {this.username = username;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}}

 (2)、新建properties文件

        若新用新的配置文件,如我在application.properties同目录下新建一个ghcustom.properties

    


需定义如下配置类:

package com.gongh.config;import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;/*** 自定义的配置文件* @author gh* 在配置类中采用@Component的方式注册为组件,然后使用@PropertySource来指定自定义的资源目录*/
@Component
@ConfigurationProperties(prefix = "gh") 
@PropertySource("classpath:ghcustom.properties")
public class GhCustom {private String username;private String password;public String getUsername() {return username;}public void setUsername(String username) {this.username = username;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}	}
最后注意在 spring Boot 入口类加上 @EnableConfigurationProperties
/***使用@SpringBootApplication制定这是一个springboot的应用程序**/
@SpringBootApplication
@ServletComponentScan //是的spring能够扫描到我们自己编写的servlet和filter。
@EnableConfigurationProperties({GhCustom.class}) //加载自定义properties配置文件
public class App extends WebMvcConfigurerAdapter{public static void main(String[] args) {  SpringApplication.run(App.class, args);  }  
}	  

使用定义的properties

@RestController
public class TestController {@Autowiredprivate GhCustom ghCustom;@RequestMapping("/test")  public String test(){  String str = "username: "+ ghCustom.getUsername()+"----,password: "+ghCustom.getPassword();System.out.println(str);  return str;  } 
}

打开浏览器,访问http://localhost:8080/test,得到


2、扫描指定包下的文件

     在开发中我们知道Spring Boot默认会扫描启动类同包以及子包下的注解,那么如何进行改变这种扫描包的方式呢,

     原理很简单就是:

     在spring boot的启动类App.java中添加@ComponentScan注解进行指定要扫描的包以及要扫描的类。

 

 首先,我们在项目中新建一个包cn.company

在这里为了方便测试,我们让我们的类在启动的时候就进行执行,所以我们就编写两个类,实现接口CommandLineRunner,这样在启动的时候我们就可以看到打印信息了。

cn.company.MyCommandLineRunner :

package cn.company;
import org.springframework.boot.CommandLineRunner;@Configuration
public class MyCommandLineRunner implements CommandLineRunner {@Overridepublicvoid run(String... args) throws Exception {System.out.println("MyCommandLineRunner run()");}
}

在spring boot的启动类中添加如下注解

@SpringBootApplication
@ServletComponentScan //是的spring能够扫描到我们自己编写的servlet和filter。
@EnableConfigurationProperties({GhCustom.class}) //加载自定义properties配置文件
@ComponentScan(basePackages={"cn.company"})//扫描指定包下的资源
public class App extends WebMvcConfigurerAdapter{public static void main(String[] args) {  SpringApplication.run(App.class, args);  } 
}

启动如果看到打印信息:

MyCommandLineRunner.run()

说明我们配置成功了。

这时候你会发现,在App.java同包下的都没有被扫描了,所以如果也希望App.java包下的也同时被扫描的话,那么在进行指定包扫描的时候一定要进行指定配置:

@ComponentScan(basePackages={"cn.company",com.gh"})




这篇关于第七课 从零开始学Spring boot 之 (自定义配置文件资源获取,扫描指定包下的文件)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/229390

相关文章

Spring Security常见问题及解决方案

《SpringSecurity常见问题及解决方案》SpringSecurity是Spring生态的安全框架,提供认证、授权及攻击防护,支持JWT、OAuth2集成,适用于保护Spring应用,需配置... 目录Spring Security 简介Spring Security 核心概念1. ​Securit

SpringBoot+EasyPOI轻松实现Excel和Word导出PDF

《SpringBoot+EasyPOI轻松实现Excel和Word导出PDF》在企业级开发中,将Excel和Word文档导出为PDF是常见需求,本文将结合​​EasyPOI和​​Aspose系列工具实... 目录一、环境准备与依赖配置1.1 方案选型1.2 依赖配置(商业库方案)二、Excel 导出 PDF

SpringBoot改造MCP服务器的详细说明(StreamableHTTP 类型)

《SpringBoot改造MCP服务器的详细说明(StreamableHTTP类型)》本文介绍了SpringBoot如何实现MCPStreamableHTTP服务器,并且使用CherryStudio... 目录SpringBoot改造MCP服务器(StreamableHTTP)1 项目说明2 使用说明2.1

spring中的@MapperScan注解属性解析

《spring中的@MapperScan注解属性解析》@MapperScan是Spring集成MyBatis时自动扫描Mapper接口的注解,简化配置并支持多数据源,通过属性控制扫描路径和过滤条件,利... 目录一、核心功能与作用二、注解属性解析三、底层实现原理四、使用场景与最佳实践五、注意事项与常见问题六

Spring的RedisTemplate的json反序列泛型丢失问题解决

《Spring的RedisTemplate的json反序列泛型丢失问题解决》本文主要介绍了SpringRedisTemplate中使用JSON序列化时泛型信息丢失的问题及其提出三种解决方案,可以根据性... 目录背景解决方案方案一方案二方案三总结背景在使用RedisTemplate操作redis时我们针对

Java中Arrays类和Collections类常用方法示例详解

《Java中Arrays类和Collections类常用方法示例详解》本文总结了Java中Arrays和Collections类的常用方法,涵盖数组填充、排序、搜索、复制、列表转换等操作,帮助开发者高... 目录Arrays.fill()相关用法Arrays.toString()Arrays.sort()A

Spring Boot Maven 插件如何构建可执行 JAR 的核心配置

《SpringBootMaven插件如何构建可执行JAR的核心配置》SpringBoot核心Maven插件,用于生成可执行JAR/WAR,内置服务器简化部署,支持热部署、多环境配置及依赖管理... 目录前言一、插件的核心功能与目标1.1 插件的定位1.2 插件的 Goals(目标)1.3 插件定位1.4 核

如何使用Lombok进行spring 注入

《如何使用Lombok进行spring注入》本文介绍如何用Lombok简化Spring注入,推荐优先使用setter注入,通过注解自动生成getter/setter及构造器,减少冗余代码,提升开发效... Lombok为了开发环境简化代码,好处不用多说。spring 注入方式为2种,构造器注入和setter

使用zip4j实现Java中的ZIP文件加密压缩的操作方法

《使用zip4j实现Java中的ZIP文件加密压缩的操作方法》本文介绍如何通过Maven集成zip4j1.3.2库创建带密码保护的ZIP文件,涵盖依赖配置、代码示例及加密原理,确保数据安全性,感兴趣的... 目录1. zip4j库介绍和版本1.1 zip4j库概述1.2 zip4j的版本演变1.3 zip4

Java堆转储文件之1.6G大文件处理完整指南

《Java堆转储文件之1.6G大文件处理完整指南》堆转储文件是优化、分析内存消耗的重要工具,:本文主要介绍Java堆转储文件之1.6G大文件处理的相关资料,文中通过代码介绍的非常详细,需要的朋友可... 目录前言文件为什么这么大?如何处理这个文件?分析文件内容(推荐)删除文件(如果不需要)查看错误来源如何避