Spring Boot Reference Tutorial (v) The configuration class usage used by the Spring boot configuration

Source: Internet
Author: User

4.2. Springboot configuration uses the configuration class to use

Most of the automatic configuration of spring boot can meet the application requirements, but if you want to control the application precisely, or if you want to override the automatic configuration, using the configuration class is another good choice, emphasizing thatSpring boot uses configuration files and configuration classes primarily for configuration.

1. Startup class

When you create a Spring boot project, you automatically generate two classes based on your project's packaging choices, one for local startup and the boot as a jar package, and one for the two most important configuration classes launched as a war package. The author is called the Startup class.

The code is as follows:

(1)jar Startup class:

About Springapplication We can do some customization, as shown in the following simple demo:

Springapplication app = Newspringapplication (Myspringconfiguration.class);

App.setbannermode (Banner.Mode.OFF);

App.run (args);

The code above is to close the banner display of Spring boot in code mode .

New Springapplicationbuilder ()

. Sources (Parent.class)

. Child (Application.class)

. Bannermode (Banner.Mode.OFF)

. Run (args);

The above code can be used to associate parent-child contexts with other application and no longer test.

2war Startup class

In fact the way the basic similarity code is as follows:

@SpringBootApplication ()

Public Classhelloworldapplication extends Springbootservletinitializer {

@Override

Protected springapplicationbuilderconfigure (Springapplicationbuilder application) {

Returnapplication.sources (Helloworldapplication.class);

}

The main method is to override the Configure () method after inheriting Springbootservletinitializer, and the contents of the method are consistent with the jar launcher .

Specifically, it can be researched on its own.

Examples are as follows:

2. Configure the home page

There are several ways to configure your app's home page in your app, one to configure in Web. XML, to configure the home page with the welcome element, and how to configure it in Spring boot, as shown below:

Write code:

Write a simple test home page:

To start the test:

3. Configure Beans

There will definitely be scenes in development that require custom beans to be added to the IOC container, you can use @Component annotations, and when annotations can be used, add @Configuration to the class, add annotations to the method Bean, test the code as follows: Create an entity Bean

Create a configuration class, add annotations to the class @Configuration, create a configuration method to add annotations to the method @Bean:

Test code:

To start the access test:

4. Conditional Configuration Bean

Development encounters a scenario where some beans are initialized by relying on other beans or depending on the condition to decide whether or not to initialize. Spring Boot provides a variety of conditional annotations to meet the requirements, in fact a large part of the automatic assembly of the spring boot application is implemented based on conditional annotations.

Specific annotations and descriptions are as follows:

1. @ConditionalOnBean:There is a corresponding instance in the Spring container. You can find it in the container by its type, class name, annotations, nickname (you can configure to find from the current container, or look in the parent container, or both ) .

2. @ConditionalOnClass: The class loader has a corresponding class. Can be specified by the class designation (Value property ) or by the full name of the class (Name property ). In the case of multiple classes or multiple class names, relationships are "and " relationships, meaning that these classes or class names must exist in the ClassLoader as well.

3. @ConditionalOnExpression: Determine If the Spel expression is true.

4.      @ConditionalOnJava: Specifies whether the java version meets the requirements. Internal 2 properties value and range. value represents an enumeration of java version, range represents an old or new equal to the specified java version (the default is the new equals jdk version of the class to the class loader query, such as if it is jdk9, the class loader needs to exist Java.security.cert.URICertStoreParameters, if jdk8, there is a need for jdk7, there is a need for java.nio.file.files in the ClassLoader; Span lang= "en-us" >jdk6, java.util.serviceloader is required in the ClassLoader.

5. @ConditionalOnMissingBean:If the corresponding instance is missing from the spring container. You can find it in the container by its type, class name, annotations, nickname (you can configure to find from the current container, or look in the parent container, or both ) . There are 2 more attributes ignored (class name ) and Ignoredtype (class name ), which are ignored during the matching process .

6. @ConditionalOnMissingClass: As with Conditionalonclass's processing logic, there is no corresponding class in the ClassLoader except for the condition.

7. @ConditionalOnNotWebApplication: Whether the application is a non- Web program, does not provide a property, just an identity. From the existence of a class that determines whether a Web application is unique, whether the environment is a servlet environment, whether the container is a web container, etc.

8. @ConditionalOnProperty: Whether there is a presence in the application environment. Provides prefix,name,havingvalue, and matchifmissing properties. prefix represents the prefix of the property name, nameis the property name,Havingvalue is the specific property value,matchifmissing is a boolean value, and if the property does not exist, the If the matchifmissing is true, it will continue to be verified, otherwise the property does not exist directly equivalent to the match is unsuccessful.

9. @ConditionalOnResource: The specified resource file exists. There is only one property resources, which is a string array. The corresponding resource file is queried for existence from the ClassLoader.

@ConditionalOnSingleCandidate: TheSpring container exists and only one corresponding instance exists. There are only 3 properties of value,type,search. This is the same as the 3 attribute values in Conditionalonbean.

@ConditionalOnWebApplication: Whether the application is a Web program, does not provide a property, is just an identity. From the existence of a class that determines whether a Web application is unique, whether the environment is a servlet environment, whether the container is a web container, etc.

The following is a test of only two annotations:

Test Note 1: @ConditionalOnBean/@ConditionalOnClass

The following tests apply to both of these annotations:

Create a two entity class as follows:

To add a configuration to the configuration file:

Configbean.value1=value1
Configbean.value2=value2
Configbean.value3=value3
Configbean.value4=value4

With beans: First Test CONFFIGBEAN1 not configured to initialize

Test class:

Start Access, test results:

Modify The code to add the configuration code to the configuration ConfigBean1:

After restarting the test, access the results:

Test Note 2: @ConditionalOnExpression

To modify the code:


To add a configuration to the Application.properties :

Configbean.configbean1.configornot=true

Configbean.configbean2.configornot=false

Note: The configuration and annotations indicate that the configuration ConfigBean1 is not configured ConfigBean2

To start the test:

The test shows that the Configbean1 initialization was successful and configbean2 was not initialized .

The conditional configuration bean has more complex , more precise configuration usage , which is demonstrated in the later chapters ,< integration mybatis+ third-party data sources >.

5. Configure static resources

About the configuration of springboot static resources The method for configuring using a configuration file is described in 4.1.1 above, but using a configuration file for configuration has the disadvantage of overriding the default static resource configuration, which you can configure using the following method if you want to keep the default configuration.

The code below is no longer tested:

< instance code >

Github:https://github.com/chunyuding/springboot-demo

Https://github.com/chunyuding/SpringBoot-Demo.git

Code Cloud: Https://gitee.com/dingchunyu/SpringBoot-Demo

Https://gitee.com/dingchunyu/SpringBoot-Demo.git

< recommended books >

Baidu Cloud: Http://pan.baidu.com/s/1qYA0Nxi

Spring Boot Reference Tutorial (v) The configuration class usage used by the Spring boot 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.