spring boot 文档学习笔记day02

2018-06-18 02:47:18来源:未知 阅读 ()

新老客户大回馈,云服务器低至5折

1. 启动报错,查看debug

$ java -jar myproject-0.0.1-SNAPSHOT.jar –debug

 

 2.自定义banner

The banner that is printed on start up can be changed by adding a banner.txt file to your classpath or by setting the spring.banner.location property to the location of such a file. If the file has an encoding other than UTF-8, you can set spring.banner.charset. In addition to a text file, you can also add a banner.gif, banner.jpg, or banner.png image file to your classpath or set the spring.banner.image.location property. Images are converted into an ASCII art representation and printed above any text banner.

 

3. 自定义SpringApplication

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

 4. 获取运行参数

If you need to access the application arguments that were passed to SpringApplication.run(…?), you can inject a org.springframework.boot.ApplicationArguments bean. The ApplicationArguments interface provides access to both the raw String[] arguments as well as parsed option and non-option arguments, as shown in the following example:

import org.springframework.boot.*
import org.springframework.beans.factory.annotation.*
import org.springframework.stereotype.*
 
@Component
public class MyBean {
 
        @Autowired
        public MyBean(ApplicationArguments args) {
               boolean debug = args.containsOption("debug");
               List<String> files = args.getNonOptionArgs();
               // if run with "--debug logfile.txt" debug=true, files=["logfile.txt"]
        }
 
}

5.application.properties的读取顺序

SpringApplication loads properties from application.properties files in the following locations and adds them to the Spring Environment:

  1. A /config subdirectory of the current directory
  2. The current directory
  3. A classpath /config package
  4. The classpath root

The list is ordered by precedence (properties defined in locations higher in the list override those defined in lower locations).

 

6. 指定配置文件名和位置

The following example shows how to specify a different file name:

$ java -jar myproject.jar --spring.config.name=myproject

The following example shows how to specify two locations:

$ java -jar myproject.jar --spring.config.location=classpath:/default.properties,classpath:/override.properties

 

7.@ConfigurationPropertie宽松的多种绑定,properties中推荐小写+中划线

  1. @ConfigurationProperties(prefix="acme.my-project.person")

public class OwnerProperties {

 

        private String firstName;

 

        public String getFirstName() {

               return this.firstName;

        }

 

        public void setFirstName(String firstName) {

               this.firstName = firstName;

        }

 

}

In the preceding example, the following properties names can all be used:

acme.my-project.person.firstName

Standard camel case syntax.

acme.my-project.person.first-name

Kebab case, which is recommended for use in .properties and .yml files.

acme.my-project.person.first_name

Underscore notation, which is an alternative format for use in .properties and .yml files.

ACME_MYPROJECT_PERSON_FIRSTNAME

Upper case format, which is recommended when using system environment variables.

The prefix value for the annotation must be in kebab case (lowercase and separated by -, such as acme.my-project.person).

 

8. profiles

Spring Profiles provide a way to segregate parts of your application configuration and make it be available only in certain environments. Any @Component or @Configuration can be marked with @Profile to limit when it is loaded

You can use a spring.profiles.active Environment property to specify which profiles are active:spring.profiles.active=dev,hsqldb

 

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:最近用spring4.x整合Jackson------&gt;java.lang.ClassNotFoundE

下一篇:使用 Collections 实现排序 sort方法 对List 实体类实现Compa