二、springboot配置

一、启动类

在包根目录下添加启动类,必须包含main方法,再添加Spring Boot启动方法:

  SpringApplication.run(SampleController.class, args);

或者流式API

  new SpringApplicationBuilder().run(args);

二、核心注解

  启动类上面的注解是@SpringBootApplication,它也是Spring Boot的核心注解,主要组合包含了以下3个注解:

    @SpringBootConfiguration:组合了@Configuration注解,实现配置文件的功能。

    @EnableAutoConfiguration:打开自动配置的功能,也可以关闭某个自动配置的选项,如关闭数据源自动配置功能: @SpringBootApplication(exclude = { DataSourceAutoConfiguration.class })。

    @ComponentScan:Spring组件扫描。

三、配置文件

Spring Boot有两种类型的配置文件,applicationbootstrap文件

  application配置文件是应用级别的,是当前应用的配置文件

  bootstrap配置文件是系统级别的,用来加载外部配置,如配置中心的配置信息,也可以用来定义系统不会变化的属性。bootstatp文件的加载先于application文件。

Spring Boot会自动加载classpath目前下的这两个文件,文件格式为properties或者yml格式。

  *.properties属性文件,最常见的key=value

  *.yml文件,是yaml格式的文件,yaml是一种非常简洁的标记语言

    key: value的形式(:后面必须有空格),层次感更强,复用性更好

    *.yml加载的属性是有顺序的,但不支持@PropertySource注解来导入配置,一般推荐用yml文件,看下来更加形象。

配置文件读取:

  1.自定义属性:

my:
 name: zhangsan
 age: 12

  @Value(“${属性名}”)

    @Value("${my.name}")
    private String name;
    @Value("${my.age}")
    private int age;

  2.将配置文件的属性赋给实体类:

my:
 name: forezp
 age: 12
 number:  ${random.int}
 uuid : ${random.uuid}
 max: ${random.int(10)}
 value: ${random.value}
 greeting: hi,i'm  ${my.name}

${random}:生成各种不同类型的随机值

把配置文件的属性赋值给javabean 

@ConfigurationProperties(prefix = "my")
@Component
public class ConfigBean {

    private String name;
    private int age;
    private int number;
    private String uuid;
    private int max;
    private String value;
    private String greeting;

    省略了getter setter....

需要加注解@ConfigurationProperties,并加上它的prefix,同时bean中的属性和配置参数名保持一致

需要在应用类或者application类,加EnableConfigurationProperties注解

@RestController
@EnableConfigurationProperties({ConfigBean.class})
public class TestController {
    @Autowired
    ConfigBean configBean;

    @RequestMapping(value = "/aaa")
    public String print(){
        return configBean.getGreeting()+" >>>>"+configBean.getName()+" >>>>"+ configBean.getUuid()+" >>>>"+configBean.getMax();
    }

  3.通过命令行设置属性值

    java -jar xxx.jar --server.port=8888

    等价于:配置文件中server.port=8888

    在命令行运行时,连续的两个减号--就是对application.properties中的属性值进行赋值的标识

    通过SpringApplication.setAddCommandLineProperties(false)可以屏蔽命令行访问属性的设置

  4.自定义配置文件

    例如:test.properties

com.forezp.name=forezp
com.forezp.age=12

  赋值给javabean

@Configuration
@PropertySource(value = "classpath:test.properties")
@ConfigurationProperties(prefix = "com.forezp")
public class User {
    private String name;
    private int age;

   get/set...
}

  5.多环境配置

  我们在开发Spring Boot应用时,通常同一套程序会被应用和安装到几个不同的环境,比如:开发、测试、生产等。其中每个环境的数据库地址、服务器端口等等配置都会不同,如果在为不同环境打包时都要频繁修改配置文件的话,那必将是个非常繁琐且容易发生错误的事。

  在Spring Boot中多环境配置文件名需要满足application-{profile}.properties的格式,其中{profile}对应你的环境标识,比如: 

  • application-dev.properties:开发环境
  • application-test.properties:测试环境
  • application-prod.properties:生产环境

 

  在配置文件中通过spring.profiles.active属性来设置,其值对应{profile}值。

  1.不同环境通过命令行方式去激活不同环境的配置,如:java -jar xxx.jar --spring.profiles.active=test

  2.注解方式:

    可以在bean上使用注解@Profile("development")即调用application-development.properties|yml文件,也可以调用SpringApplication中的etAdditionalProfiles()方法。

@Profile("development")
@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication app = new SpringApplication(Application.class);
        //   app.setAdditionalProfiles("development");
        app.addListeners(new MyApplicationStartedEventListener());
        app.run(args);
    }
}

 

posted @ 2018-04-28 15:25  灬小乙  阅读(257)  评论(0编辑  收藏  举报