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 purpose of @ SpringBootApplication annotations in springboot

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

Share

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

What is the function of @ SpringBootApplication annotation in springboot? this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible way.

SpringBootApplication is the basic annotation of springboot, which is written on the startup class of springboot to enable the automatic configuration of springboot.

@ SpringBootApplication is a combined annotation. Post the source code first.

Package org.springframework.boot.autoconfigure;@Target (ElementType.TYPE) @ Retention (RetentionPolicy.RUNTIME) @ Documented@Inherited@SpringBootConfiguration@EnableAutoConfiguration@ComponentScan (excludeFilters = {@ Filter (type = FilterType.CUSTOM, classes = TypeExcludeFilter.class), @ Filter (type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class)) public @ interface SpringBootApplication {@ AliasFor (annotation = EnableAutoConfiguration.class) Class [] exclude () default {} @ AliasFor (annotation = EnableAutoConfiguration.class) String [] excludeName () default {}; @ AliasFor (annotation = ComponentScan.class, attribute = "basePackages") String [] scanBasePackages () default {}; @ AliasFor (annotation = ComponentScan.class, attribute = "basePackageClasses") Class [] scanBasePackageClasses () default {};}

You can see here that there are seven comments.

The @ Target (ElementType.TYPE), @ Retention (RetentionPolicy.RUNTIME), @ Documented, and @ Inherited annotations are native to Java, and the usage of this annotation is not explained here.

The key points are @ SpringBootConfiguration, @ EnableAutoConfiguration and @ ComponentScan.

Let's make an analysis.

@ ComponentScan, when you see this note, it is easy to think of a tag in the spring configuration

When you see this tag, you may understand that the specified package is scanned. Similarly, @ ComponentScans annotation can configure multiple packages to be scanned.

The @ EnableAutoConfiguration annotation is also common, which means to turn on automatic configuration

The above two annotations work together, scan the package, and then start automatic configuration. Both of these are spring notes, and those with spring background should not be a problem to understand.

@ SpringBootConfiguration is not native to spring, but clicking on the open source code reveals a magical thing.

@ Target (ElementType.TYPE) @ Retention (RetentionPolicy.RUNTIME) @ Documented@Configurationpublic @ interface SpringBootConfiguration {}

This is the whole code of the @ SpringBootConfiguration annotation, which he just uses the @ Configuration annotation.

To sum up, springboot's startup class does not use @ SpringBootApplication, but @ Configuration, @ EnableAutoConfiguration, @ ComponentScan, which can also achieve the purpose of startup. The purpose of @ SpringBootApplication is just to simplify, let developers write less code and achieve the same goal, which is also the withdrawal of the idea of Java encapsulation.

This is the answer to the question about the role of @ SpringBootApplication annotation in springboot. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel for more related knowledge.

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