欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

spring boot 文档学习笔记day02

程序员文章站 2022-07-08 16:17:55
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.t ......

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