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

How Spring Boot configures Import

2025-04-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

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

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