天天看點

Springboot入門(二)項目配置

pom.xml檔案相關資訊

<!-- 項目的相關資訊-->
    <groupId>com.example</groupId>
    <artifactId>Test</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>Test</name>
    <description>Demo project for Spring Boot</description>
    <!--SpringBoot的parent配置  -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
        <!--Springboot web配置  -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--Springboot的單元測試  -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    <!--SpringBoot 建構項目時使用的插件配置  -->
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>      

TestApplication.java

//要啟動springboot項目必須添加該注解
@SpringBootApplication
public class TestApplication {

    public static void main(String[] args) {
    //springboot的啟動      

HelloWorld.java

1、在類上面條件聲明@RestController

2、增加方法@RequestMapping(“/hello”)

@RestController
public class HelloWorld {

    @RequestMapping(value="/hello",method=RequestMethod.GET)
    public String say() {
        return "Hello Springboot";
    }
}      

application.properties

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

server.context-path=/Test為浏覽網址添加了一個路徑

通路位址:​​http://127.0.0.1:8081/Test/hello​​

Springboot入門(二)項目配置

這裡推薦使用application.yml檔案,application.yml檔案寫法上更加簡單。

Springboot入門(二)項目配置
server:
  port: 8082
  context-path: /Test        

1、注意port:跟8082之間有空格,context-path也是一樣有空格。

2、使用yml檔案必須進行配置

<!-- 支援 @ConfigurationProperties 注解 -->
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-configuration-processor -->
<dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-configuration-processor</artifactId>  
</dependency>      

例如:

@RestController
public class HelloController
    @Value("${age}")
    private Integer age ;
    @RequestMapping(value="/helloctrl", method=RequestMethod.GET)
    public String say() {
        return age+"";
    }
}      

application.yml

server:
  port: 8081
age: 18      
Springboot入門(二)項目配置

Demo

HelloController.java

@RestController
public class HelloController
    @Value("${age}")
    private Integer age ;

    @Value("${name}")
    private String name;

    @Value("${content}")
    private String content;
    @RequestMapping(value="/helloctrl", method=RequestMethod.GET)
    public String say() {

        return      

application.yml

server:
  port: 8081
age: 18
name: 小芳
content: "name:${name} age:${age} "      
Springboot入門(二)項目配置

簡化書寫方式:

PersonProperties.java

//添加元件
@Component
//設定字首是person
@ConfigurationProperties(prefix="person")
public class PersonProperties
    private String name;
    private Integer age;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Integer getAge() {
        return age;
    }
    public void setAge(Integer age) {
        this.age = age;
    }

}      

application.yml

server:
  port: 8081
person:
  age: 19      

調用

@RestController
public class HelloController
    @Autowired
    private PersonProperties mPersonProperties;

    @RequestMapping(value="/helloctrl", method=RequestMethod.GET)
    public String say() {

        return "name:"+mPersonProperties.getName()+"age:"+mPersonProperties.getAge();
    }
}      

結果圖省略

開發環境與測試環境配置

Springboot入門(二)項目配置