“This article has participated in the call for good writing activities, click to view: the back end, the big front end double track submission, 20,000 yuan prize pool waiting for you to challenge!”

Friends early ah, recently is the dog days, really is super hot 😱

First up ~ (ha ha ha)

Springboot Custom starter

Now, let’s share the Springboot custom starter πŸ˜„

Go directly to the official documentation of Springboot to see ~

Docs. Spring. IO/spring – the boot…

You can see that it is in this auto-configured directory, as analyzed in the previous two articles πŸ‘‡

πŸ‘‰ took 2 hours to figure out why this AOP did not take effect, but water friends fell asleep…

Principle analysis πŸ‘‰ Springboot automatic assembly principle exploration

The automatic configuration and customization of stater have been introduced

Auto-configuration can be associated to a β€œstarter” that provides the auto-configuration code as well as the typical libraries that you would use with it.

This means that the auto-configuration classes and dependencies can be packaged as a starter for others to use

After reading the official website, I found that there seems to be no good writing πŸ™ƒ

That’s all I know… 🐷

  • Automatic configuration of various conditional annotations
  • Customize the naming conventions of the starterSuch as:xxx-spring-boot-starter

So let’s take a look at this example, pa πŸ˜„

Train of thought

  1. Prepare the automatic configuration class
  2. configurationspring.factories
  3. Deploy to the local repository
  4. Reference in other projects

Automatic configuration class

See the Redis auto-configuration class here

A number of conditional annotations are used, such as:

  • @ConditionalOnClass(RedisOperations.class)When there is thisRedisOperations.class“, the configuration is valid, and this configuration is present in the JAR we introduced
  • @ConditionalOnMissingBean(name = "redisTemplate")Without thisredisTemplateIs valid when the bean
  • @ConditionalOnSingleCandidate(RedisConnectionFactory.class)Make sure thatRedisConnectionFactoryCreate this after you create itredisTemplate

Then, our own is not so complicated, that’s all

@EnableConfigurationProperties(HelloProperties.class) public class HelloAutoConfiguration { @Bean @ConditionalOnMissingBean(HelloTest.class) public HelloTest getHelloTest(){ return new HelloTest(); }}Copy the code

Read the HelloProperties configuration class and create the HelloTest bean πŸ˜„

Configuration of two.spring.factories

Configure meta-INF /spring.factories as follows:

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
  com.java4ye.demo.config.HelloAutoConfiguration
Copy the code

Deploy to a local repository

Refer to the naming method on the official website to configure our POM file πŸ‘‡

< the groupId > org. 4 ye < / groupId > < artifactId > hello - spring - the boot - starter < / artifactId > < version > 1.0 - the SNAPSHOT < / version >Copy the code

Select JAR
for packaging

Here you can simply execute maven install

Pay attention to details

  1. Package is only packaged, install deploys to local repositories, and deploy deploys local and remote repositories
  2. spring-boot-configuration-processorWhat this package does:Generates metadata information for custom configuration classes
  3. pomFile importprocessoradd<optional>true</optional>, giving other items a choice will not add unnecessary dependencies

Iv. Use in other projects

Just import the JAR package in other projects

<dependency> <groupId>org.4ye</groupId> <artifactId>hello-spring-boot-starter</artifactId> < version > 1.0 - the SNAPSHOT < / version > < / dependency >Copy the code

As for the code, I put it on GitHub

Address πŸ‘‰ github.com/RyzeYang/sp…

conclusion

Finally, as usual, draw a graph to summarize

The last

Welcome friends to discuss the question ~

If you think this article is good, please give it a thumbs-up 😝

Let’s start this unexpected meeting! ~

Welcome to leave a message! Thanks for your support! γƒΎ(≧▽≦*)o go!!

I’m 4ye. We should… next time. See you soon!! πŸ˜†