国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

SpringBoot2.0(Spring讀取配置文件常用方法,打war包在Tomcat中啟動(dòng))

這篇具有很好參考價(jià)值的文章主要介紹了SpringBoot2.0(Spring讀取配置文件常用方法,打war包在Tomcat中啟動(dòng))。希望對(duì)大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請(qǐng)大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問(wèn)。

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

1.1,使用@Value讀取

在springBoot聲明周期內(nèi),可以使用@Value注解從SpringBoot的默認(rèn)配置文件中讀取配置信息

例如在Controller中使用:

    // 在配置文件中讀取屬性名為web.images-path對(duì)應(yīng)的值
    @Value("${web.images-path}")
    private String path;

@Value可以放到屬性或方法上,能夠正常使用的前提是所在類(lèi),必須在SpringBoot的生命周期內(nèi)。
我們?cè)趺窗岩粋€(gè)類(lèi)放到Spring的生命周期中進(jìn)行管理?使用的是@Component注解
因?yàn)锧Controller和@Service本身就包含@Component。所以可以直接使用。

下面是單獨(dú)使用@Component的例子
創(chuàng)建一個(gè)config包,然后創(chuàng)建一個(gè)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中寫(xiě)

@RestController
public class HelloController {

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

SpringBoot2.0(Spring讀取配置文件常用方法,打war包在Tomcat中啟動(dòng)),spring,tomcat,spring boot
使用ing類(lèi)型寫(xiě)

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中啟動(dòng)),spring,tomcat,spring boot

1.2,使用@ConfigurationProperties

BootProperties類(lèi)

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") // 配置屬性類(lèi),并定制前綴
@Component  // 因?yàn)锧ConfigurationProperties不能把此類(lèi)放到boot容器中,所以要配合@Componpent使用
public class BootProperties {

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

    // 不使用@Value注解,需要保證:前綴+屬性名=全路徑。還需要此屬性有對(duì)應(yīng)的setter方法
    // @Value("${server.port}")
    public int port;


    // 使用@Value注解則需要寫(xiě)全路徑
}

controller類(lèi)

@RestController
public class HelloController {

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

SpringBoot2.0(Spring讀取配置文件常用方法,打war包在Tomcat中啟動(dòng)),spring,tomcat,spring boot

1.3,使用Environment

Environment是SpringCore中的一個(gè)用于讀取配置文件的類(lèi),將此類(lèi)使用@Autowired注入到類(lèi)中就可以使用它的getProperty方法來(lái)獲取某個(gè)配置項(xiàng)的值

@RestController
public class HelloController {

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

SpringBoot2.0(Spring讀取配置文件常用方法,打war包在Tomcat中啟動(dòng)),spring,tomcat,spring boot

1.4,自定義配置文件讀取

使用之前的知識(shí)來(lái)理解下面的代碼。
主要添加新的注解@PropertySource

創(chuàng)建一個(gè)config包,然后創(chuàng)建一個(gè)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類(lèi)

@RestController
public class HelloController {

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

SpringBoot2.0(Spring讀取配置文件常用方法,打war包在Tomcat中啟動(dòng)),spring,tomcat,spring boot

二,SpringBoot部署war項(xiàng)目到tomcat9和啟動(dòng)原理

創(chuàng)建一個(gè)新項(xiàng)目
SpringBoot2.0(Spring讀取配置文件常用方法,打war包在Tomcat中啟動(dòng)),spring,tomcat,spring boot
在添加個(gè)模塊
SpringBoot2.0(Spring讀取配置文件常用方法,打war包在Tomcat中啟動(dòng)),spring,tomcat,spring boot
然后在pom中添加依賴(lài)

    <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類(lèi)和一個(gè)啟動(dòng)類(lèi)
SpringBoot2.0(Spring讀取配置文件常用方法,打war包在Tomcat中啟動(dòng)),spring,tomcat,spring boot
然后隨便在Controller類(lèi)里面加個(gè)方法

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";
    }
}

開(kāi)始打包
SpringBoot2.0(Spring讀取配置文件常用方法,打war包在Tomcat中啟動(dòng)),spring,tomcat,spring boot
在文件夾中找到這個(gè)位置
SpringBoot2.0(Spring讀取配置文件常用方法,打war包在Tomcat中啟動(dòng)),spring,tomcat,spring boot
找到后把剛打的war包復(fù)制下來(lái)
SpringBoot2.0(Spring讀取配置文件常用方法,打war包在Tomcat中啟動(dòng)),spring,tomcat,spring boot
然后在到你Tomcat的位置
SpringBoot2.0(Spring讀取配置文件常用方法,打war包在Tomcat中啟動(dòng)),spring,tomcat,spring boot
把war包復(fù)制到里面
SpringBoot2.0(Spring讀取配置文件常用方法,打war包在Tomcat中啟動(dòng)),spring,tomcat,spring boot
然后打開(kāi)bin目錄
SpringBoot2.0(Spring讀取配置文件常用方法,打war包在Tomcat中啟動(dòng)),spring,tomcat,spring boot
在里面找到startup.bat這個(gè)
SpringBoot2.0(Spring讀取配置文件常用方法,打war包在Tomcat中啟動(dòng)),spring,tomcat,spring boot
打開(kāi)等他運(yùn)行完
SpringBoot2.0(Spring讀取配置文件常用方法,打war包在Tomcat中啟動(dòng)),spring,tomcat,spring boot
然后在打開(kāi)你剛才把war包粘貼的那個(gè)文件夾
SpringBoot2.0(Spring讀取配置文件常用方法,打war包在Tomcat中啟動(dòng)),spring,tomcat,spring boot
現(xiàn)在就好了,打開(kāi)瀏覽器試試

SpringBoot2.0(Spring讀取配置文件常用方法,打war包在Tomcat中啟動(dòng)),spring,tomcat,spring boot

有什么不理解的可以私信!??!文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-700470.html

到了這里,關(guān)于SpringBoot2.0(Spring讀取配置文件常用方法,打war包在Tomcat中啟動(dòng))的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來(lái)自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場(chǎng)。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請(qǐng)注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請(qǐng)點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • Spring Boot讀取配置文件

    Spring Boot 是一種用于快速構(gòu)建基于Spring的應(yīng)用程序的框架,它提供了很多便利的功能和約定,使開(kāi)發(fā)者可以快速搭建、配置和部署應(yīng)用程序。在Spring Boot中,讀取配置文件是一個(gè)非常常見(jiàn)的任務(wù),本文將介紹如何在Spring Boot應(yīng)用程序中讀取配置文件,并使用讀取到的配置信息。

    2024年02月07日
    瀏覽(20)
  • Springboot讀取外部配置文件,項(xiàng)目部署時(shí)配置讀取不到問(wèn)題

    需求: 最近項(xiàng)目部署,但是每個(gè)地方都有個(gè)別地方配置的差異,我們每次打包后再進(jìn)行修改極度不方便,這里將有關(guān)的配置都抽取出來(lái),放在jar包外進(jìn)行配置,這樣以后更新時(shí)只需要將jar包更換然后重啟即可,配置讀取外部的固定配置文件。 SpringBoot 配置 springboot默認(rèn)配置的

    2024年02月07日
    瀏覽(24)
  • Springboot讀取配置文件

    Springboot讀取配置文件

    springboot項(xiàng)目中不同配置文件的優(yōu)先加載順序 為:properties yml yaml自定義核心類(lèi)配置 自定義配置文件的加載 一般系統(tǒng)會(huì)加載默認(rèn)的application.properties或者application.yml,但如果使用自定義配置文件,可使用下面方式進(jìn)行加載: @PropertySource(\\\"classpath:myApplication.properties\\\")加載自定義配置

    2024年01月17日
    瀏覽(19)
  • SpringBoot2.3集成Spring Security(二) JWT認(rèn)證

    SpringBoot2.3集成Spring Security(二) JWT認(rèn)證

    緊接上文,我們已經(jīng)完成了 SpringBoot中集成Spring Security,并且用戶名帳號(hào)和密碼都是從數(shù)據(jù)庫(kù)中獲取。但是這種方式還是不能滿足現(xiàn)在的開(kāi)發(fā)需求。 使用JWT的好處: 無(wú)狀態(tài)認(rèn)證:JWT本身包含了認(rèn)證信息和聲明,服務(wù)器不需要在會(huì)話中保存任何狀態(tài)。這樣使得應(yīng)用程序可以更加

    2024年02月11日
    瀏覽(21)
  • SpringBoot配置文件的注入和讀取

    SpringBoot配置文件的注入和讀取

    目錄 1. 配置文件的作用 2. 兩種配置文件的格式: 2.1 properties 基本語(yǔ)法: 2.1.1 寫(xiě)入 2.1.2 讀取 執(zhí)行原理 2.1.3 缺點(diǎn)分析 2.2 yml 基本語(yǔ)法: 2.2.1 寫(xiě)入(非對(duì)象) 2.2.3 配置對(duì)象 2.2.4 配置集合 多個(gè)配置文件 ? ? ? ? SpringBoot 是為了簡(jiǎn)化 Spring 的操作,提高 Spring 項(xiàng)目的開(kāi)發(fā)效率,它

    2024年02月07日
    瀏覽(21)
  • SpringBoot讀取配置文件中的內(nèi)容

    SpringBoot讀取配置文件中的內(nèi)容

    配置文件application.yml: Environment 是 springboot 核心的環(huán)境配置接口,它提供了簡(jiǎn)單的方法來(lái)訪問(wèn)應(yīng)用程序?qū)傩?,包括系統(tǒng)屬性、操作系統(tǒng)環(huán)境變量、命令行參數(shù)、和應(yīng)用程序配置文件中定義的屬性等等。 Springboot 程序啟動(dòng)加載流程里,會(huì)執(zhí)行SpringApplication.run中的prepareEnvironmen

    2024年01月21日
    瀏覽(32)
  • springboot讀取多文件配置(包括nacos)

    首先來(lái)簡(jiǎn)單了解一下這個(gè)類(lèi)。 ResourceBundle類(lèi)主要是用來(lái) 解決國(guó)際化和本地化問(wèn)題 ,就我的理解,就是類(lèi)似于前端界面的字體顯示,國(guó)際化操作一般都要支持多國(guó)語(yǔ)言,那么這個(gè)ResourceBundle類(lèi)就能夠簡(jiǎn)單快速的解決這個(gè)問(wèn)題。 同時(shí),這個(gè)類(lèi)只能支持讀取properties屬性文件,和

    2023年04月09日
    瀏覽(24)
  • 【微服務(wù)】spring讀取配置文件多種方式深入詳解

    【微服務(wù)】spring讀取配置文件多種方式深入詳解

    目錄 一、前言 二、java配置文件介紹 2.1 java配置文件產(chǎn)生原因 2.2 項(xiàng)目使用配置文件好處 2.3 springboot項(xiàng)目配置文件的必要性 2.4 微服務(wù)架構(gòu)下配置文件使用場(chǎng)景 三、java讀取配置文件常用方法 3.1 使用Properties類(lèi)讀取配置文件 3.1.1 使用getResourceAsStream讀取 3.1.2 使用getClassLoader讀取

    2024年04月22日
    瀏覽(28)
  • 【數(shù)據(jù)處理】Pandas讀取CSV文件示例及常用方法(入門(mén))

    【數(shù)據(jù)處理】Pandas讀取CSV文件示例及常用方法(入門(mén))

    查看讀取前10行數(shù)據(jù) 2067 向前填充 指定列的插值填充 使用某數(shù)據(jù)填充指定列的空值 示例: 類(lèi)似切片 array([‘SE’, ‘cv’, ‘NW’, ‘NE’], dtype=object) 類(lèi)似數(shù)據(jù)庫(kù)查詢(xún)中的groupby查詢(xún) 先添加新的一列按月將數(shù)據(jù)劃分 聚合,對(duì)指定的列按月劃分求平均值等 min 最小值 max 最大值 sum

    2024年02月06日
    瀏覽(1673)
  • springboot的配置文件如何配置可以實(shí)現(xiàn)多個(gè)yml相互讀取

    在Spring Boot中,可以通過(guò)多種方式來(lái)實(shí)現(xiàn)配置文件的相互讀取和組合。如果你想要在一個(gè)Spring Boot應(yīng)用中使用多個(gè)YAML( .yml )配置文件,并且希望這些配置文件可以相互讀取或者互相覆蓋某些配置,你可以采用以下幾種方法: 1. 使用 spring.config.import 屬性(Spring Boot 2.4及以上版

    2024年02月20日
    瀏覽(24)

覺(jué)得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請(qǐng)作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包