springboot-簡單測試-文件配置

springboot項目案例:https://blog.52itstyle.vip/archives/3344/

springboot詳解:https://blog.csdn.net/m0_37106742/article/details/64438892

springboot video:https://www.bilibili.com/video/BV1m7411d7Um?p=7

項目名稱:springboot-03-setupfiles

注:controller和model,service類得在主類(啓動類)的子包或同目錄下,要不然匹配不到

springboot第一個程序總結:

1.Springboot的父級依賴spring-boot-starter-parent配置後,當前項目就是Springboot項目;

2.如果不想使用某個默認的依賴版本,可以通過pom.xml文件的屬性配置覆蓋各個依賴項,比如覆蓋Spring版本

<properties>

  <spring.version>5.0.0.RELEASE</spring.version>

</properties>

3.@SpringBootApplication註解是Springboot項目的核心註解,主要作用是開啓Spring自動配置

4.main方法是一個標準的java程序的main方法,主要作用是作爲項目啓動運行入口

5.@Controller和@ResponseBody依然是我們之前的Spring mvc,因爲Springboot的裏面依然使用的是我們的Springmvc+spring+mybatis等框架

6.啓動時遇到這樣的錯誤

a. Exception in thread "main" java.lang.UnsupportedClassVersionError: org/springframework/boot/SpringApplication : Unsupported major.minor version 52.0

原因:52.0--java1.8

解決方案:1.更換jdk1.8

                   2.將spring-boot-starter-parent改爲1.4.1.RELEASE版本

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

b.當使用jdk是1.7時

    1.創建完項目後修改

<properties>
    <java.version>11</java.version>
</properties>

<properties>
    <java.version>1.7</java.version>
</properties>

  2.Settings--->Build,Execution,Deployment--->Compiler--->Java Compiler

將11改爲1.7

7.當application.properties和application.yml文件同時存在一樣的屬性時,以application.properties中的屬性爲主,如果想讀yml中的就將application.properties刪掉

8.多環境配置文件

主文件application.properties  :

#當前使用聯調環境配置文件

#spring.profiles.active=dev

#當前使用測試環境配置文件,和"-"後面的一致

spring.profiles.active=test

其他配置文件application-dev.properties  (聯調環境配置文件), application-test.properties (測試環境配置文件)

application-dev.properties   :

#配置內嵌的Tomcat端口號
server.port=9090

application-test.properties  :

#配置內嵌的Tomcat端口號 server.port=8080

9.讀取自定義配置方式

方法一:

@values註解

@values("${pay_address}")

private String pay_address;

方法二:

這個是屬於讀取自定義數據比較多時,將配置文件映射成一個對象

在model類前加@component和@ConfigurationProperties

eg:

package com.example.springboot.springboot03setupfiles.model;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Component
@ConfigurationProperties
public class UserModelYang {
    private String pay_address;
    private String customer;
    private String phoneNumber;

    public UserModelYang() {
    }

    public UserModelYang(String pay_address, String customer, String phoneNumber) {
        this.pay_address = pay_address;
        this.customer = customer;
        this.phoneNumber = phoneNumber;
    }

    public String getPay_address() {
        return pay_address;
    }

    public void setPay_address(String pay_address) {

        this.pay_address = pay_address;
    }

    public String getCustomer() {

        return customer;
    }

    public void setCustomer(String customer) {

        this.customer = customer;
    }

    public String getPhoneNumber() {
        return phoneNumber;
    }

    public void setPhoneNumber(String phoneNumber) {

        this.phoneNumber = phoneNumber;
    }

}

在使用這個類的類中使用@Autowired

package com.example.springboot.springboot03setupfiles.controller;

import com.example.springboot.springboot03setupfiles.model.UserModelYang;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/*
文件配置測試controller
 */


@Controller
public class HelloController {

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

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

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

    @Autowired
    private UserModelYang consumers;

    //使用文件映射對象進行自定義屬性值讀取
    @RequestMapping("/boot/config02")
    public  @ResponseBody String obtainConsumerDetailsWayTwo(){
        return "name:"+consumers.getCustomer()+"  phone:"+consumers.getPhoneNumber()+" address:"+consumers.getPay_address();
    }

    //讀取自定義屬性值
    @RequestMapping("/boot/config")
    public @ResponseBody  String obtainConsumerDetails(){
        return "customer:"+customer+"  phoneNumber:"+phoneNumber+"  pay_address:"+pay_address;
    }
    //springboot簡單測試
    @RequestMapping("/boot/hello")
    public @ResponseBody  String hello(){
        return "hello springboot!!!";
    }

}

application.properties:

#配置內嵌的Tomcat端口號
server.port=8080

#配置項目訪問的上下文根路徑
server.servlet.context-path=/springboot-03-setupfiles

#自定義配置
pay_address=https://blog.csdn.net/yanhhuan/[email protected]
customer=yanghuan01@
phoneNumber=183928789@@

pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.1.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example.springboot</groupId>
    <artifactId>springboot-03-setupfiles</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>springboot-03-setupfiles</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.7</java.version>
    </properties>

    <dependencies>
        <!--springboot 基本包 start-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <!--springboot 基本包 end-->

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>



</project>

注:持久層和業務層的包都必須是在主程序的包的子包下面,這樣model的bean才能被注入,像下面這樣

 

springboot簡單案例

idea創建springboot

訪問地址:http://localhost:8090//boot/hello

controller:

@Controller
public class HelloController {
    @RequestMapping("/boot/hello")
    public @ResponseBody  String hello(){
        return "hello springboot!!!";
    }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章