SpringBoot2.0(Spring读取配置文件常用方法,打war包在Tomcat中启动)

这篇具有很好参考价值的文章主要介绍了SpringBoot2.0(Spring读取配置文件常用方法,打war包在Tomcat中启动)。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

一,SpringBoot中读取配置文件的常用方法

1.1,使用@Value读取

在springBoot声明周期内,可以使用@Value注解从SpringBoot的默认配置文件中读取配置信息

例如在Controller中使用:

    // 在配置文件中读取属性名为web.images-path对应的值
    @Value("${web.images-path}")
    private String path;

@Value可以放到属性或方法上,能够正常使用的前提是所在类,必须在SpringBoot的生命周期内。
我们怎么把一个类放到Spring的生命周期中进行管理?使用的是@Component注解
因为@Controller和@Service本身就包含@Component。所以可以直接使用。

下面是单独使用@Component的例子
创建一个config包,然后创建一个BootProperties

package com.demo.config;

import lombok.Setter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;


@Component
public class BootProperties {

    @Value("${web.images-path}")
    public String path;
}

然后在controller中写

@RestController
public class HelloController {

    @Autowired
    private BootProperties bootProperties;
	
    @RequestMapping("/test5")
    public Object test5(){
        return bootProperties.path;
    }
	
}

SpringBoot2.0(Spring读取配置文件常用方法,打war包在Tomcat中启动),spring,tomcat,spring boot
使用ing类型写

package com.demo.config;

import lombok.Setter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;


@Component
public class BootProperties {

    @Value("${web.images-path}")
    public String path;

	@Value("${server.port}")
    public int port;
}
@RestController
public class HelloController {

    @Autowired
    private BootProperties bootProperties;
	
    @RequestMapping("/test5")
    public Object test5(){
    	return bootProperties.path +  " ------ "+ bootProperties.port;
    }
	
}

SpringBoot2.0(Spring读取配置文件常用方法,打war包在Tomcat中启动),spring,tomcat,spring boot

1.2,使用@ConfigurationProperties

BootProperties类

package com.demo.config;

import lombok.Setter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;

@Setter  // lombok,生成set方法
@ConfigurationProperties(prefix = "server") // 配置属性类,并定制前缀
@Component  // 因为@ConfigurationProperties不能把此类放到boot容器中,所以要配合@Componpent使用
public class BootProperties {

    @Value("${web.images-path}")
    public String path;

    // 不使用@Value注解,需要保证:前缀+属性名=全路径。还需要此属性有对应的setter方法
    // @Value("${server.port}")
    public int port;


    // 使用@Value注解则需要写全路径
}

controller类

@RestController
public class HelloController {

    @Autowired
    private BootProperties bootProperties;
	
    @RequestMapping("/test5")
    public Object test5(){
    	return bootProperties.path +  " ------ "+ bootProperties.port;
    }
	
}

SpringBoot2.0(Spring读取配置文件常用方法,打war包在Tomcat中启动),spring,tomcat,spring boot

1.3,使用Environment

Environment是SpringCore中的一个用于读取配置文件的类,将此类使用@Autowired注入到类中就可以使用它的getProperty方法来获取某个配置项的值

@RestController
public class HelloController {

    @Autowired
    private Environment environment;
	
   	@RequestMapping("/test7")
    public Object test7(){
        return environment.getProperty("server.port");
    }
	
}

SpringBoot2.0(Spring读取配置文件常用方法,打war包在Tomcat中启动),spring,tomcat,spring boot

1.4,自定义配置文件读取

使用之前的知识来理解下面的代码。
主要添加新的注解@PropertySource

创建一个config包,然后创建一个SysProperties

package com.demo.config;

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

@ConfigurationProperties(prefix = "sys")
@Component
@PropertySource("classpath:sys.properties")
@Getter
@Setter
public class SysProperties {

    private String param1;
    private String param2;
}

controller类

@RestController
public class HelloController {

    @Autowired
    private SysProperties sysProperties;
	
    @RequestMapping("/test6")
    public Object test6(){
        return sysProperties.getParam1()+sysProperties.getParam2();
    }
	
}

SpringBoot2.0(Spring读取配置文件常用方法,打war包在Tomcat中启动),spring,tomcat,spring boot

二,SpringBoot部署war项目到tomcat9和启动原理

创建一个新项目
SpringBoot2.0(Spring读取配置文件常用方法,打war包在Tomcat中启动),spring,tomcat,spring boot
在添加个模块
SpringBoot2.0(Spring读取配置文件常用方法,打war包在Tomcat中启动),spring,tomcat,spring boot
然后在pom中添加依赖

    <packaging>war</packaging>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.1.RELEASE</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>


    <build>
        <finalName>passerby-war</finalName>
        <plugins>
            <plugin>
                <!-- 打包插件 -->
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

然后添加controller类和一个启动类
SpringBoot2.0(Spring读取配置文件常用方法,打war包在Tomcat中启动),spring,tomcat,spring boot
然后随便在Controller类里面加个方法

package com.demo.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class Controller {

    @RequestMapping("/demo01")
    public Object demo01(){
        return "hello,war";
    }
}

开始打包
SpringBoot2.0(Spring读取配置文件常用方法,打war包在Tomcat中启动),spring,tomcat,spring boot
在文件夹中找到这个位置
SpringBoot2.0(Spring读取配置文件常用方法,打war包在Tomcat中启动),spring,tomcat,spring boot
找到后把刚打的war包复制下来
SpringBoot2.0(Spring读取配置文件常用方法,打war包在Tomcat中启动),spring,tomcat,spring boot
然后在到你Tomcat的位置
SpringBoot2.0(Spring读取配置文件常用方法,打war包在Tomcat中启动),spring,tomcat,spring boot
把war包复制到里面
SpringBoot2.0(Spring读取配置文件常用方法,打war包在Tomcat中启动),spring,tomcat,spring boot
然后打开bin目录
SpringBoot2.0(Spring读取配置文件常用方法,打war包在Tomcat中启动),spring,tomcat,spring boot
在里面找到startup.bat这个
SpringBoot2.0(Spring读取配置文件常用方法,打war包在Tomcat中启动),spring,tomcat,spring boot
打开等他运行完
SpringBoot2.0(Spring读取配置文件常用方法,打war包在Tomcat中启动),spring,tomcat,spring boot
然后在打开你刚才把war包粘贴的那个文件夹
SpringBoot2.0(Spring读取配置文件常用方法,打war包在Tomcat中启动),spring,tomcat,spring boot
现在就好了,打开浏览器试试

SpringBoot2.0(Spring读取配置文件常用方法,打war包在Tomcat中启动),spring,tomcat,spring boot

有什么不理解的可以私信!!!文章来源地址https://www.toymoban.com/news/detail-700470.html

到了这里,关于SpringBoot2.0(Spring读取配置文件常用方法,打war包在Tomcat中启动)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • Spring Boot读取配置文件

    Spring Boot 是一种用于快速构建基于Spring的应用程序的框架,它提供了很多便利的功能和约定,使开发者可以快速搭建、配置和部署应用程序。在Spring Boot中,读取配置文件是一个非常常见的任务,本文将介绍如何在Spring Boot应用程序中读取配置文件,并使用读取到的配置信息。

    2024年02月07日
    浏览(34)
  • Springboot读取外部配置文件,项目部署时配置读取不到问题

    需求: 最近项目部署,但是每个地方都有个别地方配置的差异,我们每次打包后再进行修改极度不方便,这里将有关的配置都抽取出来,放在jar包外进行配置,这样以后更新时只需要将jar包更换然后重启即可,配置读取外部的固定配置文件。 SpringBoot 配置 springboot默认配置的

    2024年02月07日
    浏览(39)
  • Springboot读取配置文件

    springboot项目中不同配置文件的优先加载顺序 为:properties yml yaml自定义核心类配置 自定义配置文件的加载 一般系统会加载默认的application.properties或者application.yml,但如果使用自定义配置文件,可使用下面方式进行加载: @PropertySource(\\\"classpath:myApplication.properties\\\")加载自定义配置

    2024年01月17日
    浏览(24)
  • SpringBoot2.3集成Spring Security(二) JWT认证

    紧接上文,我们已经完成了 SpringBoot中集成Spring Security,并且用户名帐号和密码都是从数据库中获取。但是这种方式还是不能满足现在的开发需求。 使用JWT的好处: 无状态认证:JWT本身包含了认证信息和声明,服务器不需要在会话中保存任何状态。这样使得应用程序可以更加

    2024年02月11日
    浏览(45)
  • SpringBoot配置文件的注入和读取

    目录 1. 配置文件的作用 2. 两种配置文件的格式: 2.1 properties 基本语法: 2.1.1 写入 2.1.2 读取 执行原理 2.1.3 缺点分析 2.2 yml 基本语法: 2.2.1 写入(非对象) 2.2.3 配置对象 2.2.4 配置集合 多个配置文件         SpringBoot 是为了简化 Spring 的操作,提高 Spring 项目的开发效率,它

    2024年02月07日
    浏览(57)
  • SpringBoot读取配置文件中的内容

    配置文件application.yml: Environment 是 springboot 核心的环境配置接口,它提供了简单的方法来访问应用程序属性,包括系统属性、操作系统环境变量、命令行参数、和应用程序配置文件中定义的属性等等。 Springboot 程序启动加载流程里,会执行SpringApplication.run中的prepareEnvironmen

    2024年01月21日
    浏览(47)
  • springboot读取多文件配置(包括nacos)

    首先来简单了解一下这个类。 ResourceBundle类主要是用来 解决国际化和本地化问题 ,就我的理解,就是类似于前端界面的字体显示,国际化操作一般都要支持多国语言,那么这个ResourceBundle类就能够简单快速的解决这个问题。 同时,这个类只能支持读取properties属性文件,和

    2023年04月09日
    浏览(36)
  • 【微服务】spring读取配置文件多种方式深入详解

    目录 一、前言 二、java配置文件介绍 2.1 java配置文件产生原因 2.2 项目使用配置文件好处 2.3 springboot项目配置文件的必要性 2.4 微服务架构下配置文件使用场景 三、java读取配置文件常用方法 3.1 使用Properties类读取配置文件 3.1.1 使用getResourceAsStream读取 3.1.2 使用getClassLoader读取

    2024年04月22日
    浏览(46)
  • 【数据处理】Pandas读取CSV文件示例及常用方法(入门)

    查看读取前10行数据 2067 向前填充 指定列的插值填充 使用某数据填充指定列的空值 示例: 类似切片 array([‘SE’, ‘cv’, ‘NW’, ‘NE’], dtype=object) 类似数据库查询中的groupby查询 先添加新的一列按月将数据划分 聚合,对指定的列按月划分求平均值等 min 最小值 max 最大值 sum

    2024年02月06日
    浏览(52)
  • springboot的配置文件如何配置可以实现多个yml相互读取

    在Spring Boot中,可以通过多种方式来实现配置文件的相互读取和组合。如果你想要在一个Spring Boot应用中使用多个YAML( .yml )配置文件,并且希望这些配置文件可以相互读取或者互相覆盖某些配置,你可以采用以下几种方法: 1. 使用 spring.config.import 属性(Spring Boot 2.4及以上版

    2024年02月20日
    浏览(38)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包