Spring boot document Study Notes day02 and Study Notes day02

Source: Internet
Author: User

Spring boot document Study Notes day02 and Study Notes day02

1.Start to report an error. View debug

$ Java-jar myproject-0.0.1-SNAPSHOT.jar-debug

 

2.Custom banner

The banner that is printed on start up can be changed by addingbanner.txtFile to your classpath or by settingspring.banner.locationProperty to the location of such a file. If the file has an encoding other than UTF-8, you can setspring.banner.charset. In addition to a text file, you can also addbanner.gif,banner.jpg, Orbanner.pngImage file to your classpath or setspring.banner.image.locationProperty. Images are converted into an ASCII art representation and printed above any text banner.

 

3.Custom SpringApplication

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

 4.Get running parameters

If you need to access the application arguments that were passedSpringApplication.run(…), You can injectorg.springframework.boot.ApplicationArgumentsBean.ApplicationArgumentsInterface provides access to both the rawString[]Arguments as well as parsedoptionAndnon-optionArguments, 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.Read order of 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.Specify the configuration file name and location

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 loose multiple bindings. It is recommended to use lowercase + hyphen in properties.

Public class OwnerProperties {

 

PrivateString firstName;

 

PublicString getFirstName (){

Returnthis. firstName;

}

 

Public voidsetFirstName (String firstName ){

This. firstName = firstName;

}

 

}

In thepreceding 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.

TheprefixValuefor the annotationMustBe in kebab case (lowercase and separated-, Suchacme.my-project.person).

 

8.Profiles

SpringProfiles provide a way to segregate parts of your application configuration andmake it be available only in certain environments. Any@ComponentOr@ConfigurationCan be marked@ProfileTo 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

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.