一,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;
}
}
使用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;
}
}
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;
}
}
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");
}
}
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();
}
}
二,SpringBoot部署war項(xiàng)目到tomcat9和啟動(dòng)原理
創(chuàng)建一個(gè)新項(xiàng)目
在添加個(gè)模塊
然后在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)
然后隨便在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)始打包
在文件夾中找到這個(gè)位置
找到后把剛打的war包復(fù)制下來(lái)
然后在到你Tomcat的位置
把war包復(fù)制到里面
然后打開(kāi)bin目錄
在里面找到startup.bat這個(gè)
打開(kāi)等他運(yùn)行完
然后在打開(kāi)你剛才把war包粘貼的那個(gè)文件夾
現(xiàn)在就好了,打開(kāi)瀏覽器試試
文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-700470.html
有什么不理解的可以私信!??!文章來(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)!