Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How Spring Boot configures Import

Shulou Source: shulou.com Published: 2022-06-02 05:14:36 09月23日 Update

Editor to share with you how to configure Spring Boot import, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to understand it!

We know that all Bean and other configurations can be configured with an @ Configuration configuration file in Spring Boot, but there is no need to do so.

We can separate the relevant configurations and put them into multiple @ Configuration, such as the following reference:

MainConfiguration: main configuration of the project

DataSoureceConfiguration: data source configuration

RedisConfiguration:Redis configuration

MongoDBConfiguration:MongoDB configuration

At this point, the @ Import annotation can come in handy. Take a look at its source code:

@ Target (ElementType.TYPE) @ Retention (RetentionPolicy.RUNTIME) @ Documentedpublic @ interface Import {/ * * {@ link Configuration}, {@ link ImportSelector}, {@ link ImportBeanDefinitionRegistrar} * or regular component classes to import. * / Class [] value ();}

You can import these three types of registration files:

Configuration

ImportSelector

ImportBeanDefinitionRegistrar

The first one has already been talked about, and the other two will talk later. Follow the official Wechat account of the Java technology stack to get the first push.

Let's take a look at an example of @ Import and import other configuration files directly:

@ Configuration@Import ({RedisConfiguration.class}) public class MainConfiguration {/ /.}

Of course, if these configuration files are under the classscan path, you can do it directly with @ ComponentScan instead of importing @ Import annotations. This default @ SpringBootApplication annotation is included and does not need to be repeated.

Furthermore, the @ Enable* annotation can also come in handy:

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

Specific can refer to this article: Spring Enable* advanced applications and principles, in addition, follow the Java technology stack Wechat official account, in the background reply: spring, you can get more Spring practical information.

So the problem is, the old project wants to use the Spring Boot framework, but there are still a large number of XML configuration files, it is difficult to migrate to the @ Configuration configuration file, what to do?

The foreman will continue to give you advice!

The answer is: @ ImportResource annotation, take a look at its source code:

@ Retention (RetentionPolicy.RUNTIME) @ Target (ElementType.TYPE) @ Documentedpublic @ interface ImportResource {@ AliasFor ("locations") String [] value () default {}; @ AliasFor ("value") String [] locations () default {}; Class

Tags: Configuration files comments articles paths references public content technology more source code usage examples projects advanced necessary not much not two principles Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno OPPO Reno NVidia Microsoft Shulou Information Shulou Tech Info