Spring Boot Common Configuration

Source: Internet
Author: User
Tags java util

Overview

This article mainly writes about some common configurations of spring boot.

Spring Boot Basic Configuration

    Entry class:

Spring boot usually has an entry class named *application, and the entry class has a Main method, which is actually a standard Java application entry method. Use Springapplication.run (*application.class, args) in the main method to start the Spring Boot application project.

@SpringBootApplication:

@SpringBootApplication is the core annotation of spring boot, it is a combination of annotations, the source code is as follows:

1 //2 //Source code recreated from a. class file by IntelliJ idea3 //(Powered by Fernflower Decompiler)4 //5 6  Packageorg.springframework.boot.autoconfigure;7 8 Importjava.lang.annotation.Documented;9 ImportJava.lang.annotation.ElementType;Ten Importjava.lang.annotation.Inherited; One Importjava.lang.annotation.Retention; A ImportJava.lang.annotation.RetentionPolicy; - ImportJava.lang.annotation.Target; - ImportOrg.springframework.context.annotation.ComponentScan; the Importorg.springframework.context.annotation.Configuration; -  - @Target ({elementtype.type}) - @Retention (retentionpolicy.runtime) + @Documented - @Inherited + @Configuration A @EnableAutoConfiguration at @ComponentScan -  Public@Interfacespringbootapplication { -Class<?>[] Exclude ()default {}; -  -String[] Excludename ()default {}; -}
springbootapplication Source

@SpringBootApplication annotations are mainly composed of @configuration, @EnableAutoConfiguration, @ComponentScan If you do not use @springbootapplication annotations, you can use @configuration, @EnableAutoConfiguration, @ComponentScan directly on the Ingress class.

Where @EnableAutoConfiguration let spring boot automatically configure the current project based on the jar package dependencies in the classpath. For example, if you add a spring-boot-starter-web dependency that automatically adds Tomcat and spring MVC dependencies, Spring boot will automatically configure Tomcat and spring MVC.

Spring Boot automatically scans the siblings of the @springbootapplication class and the beans in the subordinate package. It is recommended that the location of the Ingress class be placed under the package name of the Groupid+arctifactid combination.

To turn off a specific automatic configuration:

As you can see from the source code of the @springbootapplication above, turning off a specific automatic configuration should use the exclude parameters of the @springbootapplication annotation, for example: @SpringBootApplication ( Exclude = {Datasourceautoconfiguration.class}).

    Banner:

Banner is a boot pattern that is displayed when spring boot starts, and we can modify or close it.

Modify Banner:

Create a new banner.txt under scr/main/resources and write the pattern you want.

Close banner:

1 //There are two ways to turn off banner, which is to modify the contents of main. 2         //1:3         NewSpringapplicationbuilder (ch623application.class). Showbanner (false). Run (args);4         //2:5Springapplication springapplication =NewSpringapplication (ch623application.class);6Springapplication.setshowbanner (false);7Springapplication.run (args);
Close Banner

Spring Boot configuration file:

Spring Boot uses a global configuration file, application.properties or APPLICATION.YML, placed under the/config of the Src/main/resources directory or classpath.

Spring boot supports not only the general properties configuration file, but also the Yaml language configuration file. YAML is a data-centric language that has object-oriented features when it comes to configuring data.

The role of the Spring boot global configuration file is to modify configuration values for some of the default configurations.

Example: Modify the default port number for Tomcat and modify the default access path "/" to "/helloboot" to add the following code to the configuration file.

server.port=9090

Server. Context_path=/helloboot

Starter Pom:

Spring boot provides us with the starter pom that simplifies most scenarios for enterprise-wide development, and with the starter pom required for the application scenario, the associated technical configuration will be eliminated and the automatically configured beans provided by spring boot are available.

You can go to the Spring boot website to find out which starter pom it offers.

Using XML configuration:

Spring boot advocates 0 configuration, i.e. no XML configuration, but there may be some special requirements in the actual project that you must use the XML configuration, and we can load the XML configuration with the @importresource provided by spring.

Example: @ImportResource ({"Classpath:som-context.xml", "Classpath:another-context.xml"})

Spring Boot External configuration

Spring boot allows the use of properties files, yaml files, or command line parameters as an external configuration, in addition to the above configuration.

   Command line parameter configuration:

Spring boot can be run based on the jar package, which can be run directly through the command: Java-jar Xx.jar.

You can modify the Tomcat port number by command: Java-jar Xx.jar--server.port=9090.

General Property Configuration:

In spring, you can inject values through the @value annotation by injecting values into the properties file and using @propertysource to indicate the location of the properties file.

In spring boot, we only need to define the attributes in Application.properties and use @value injection directly.

For example:

1 //write down the following code in Application.properties2     //book.author=gaof3     //book.name=spring Boot4     //then inject. 5@Value ("${book.author}")6     PrivateString Bookauthor;7@Value ("${book.name}")8     PrivateString BookName;

Type-safe configuration (based on properties):

Using @value to inject each configuration into the actual project will be particularly troublesome, as our configuration will usually be many, and if you use the method above, use @value to inject many times.

Spring boot also provides a type-safe configuration method that enables type-safe configuration by associating the properties property with a bean and its attributes through @configurationproperties.

For example:

1  PackageCom.wisely.ch6_2_3.config;2 3 Importorg.springframework.boot.context.properties.ConfigurationProperties;4 Importorg.springframework.stereotype.Component;5 6 /**7 * Write down the following code in Application.properties8 * author.name=gaof9 * author.age=32Ten * Then use the prefix of the @configurationproperties annotation to specify the prefix configured within the properties.  One  *  A * If you need to specify a different configuration file, you will need to add the attribute locations.  -  */ - @Component the@ConfigurationProperties (prefix = "Author") - //@ConfigurationProperties (prefix = "Author", locations = {"Classpath:config/author.properties"}) -  Public classauthorsettings { -     PrivateString name; +     PrivateLong age; -  +      PublicString GetName () { A         returnname; at     } -  -      Public voidsetName (String name) { -          This. Name =name; -     } -  in      PublicLong getage () { -         returnAge ; to     } +  -      Public voidsetage (Long age) { the          This. Age =Age ; *     } $}

Log configuration

Spring Boot supports Java Util Logging, log4j, Log4j2, and Logback as the logging framework, and spring boot is configured for the console output and file output currently using the log framework, regardless of the log framework used.

By default, Spring boot uses logback as the log frame.

    Configuration Log level:logging.level.org.springframework.web = DEBUG; Format: logging.level. Package name = level.

    Configuration log file:logging.file=d:/mylog/log.log.

Profile configuration

Profile is used by spring to provide support for different configurations for different environments, and global profiles are configured to use Application-{profile}.properties.

Specify the active profile by setting the Spring.profiles.active=prod in Application.properties.

For example:

        

        

Spring Boot Common Configuration

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.