Java框架spring Boot學習筆記(二):Hello Spring Boot、以及項目屬性配置

新建一個新建一個SpringBootTest工程java

新建一個HelloController.java文件web

 1 package com.example.demo;
 2 
 3 import org.springframework.web.bind.annotation.RequestMapping;
 4 import org.springframework.web.bind.annotation.RequestMethod;
 5 import org.springframework.web.bind.annotation.RestController;
 6 
 7 @RestController
 8 public class HelloController {
 9 
10     @RequestMapping(value = "/hello" , method = RequestMethod.GET)
11     public String say() {
12         return "Hello Spring Boot!";
13     }
14 }

 

運行,打開瀏覽器訪問127.0.0.1:8080/hello,輸出Hello Spring Boot!spring

 

spring boot配置文件的使用瀏覽器

使用.properties格式的配置文件app

經過修改配置文件修改訪問端口和URL前綴,打開配置文件this

 

添加端口號和URL前綴spa

server.port=8081
server.context-path=/test

 

從新運行,可見訪問的URl已經發生了改變。3d

 

 

使用yml格式的配置文件code

新建一個application.yml的配置文件server

 

添加端口號和URL前綴

server:
  port: 8082
  context-path: /test

 

運行,打開瀏覽器

 

經過配置文件傳遞值

在application.yml添加一個值testValue

1 server:
2   port: 8082
3   context-path: /test
4 
5 
6 testValue: I'm test

 

使用註解的方式在配置文件中取值

 1 package com.example.demo;
 2 
 3 import org.springframework.beans.factory.annotation.Value;
 4 import org.springframework.web.bind.annotation.RequestMapping;
 5 import org.springframework.web.bind.annotation.RequestMethod;
 6 import org.springframework.web.bind.annotation.RestController;
 7 
 8 @RestController
 9 public class HelloController {
10 
11     @Value("${testValue}") 12     private String testValue; 13 
14     @RequestMapping(value = "/hello" , method = RequestMethod.GET)
15     public String say() {
16         return testValue;
17     }
18 }

 

 

運行,打開瀏覽器

能夠看到配置文件裏面的值經過註解的方式傳遞到了HelloController中的testValue。

 

配置屬性注入類

一個一個值注入比較麻煩,能夠使用注入類的方式,一次性將屬性注入。

修改配置文件application.yml

1 server:
2   port: 8081
3   context-path: /test
4 
5 testValue: I'm test
6 
7 people: 8  name: XiaoMing 9   age: 22

修改PeopleProperties.java

 1 package com.example.demo;
 2 
 3 import org.springframework.boot.context.properties.ConfigurationProperties;
 4 import org.springframework.stereotype.Component;
 5 
 6 //獲取注入還須要加一個@Component註解,@ConfigurationProperties(prefix = "people")獲取前綴是people的配置
 7 @Component
 8 @ConfigurationProperties(prefix = "people")
 9 public class PeopleProperties {
10 
11     private String name;
12     private Integer age;
13 
14 
15     public void setName(String name) {
16         this.name = name;
17     }
18     public void setAge(Integer age) {
19         this.age = age;
20     }
21 
22     public String getName() { return name; }
23     public Integer getAge() { return age; }
24 }

修改HelloController.java

 1 package com.example.demo;
 2 
 3 import org.springframework.beans.factory.annotation.Autowired;
 4 import org.springframework.web.bind.annotation.RequestMapping;
 5 import org.springframework.web.bind.annotation.RequestMethod;
 6 import org.springframework.web.bind.annotation.RestController;
 7 
 8 @RestController
 9 public class HelloController {
10 
11     //自動裝配註解
12     @Autowired
13     private PeopleProperties peopleProperties;
14 
15     @RequestMapping(value = "/hello" , method = RequestMethod.GET)
16     public String say() {
17         return peopleProperties.getName();
18     }
19 }

運行,打開瀏覽器訪問輸出。

 

不一樣的配置文件的切換

咱們在實際開發中,經常須要來回來回切換配置,好比常常須要在生產環境和開發環境之間來回切換。

建立三個配置文件application-dev.yml(開發環境配置文件)和application-prod.yml(生產環境配置文件)以及application.yml(默認配置文件)

 

 

application-dev.yml(開發環境配置文件)

1 server:
2   port: 8081
3   context-path: /test
4 
5 testValue: I'm test
6 
7 people:
8   name: XiaoMing
9   age: 22

 

application-prod.yml(生產環境配置文件)

1 server:
2   port: 8082
3   context-path: /test
4 
5 testValue: I'm test
6 
7 people:
8   name: Li Lei
9   age: 3

 

若是咱們使用開發環境的配置時

application.yml(默認配置文件),active設置爲dev

1 spring:
2   profiles:
3     active: dev

運行,訪問本地8081端口

 

若是咱們使用開發環境的配置時

application.yml(默認配置文件),active設置爲prod

1 spring:
2  profiles: 3 active: prod

運行,訪問本地8082端口

 

這樣只需修改默認配置文件application.yml就能夠實現不一樣配置的來回切換了

相關文章
相關標籤/搜索