Alibaba Micro Service expert handwriting Spring Boot implements a simple auto-configuration module

Source: Internet
Author: User

To better understand how Spring Boot is automatically configured and how it works, we implement a simple automatic configuration module ourselves.

Suppose, now the project needs a function, need to automatically record the project publisher's relevant information, how can we use Spring Boot automatic configuration, better implementation of the function?

The beginning of actual combat –maven construction

To create a MAVEN project, let me manually configure the next POM file.

Configuration of Parameters-attribute parameter class

First, we define a custom prefix, called Custom. As mentioned before, the configuration parameters here can be set directly through the application.properties. Well, we create a field for the author, set the default value to Lianggzone.

So, smart you, should think of, we in the application.properties configuration, it is necessary to configure this.

#custom

Custom.author = James

It's really simple.-Simple service class

You are not mistaken, it is really too simple, no tall on the complex business. Its main purpose is to assign values.

Automatic configuration of the core-automatic configuration class

@ConditionalOnClass, the corresponding class in the parameter will not parse the corresponding configuration class until it exists in the Classpath directory. Therefore, we need to configure Authorserver.

@EnableConfigurationProperties that is used to load configuration parameters, so it should be the attribute parameter class

Authorproperties.

The function of the Authorresolver method, that is, the authorproperties parameter is assigned to Authorserver.

Spring.factories Don't miss it.

When we need to implement custom automatic assembly, we need to customize the spring.factories parameter. So, we need to configure the information in the src/main/resources/meta-inf/spring.factories, it is worth noting that this file is to be created by itself.

Feature packaging and configuration dependencies

Well, we've implemented a simple auto-configuration feature. Well, we need to put this project into a jar package deployed on our local or on-premises. Then, you can use it.

In another project, we configured Maven dependencies.

Test, test

Run up and we look at what publisher information is printed?

We configure a message in the application.properties.

#custom

Custom.author = James

Run up and we look at what publisher information is printed?

Here I recommend an architecture learning Communication group. Exchange Learning Group number: 697-57-97-51 It will share some of the video footage recorded by experienced architects: Spring,mybatis,netty source analysis, high concurrency, performance, distributed, microservices architecture, JVM performance optimization, The distributed architecture is a necessary knowledge system for architects. You can also receive free learning resources, and now benefit a lot

First, source code analysis

Second, distributed architecture

Third, micro-services

Four, performance optimization

V. Java ENGINEERING

Alibaba Micro Service expert handwriting Spring Boot implements a simple auto-configuration module

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.