SpringBoot_3

1.Spring Initializer快速创建Spring Boot项目

src/main/java----保存java源代码
src/main/resources
application.properties-------Spring Boot应用的配置文件
[static]—需要自己手动创建【保存web应用程序所需的静态资源{hrml、css、js、img}】
[templates]–需要自己手动创建【保存模板页面】
Spring Boot默认jar包使用嵌入式的Tomcat,默认不支持JSP页面; 可以使用模板引擎freemarker、thymeleaf;
在不同环境下SpringBoot项目所使用的自动配置的默认数据值就需要随着环境的变化而被修改,我们在修改的时候不能修改源码,而且源码页无法修改,基于这个情况,SpringBoot项目对外提供了一个可以用来修改自动配置的默认数据值的文件,这个文件就是src/main/resources/application.properties文件。
application.properties文件SpringBoot的核心配置文件
作用:修改自动配置的默认数据值的文件
名称:application.properties / application.yml
application.properties /application.yml就是同一个配置文件,后缀名的不同,表示这个文件中内容的书写风格不同。
例如:配置数据库驱动名称

application.properties
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

application.yml
spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver

YAML(YAML Ain’t Markup Language)

例如
Xml:配置例子
<server> 
<port>8081</port> 
</server>
YAML:配置例子
server:
Port:8081

YAML语法格式:
键:(空格)值:表示一对键值对(空格必须有);
以空格的缩进来控制层级关系;只要是左对齐的一列数据,都是同一个层级的。
例如:

spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://127.0.0.1:3306/test
  jdbc:
    template:
      query-timeout: 1000
server:
  port: 9090

2.属性值得写法
属性值—键值对的键所对应的值
在上面的例子中url/port都是属性,实际都是xxxxxProperties类的成员变量。“jdbc:mysql://127.0.0.1:3306/test 、9090”就是属性值。
由于yml文件的属性,实际都是xxxxxProperties类的成员变量,成员变量都是有固定的数据类型,所以属性的值不能随便写,得符合成员变量对应的数据类型。
普通的值(数字,字符串,布尔)
1.1数字–数字值
1.2布尔–true/false
1.3字符串
默认不用加上单引号或者双引号;
如果有””[双引号],字符串中的转义字符会执行【\n—换行】
如果有’’[单引号],字符串中的转义字符不会执行,会作为一个字符直接使用【\n–\n】
2.对象
2.1对象名称对象中的属性换行缩进表示
例如:

student: 
  		stuid: 1001
  		stuname: zhangsan
  		stuage: 23
  		stuaddress: 西安
student----对象名称
stuid、stuname、stuage、stuaddress----对象中的属性
1001、zhangsan、23、西安----属性的数据值

2.2对象名称: {属性名称:属性值,属性名称:属性值}
3.集合
数组类型的集合(List、Set)
1”-[空格]数组集合中的元素值”
例如:

javas: 
  		- javase
  		- javaee
 		- javame
javas----数组名称
javase、javaee、javame---数组集合中的数据值

2.数组名称: [数据值1,数据值2]
例如:行内写法

names: [zhangsan,lisi,wangwu]
Map(键值对):
Map集合的名称: {key1:value1,key2:value2}
例如:maps: {name: zhangsan,age: 23,address: 西安}

3.将YAML文件中的数据值绑定到javabean上
通过@ConfigurationProperties将核心配置文件中的数据值与javabean类中的成员变量绑定
@ConfigurationProperties有一个属性prefix
prefix = “YAML文件中的对象名称”
具体步骤:
1.导入依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-configuration-processor</artifactId>
    <optional>true</optional>
</dependency>

2.创建javabean

package com.wangxing.springboot.bean;

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

import java.util.List;
import java.util.Map;
@Component
@ConfigurationProperties(prefix = "student")
public class UserBean {
    private Integer stuid;
    private String stuname;
    private boolean stusex;
    private List<String>likes;

    public Integer getStuid() {
        return stuid;
    }

    public void setStuid(Integer stuid) {
        this.stuid = stuid;
    }

    public String getStuname() {
        return stuname;
    }

    public void setStuname(String stuname) {
        this.stuname = stuname;
    }

    public boolean isStusex() {
        return stusex;
    }

    public void setStusex(boolean stusex) {
        this.stusex = stusex;
    }

    public List<String> getLikes() {
        return likes;
    }

    public void setLikes(List<String> likes) {
        this.likes = likes;
    }

    public Map<String, String> getMaps() {
        return maps;
    }

    public void setMaps(Map<String, String> maps) {
        this.maps = maps;
    }

    public MyAddress getMyAddress() {
        return myAddress;
    }

    public void setMyAddress(MyAddress myAddress) {
        this.myAddress = myAddress;
    }

    private Map<String,String> maps;
    private MyAddress myAddress;

}

3.Javabean上添加注解

@Component
@ConfigurationProperties(prefix = "student")

4.在resources的application.yml配置属性

student:
  stuid: 1001
  stuname: '张三'
  stusex: false
  likes:
    - game
    - book
    - kexi
  maps: {xiaoxue: '网星软件',zhongxue: '北京大学'}
  my-address: {address: 1,addressname: zhangsan, addressinfo: }

在resources的application.properties配置属性

student.stuid=1002
student.stuname=王小
student.stusex=true
student.likes=game,booke,play
student.maps.xiaoxue=长安县
student.maps.zhongxue=北京大学
student.my-address.address=1
student.my-address.addressname=西安
student.my-address.addressinfo=北京

由于properties配置文件在idea中默认utf-8可能会有中文乱码,所以需要设置修改转码;

测试用来的控制器类

package com.wangxing.springboot.controller;

import com.wangxing.springboot.bean.MyAddress;
import com.wangxing.springboot.bean.UserBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.sound.midi.SoundbankResource;
import javax.xml.ws.RequestWrapper;
import java.util.List;
import java.util.Map;

@Controller
public class StudentController {
    @Autowired
    private UserBean userBean;
    @RequestMapping(value = "/get")
    @ResponseBody
    public String get(){
        System.out.println(userBean);
        int stuid=userBean.getStuid();
        String stuname=userBean.getStuname();
        String stuinfo=stuid+""+stuname;
        List<String>likes= userBean.getLikes();
        for(String like:likes){
            System.out.println("like=="+like);
        }
        Map<String,String>maps=userBean.getMaps();
        for(Map.Entry<String,String>entry:maps.entrySet()){
            System.out.println(entry.getKey()+" "+entry.getValue());
        }
         MyAddress myAddress=userBean.getMyAddress();
        System.out.println(myAddress.getAddress()+" "+myAddress.getAddressname());
        return stuinfo;
    }
}

通过@Value将数据值与javabean类中的成员变量绑定
controller

package com.wangxing.springboot.controller;

import com.wangxing.springboot.bean.PersonBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller

public class PersonController {
    @Autowired
    private PersonBean personBean;
    @RequestMapping(value = "get")
    @ResponseBody
    public String get(){
     int perid=personBean.getPerid();
     String pername=personBean.getPername();
        System.out.println(perid+" "+pername);

     return "成功了";
    }

}

bean

package com.wangxing.springboot.bean;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class PersonBean {
    @Value("1001")
    private int perid;
    @Value("张三啊")
    private String pername;

    public int getPerid() {
        return perid;
    }

    public void setPerid(int perid) {
        this.perid = perid;
    }

    public String getPername() {
        return pername;
    }

    public void setPername(String pername) {
        this.pername = pername;
    }
}

@Value获取值和@ConfifigurationProperties获取值比较

配置文件yml还是properties他们都能获取到值;
如果说,我们只是在某个业务逻辑中需要获取一下配置文件中的某项值,使用@Value;
如果说,我们专门编写了一个javaBean来和配置文件进行映射,我们就直接使用@ConfifigurationProperties;

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值