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/category_9902641000000@.html
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!!!";
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值