Springboot_day02

3.12-白天考研,晚上肝代码,充实。

1.获取.yml文件中的信息

2.通过@PropertySource获取信息

3.在容器中增加组件

1.获取.yml文件中的信息

application.yml

server:
  port: 8080
person:
  lastname: zhangsan
  age: 18
  boss: yes
  birth: 2020/2/20
  maps: {k1: v1,k2: v2}
  lists:
    - lisi
    - ss
  dog:
    name: dogy
    age: 3

test.java

package com;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/*
使用spring Initializer快速创建springboot项目
默认生成的springboot项目:
 主项目已经生成好了,我们只需要编写逻辑
 resourses文件夹中目录结构
    static:保存所有的静态资源
    templates:保存所有的模板页面(springboot默认jar包使用嵌入式的Tomcat,默认不支持jsp页面),可以使用模板引擎
    (freemarker thymleaf)
    application.properties:springboot应用的配置文件,可以修改一些默认设置

springboot使用一个全局的配置文件,可以对一些默认配置值进行修改
配置文件名是固定的application.properties application.yml
.yml以数据为中心比json、xml等更适合做配置文件

yml基本语法:
  1.k:(空格)v:表示一对键值对(空格必须有)
以空格的缩进来控制层级关系,只要是左对齐的一类数据,都是同一层级的
  2.属性和值也是大小写敏感
值的写法:
  1.字面量:普通的值(数字,字符串,布尔)
    字符串默认不用加上单引号或者双引号
      双引号:不会转义字符串里面的特殊字符,特殊字符会作为本身想表达的意思 "\n"代表换行
      单引号:会转义特殊字符,特殊字符只是一个普通的字符串数据
   2.对象、map(属性和值)(键值对):
     friends:
        lastname: zhangsan
        age: 20
     行内写法
     friends: {lastname: zhangsan,age: 20}
   3.数组(list,set)
     用-值表示数组中的一个元素
     pets:
      - cat
      - dog
      - pig
     行内写法:
     pets:[cat,dog,pig]
 */
@SpringBootApplication
public class DemoApplication {
    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}

person.java

package com.example.demo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
import javax.validation.constraints.Email;
import java.util.Date;
import java.util.List;
import java.util.Map;
/*
将配置文件中配置的每一个属性的值,映射到这个组件中
@ConfigurationProperties告诉springboot将本类中的所有属性和配置文件中
相关配置进行绑定
prefix ="person",配置文件中哪个下面的所有属性进行一一映射
只有这个组件是容器中的组件,才能使用容器提供的功能


  @ConfigurationProperties              @Value("${person.lastname}")
  功能:批量注入配置文件中的属性        一个个指定
  松散绑定:支持                                不支持
    -person.firstName 标准形式
    -person.first-name:大写用-
    -person.first_name:大写用_
    -PERSON_FIRST_NAME
  SpEL:不支持                                 支持
  JSP303数据校验:支持                     不支持
  复杂类型的封装:支持                       不支持
 */
@Component
@ConfigurationProperties(prefix ="person" )
public class person {
    @Value("${person.lastname}")
    //lastname必须是邮箱格式(@ConfigurationProperties可以校验)
    //@Email
    private String lastname;
    private Integer age;
    private Boolean boss;
    private Date birth;
    private Map<String,Object>maps;
    private List<Object>lists;
    private dog dog;

    public String getLastname() {
        return lastname;
    }

    public void setLastname(String lastname) {
        this.lastname = lastname;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public Boolean getBoss() {
        return boss;
    }

    public void setBoss(Boolean boss) {
        this.boss = boss;
    }

    public Date getBirth() {
        return birth;
    }

    public void setBirth(Date birth) {
        this.birth = birth;
    }

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

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

    public List<Object> getLists() {
        return lists;
    }

    public void setLists(List<Object> lists) {
        this.lists = lists;
    }

    public com.example.demo.dog getDog() {
        return dog;
    }

    public void setDog(com.example.demo.dog dog) {
        this.dog = dog;
    }

    @Override
    public String toString() {
        return "person{" +
                "lastname='" + lastname + '\'' +
                ", age=" + age +
                ", boss=" + boss +
                ", birth=" + birth +
                ", maps=" + maps +
                ", lists=" + lists +
                ", dog=" + dog +
                '}';
    }
}

dog.java

package com.example.demo;

public class dog {
    public String getName() {
        return name;
    }

    public Integer getAge() {
        return age;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "dog{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    private  String name;
    private Integer age;

}
View Code

2.通过@PropertySource(value = {"classpath:p.properties"})获取信息

这里注意:

之前写的application.yml 文件会被优先加载,而如果同时存在 p.properties 文件,并且存在相同的配置,

那么则会用 p.properties 文件中的配置覆盖之前的配置;

也就是说哪个文件被最后加载,哪个才具有最高级别,因为最后的,会覆盖前面所有的。

p.properties

person.age=21
person.boss=true
person.lastname=wxy

3.在容器中增加组件

测试类:

package com.example.demo;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.junit4.SpringRunner;
/*
可以在测试期间很方便的类似编码一样进行自动注入到容器中
 */
@RunWith(SpringRunner.class)
@SpringBootTest
public class DemoApplicationTests {
    @Autowired
    ApplicationContext ioc;
    @Test
   public void  testhelloservice(){
     boolean b= ioc.containsBean("helloservice");
     System.out.println(b);
    }
}
View Code

  1.用配置文件.xml的方式

bean.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="helloservic" class="com.service.helloservice">

</bean>
</beans>
View Code

DemoApplication.java   加入注解@ImportResource(locations = {"classpath:beans.xml"})

package com;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.ImportResource;

/*
使用spring Initializer快速创建springboot项目
默认生成的springboot项目:
 主项目已经生成好了,我们只需要编写逻辑
 resourses文件夹中目录结构
    static:保存所有的静态资源
    templates:保存所有的模板页面(springboot默认jar包使用嵌入式的Tomcat,默认不支持jsp页面),可以使用模板引擎
    (freemarker thymleaf)
    application.properties:springboot应用的配置文件,可以修改一些默认设置

springboot使用一个全局的配置文件,可以对一些默认配置值进行修改
配置文件名是固定的application.properties application.yml
.yml以数据为中心比json、xml等更适合做配置文件

yml基本语法:
  1.k:(空格)v:表示一对键值对(空格必须有)
以空格的缩进来控制层级关系,只要是左对齐的一类数据,都是同一层级的
  2.属性和值也是大小写敏感
值的写法:
  1.字面量:普通的值(数字,字符串,布尔)
    字符串默认不用加上单引号或者双引号
      双引号:不会转义字符串里面的特殊字符,特殊字符会作为本身想表达的意思 "\n"代表换行
      单引号:会转义特殊字符,特殊字符只是一个普通的字符串数据
   2.对象、map(属性和值)(键值对):
     friends:
        lastname: zhangsan
        age: 20
     行内写法
     friends: {lastname: zhangsan,age: 20}
   3.数组(list,set)
     用-值表示数组中的一个元素
     pets:
      - cat
      - dog
      - pig
     行内写法:
     pets:[cat,dog,pig]
 */
/*
@ImportResource:导入spring的配置文件,让配置文件里面的内容生效,
Springboot里面没有spring的配置文件,我们自己编写的配置文件,也不能识别,
想让spring的配置文件生效,@ImportResource标注在一个配置类上

springboot不推荐编写spring的配置文件(beans.xml)来添加组件,而推荐使用全注解的方式
通过配置类替代配置文件。

 */
@ImportResource(locations = {"classpath:beans.xml"})
@SpringBootApplication
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

}
View Code

helloservice.java

package com.service;

public class helloservice {

}
View Code

  2.用配置类的方式

去除起前面的beans.xml配置文件

config.java

package com.config;
import com.service.helloservice;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/*
@Configuration:指明当前类是一个配置类,来替代之前spring的配置文件(配置文件中 用<bean><bean/>标签添加组件)
 */
@Configuration
public class myconfig {
//将方法的返回值添加到容器中,容器中默认的ID就是方法名
    @Bean
    public helloservice helloservice(){
        return new helloservice();
    }
}

猜你喜欢

转载自www.cnblogs.com/zuiaimiusi/p/12484041.html