Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the automatic configuration principle of SpringBoot and custom SpringBootStarter

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article shows you what is the automatic configuration principle of SpringBoot and custom SpringBootStarter, the content is concise and easy to understand, can definitely brighten your eyes, through the detailed introduction of this article, I hope you can get something.

Take the implementation of SpringBootAdmin as a reference

# effect achieved:

Add related dependencies and add @ EnableXXX to the startup class to use the relevant functionality.

# principle-SpringBootAdmin as an example

Mark the @ EnableAdminServer annotation on the startup class

@ EnableAdminServer@SpringBootApplicationpublic class AdminApplication {public static void main (String [] args) {SpringApplication.run (AdminApplication.class, args);}}

Click in and find that you only import a class AdminServerMarkerConfiguration.class.

@ Target (ElementType.TYPE) @ Retention (RetentionPolicy.RUNTIME) @ Documented@Import (AdminServerMarkerConfiguration.class) public @ interface EnableAdminServer {}

Click into this class AdminServerMarkerConfiguration to find that you only add a bean Marker to the container

Public class AdminServerMarkerConfiguration {@ Bean public Marker adminServerMarker () {return new Marker ();} public static class Marker {}}

So the summary of this section is: add the @ EnableAdminServer annotation and add a bean of type Marker to the container

Next, we find the AdminServerAutoConfiguration class. Generally speaking, the default SpringBoot autoconfiguration class is named after XXXAutoConfiguration, so it is easy to find, such as RedisAutoConfiguration....

@ Configuration@ConditionalOnBean (AdminServerMarkerConfiguration.Marker.class) @ EnableConfigurationProperties (AdminServerProperties.class) @ Import ({AdminServerWebConfiguration.class}) public class AdminServerAutoConfiguration {@ Bean @ ConditionalOnMissingBean public InstanceRegistry instanceRegistry (InstanceRepository instanceRepository, InstanceIdGenerator instanceIdGenerator) {return new InstanceRegistry (instanceRepository, instanceIdGenerator);} @ Bean @ ConditionalOnMissingBean public InstanceIdGenerator instanceIdGenerator () {return new HashingInstanceUrlIdGenerator ();}}

The following points can be found in the interpretation of the notes for this category:

@ ConfigurationProperties ("spring.boot.admin") public class AdminServerProperties {...}

@ Import ({AdminServerWebConfiguration.class}) imports another configuration class to add additional bean to the container

@ Configuration this is a configuration class. You can see that there are many bean defined by @ Bean in this class.

@ ConditionalOnBean (AdminServerMarkerConfiguration.Marker.class) [key], determine if there is this bean in the container. This is the function of adding @ EnableAdminServer to the startup class in the previous section. When the bean of Marker is in the container, the Configuration will take effect and the bean defined in this class will be added to the container.

@ EnableConfigurationProperties (AdminServerProperties.class) launches the configuration class AdminServerProperties and binds the configuration class to the configuration file

The next question is: how to make Spring scan to this configuration class at startup? by analyzing the implementation of @ EnableAutoConfiguration annotation, we can know that if you configure the full path of the class that needs to be configured automatically, META-INF/spring.factories,Spring will scan all META-INF/spring.factories files under the classpath at startup.

Such as the configuration of SpringBootAdmin:

Org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ de.codecentric.boot.admin.server.config.AdminServerAutoConfiguration,\ de.codecentric.boot.admin.server.config.AdminServerNotifierAutoConfiguration,\ de.codecentric.boot.admin.server.config.AdminServerHazelcastAutoConfiguration,\ de.codecentric.boot.admin.se rver.config.AdminServerCloudFoundryAutoConfiguration

Complete ~

# another example: automatic configuration of Redis in the project

It is easier to use Redis in SpringBoot projects. You can use it by adding Redis dependencies.

Org.springframework.boot spring-boot-starter-data-redis

How does SpringBoot make it possible to use Redis,RedisTemplate by adding redis dependencies?

Principle:

Always the first step: find Redis's autoconfiguration class RedisAutoConfiguration

@ Configuration@ConditionalOnClass (RedisOperations.class) @ EnableConfigurationProperties (RedisProperties.class) @ Import ({LettuceConnectionConfiguration.class, JedisConnectionConfiguration.class}) public class RedisAutoConfiguration {@ Bean @ ConditionalOnMissingBean (name = "redisTemplate") public RedisTemplate redisTemplate (RedisConnectionFactory redisConnectionFactory) throws UnknownHostException {RedisTemplate template = new RedisTemplate (); template.setConnectionFactory (redisConnectionFactory); return template } @ Bean @ ConditionalOnMissingBean public StringRedisTemplate stringRedisTemplate (RedisConnectionFactory redisConnectionFactory) throws UnknownHostException {StringRedisTemplate template = new StringRedisTemplate (); template.setConnectionFactory (redisConnectionFactory); return template;}}

@ Configuration

@ ConditionalOnClass (RedisOperations.class) when there is a RedisOperations class under the classpath, it is obvious that this class must have been imported when adding redis dependencies. Now take a look at the path of this class. It is indeed imported through redis's startup dependency, which is why redis's startup dependency is added so that redis can be used directly.

@ EnableConfigurationProperties (RedisProperties.class) activate the configuration class RedisProperties

@ ConfigurationProperties (prefix = "spring.redis") public class RedisProperties {/ * * Database index used by the connection factory. * / private int database = 0; / * * Connection URL. Overrides host, port, and password. User is ignored. Example: * redis://user:password@example.com:6379 * / private String url; / * Redis server host. * / private String host = "localhost"; / * * Login password of the redis server. * / private String password; / * * Redis server port. * / private int port = 6379;. }

@ Import ({LettuceConnectionConfiguration.class, JedisConnectionConfiguration.class}) imports LettuceConnectionConfiguration and JedisConnectionConfiguration, both clients of redis. So which one is enabled?

LettuceConnectionConfiguration

@ Configuration@ConditionalOnClass (RedisClient.class) class LettuceConnectionConfiguration extends RedisConnectionConfiguration {}

JedisConnectionConfiguration

@ Configuration@ConditionalOnClass ({GenericObjectPool.class, JedisConnection.class, Jedis.class}) class JedisConnectionConfiguration extends RedisConnectionConfiguration {}

You can see that when there is a responsive client under the classpath, the corresponding automatic configuration class is enabled.

SpringBoot uses lettuce by default.

The above is what is the automatic configuration principle of SpringBoot and custom SpringBootStarter, have you learned the knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report