SpringBoot初体验(yml的使用、注入的两种方法、多环境配置、原理Demo)

yml的使用

yml的格式有两种:
注意!!key和value之间的:有空格,必须要有空格!!
对象格式

student:
  name : "吴师傅"
  age : 18

普通格式

student.name : "吴师傅"
student.age : 18

yml使用的Demo(为了方便文件全部都放在Java文件夹下与application.java同级)
实体类

package com.bdqn.useyml;

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

//component:加入到Bean容器内
@Component
//prefix的值对应application.yml内的名字,这个类的属性对应其属性
@ConfigurationProperties(prefix = "student")
public class Students {
    //对应yml里面的属性
    private String name;
    private int age;
    //必须set get
    public String getName() {
        return name;
    }

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

    public int getAge() {
        return age;
    }

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

application.yml

student:
  name : "吴师傅"
  age : 18

Controller

package com.bdqn.useyml;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class Mycontroller {

    @Autowired
    private Students student;

    @RequestMapping("/")
    public String hello(){
        return student.getName()+student.getAge();
    }

}

修改起始读取文件(resources文件夹下的application)
在入口文件修改

package com.bdqn.useyml;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.PropertySource;

//追加读取配置文件(系统默认为resources文件夹下XXXapplication),若resources里面有application,也会读取
@PropertySource("classpath:/config/config.yml")
@SpringBootApplication
public class UseymlApplication {

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

config.yml

server.port : 8023

@value与@configurationpProperties的使用

虽然两个都是Springboot读值,但比较常用给的是@configurationpProperties,前面的例子已经使用过@configurationpProperties了,所以这里只介绍@value的使用demo

实体类

package com.bdqn.useyml;

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

//component:加入到Bean容器内
@Component
public class Students {
    //value的使用
    @Value("${student.name}")
    private String name;
    @Value("${student.age}")
    private int age;

    public String getName() {
        return name;
    }

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

    public int getAge() {
        return age;
    }

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

application.yml

student:
  name : "吴师傅"
  age : 18

Controller还是跟上面的一样

package com.bdqn.useyml;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class Mycontroller {

    @Autowired
    private Students student;

    @RequestMapping("/")
    public String hello(){
        return student.getName()+student.getAge();
    }

}

Springboot获取容器方法

MyUtil.java

package com.bdqn.useyml;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
//加入到bean容器内
@Component
//获取applicationContextAware
public class MyUtil implements ApplicationContextAware {

    private static ApplicationContext applicationContext;

    public static ApplicationContext getApplicationContext() {
        return applicationContext;
    }

    //类似以前Struts2获取session那样,此方法需要必须重写
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        if (MyUtil.applicationContext == null) {
            MyUtil.applicationContext = applicationContext;
            System.out.println("走构造了");
        }
    }

    public static Object getBean(String name){
        return getApplicationContext().getBean(name);
    }

}

实体类

package com.bdqn.useyml;

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

//component:加入到Bean容器内,同时也是这个bean的name和id
@Component("student")
@ConfigurationProperties(prefix = "student")
public class Students {
    private String name;
    private int age;

    public String getName() {
        return name;
    }

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

    public int getAge() {
        return age;
    }

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

在text文件夹下的文件测试获取bean

package com.bdqn.useyml;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
public class UseymlApplicationTests {

    @Test
    public void contextLoads() {
        //bean的名字与component的value对应,若component没有设置value则会默认类名,类名首字母小写
        Students students=(Students) MyUtil.getBean("student");
        System.out.println(students.getName());
    }

}

application.yml

student:
  name : "吴师傅"
  age : 18

多环境配置

properties版
三个文件 主:application.properties(负责选择是生产还是发开)   子-开发环境:application-dev.properties  子-生产环境:application-pro.properties
application.properties文件

spring.profiles.active=pro

application-dev.properties文件(开发环境,这里只是改端口而已,后面可以详细分功能)

server.port = 8081

application-pro.properties文件(生产环境)

server.port = 8082

yml配置

student:
  name : "吴师傅"
  age : 18
---
spring:
 profiles:
  active : dev
---
spring:
 profiles: pro
server:
 port: 8082
---
spring:
 profiles: dev
server:
 port: 8081

猜你喜欢

转载自blog.csdn.net/chijiajing/article/details/83586217