Spring-boot参考(三) 配置

debug

开启debug开关可以查看更多的信息

java -jar myproject-0.0.1-SNAPSHOT.jar –debug
或者
在application.yml 中
debug: true

banner

banner.txt在classpath下增加这个文件,可以替换掉启动Spring的 图案。

Customizing the Banner

配置文件就是 环境变量。

在程序启动时 执行一些方法

实现 ApplicationRunner or CommandLineRunner 接口。

@Component
public class MyBean2 implements ApplicationRunner {

    @Override
    public void run(ApplicationArguments args) throws Exception {
        System.out.println(args);
    }
}

or

@Component
public class MyBean implements CommandLineRunner {

    public void run(String... args) {
        // Do something...
        System.out.println("hello");
        System.out.println(args);
    }

}

修改程序退出的 返回值

@SpringBootApplication
public class ExitCodeApplication {

    @Bean
    public ExitCodeGenerator exitCodeGenerator() {
        return () -> 42;
    }

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

}

Spring boot 配置参数的 加载顺序

  1. Devtools global settings properties on your home directory
    (~/.spring-boot-devtools.properties when devtools is active).
  2. @TestPropertySource annotations on your tests.

  3. @SpringBootTest#properties annotation attribute on your tests.

  4. Command line arguments.

  5. Properties from SPRING_APPLICATION_JSON
    (inline JSON embedded in an environment variable or system
  6. ServletConfig init parameters.
  7. ServletContext init parameters.
  8. JNDI attributes from java:comp/env.
  9. Java System properties (System.getProperties()).
  10. OS environment variables.
  11. A RandomValuePropertySource that only has properties in random.*.
  12. Profile-specific application properties outside of your packaged jar
    (application-{profile}.properties and YAML variants).

  13. Profile-specific application properties packaged inside your jar
    (application-{profile}.properties and YAML variants).

  14. Application properties outside of your packaged jar (application.properties and YAML variants).
  15. Application properties packaged inside your jar
    (application.properties and YAML variants).
  16. @PropertySource
    annotations on your @Configuration classes.
  17. Default properties
    (specified using SpringApplication.setDefaultProperties).

配置随机值

my.secret=${random.value}
my.number=${random.int}
my.bignumber=${random.long}
my.uuid=${random.uuid}
my.number.less.than.ten=${random.int(10)}
my.number.in.range=${random.int[1024,65536]}

配置 SpringApplication

public static void main(String[] args) {
    SpringApplication app = new SpringApplication(MySpringConfiguration.class);
    app.setBannerMode(Banner.Mode.OFF);
    app.run(args);
}

配置文件

默认会从 application.properties 这个文件中读取配置

这个文件放在

  • A /config subdirectory of the current directory.
  • The current directory
  • A classpath /config package
  • The classpath root
    这几个位置都可以。

类型安全的 配置 ConfigurationProperties

@Value(“${property}”) 可能会造成类型的不安全, 所以提供了
ConfigurationProperties

@ConfigurationProperties("foo")
public class FooProperties {

    private boolean enabled;

    private InetAddress remoteAddress;

    private final Security security = new Security();

    public boolean isEnabled() { ... }

    public void setEnabled(boolean enabled) { ... }

    public InetAddress getRemoteAddress() { ... }

    public void setRemoteAddress(InetAddress remoteAddress) { ... }

    public Security getSecurity() { ... }

    public static class Security {

        private String username;

        private String password;

        private List roles = new ArrayList<>(Collections.singleton("USER"));

        public String getUsername() { ... }

        public void setUsername(String username) { ... }

        public String getPassword() { ... }

        public void setPassword(String password) { ... }

        public List getRoles() { ... }

        public void setRoles(List roles) { ... }

    }
}

ConfigurationProperties 需要设置setter方法。

profile

spring.profiles.active=dev,hsqldb

你可能感兴趣的:(Java)