Can Spring Boot @Import annotation be used without @Configuration tag

CarlosV

@Import annotation in Spring is used in order to group configurations.

I know that the standard syntax for this annotation looks like this:

@Configuration
@Import({ Manager.class, Programmer.class })
class WorkerConfiguration {
}

But I am wondering is it possible to use @Import annotation to import a group of those annotations outside of a configuration file(maybe in the main file).

Example:

@SpringBootApplication
@Import({ Manager.class, Programmer.class })
public class App{

    public static void main(String[] args) {
        SpringApplication.run(App.class, args);
    }
}
davidxxx

Import Javadoc :

Indicates one or more component classes to import — typically @Configuration classes.

@Import is often used in the context of a class annotated with @Configuration class to include some declared beans in @Configuration in another one. But it also works with composite annotations that contains among other annotations the @Configuration one.
And in Spring Boot it turns out that several annotations include @Configuration:

For example @SpringBootApplication that you ask to is also composed (among other things) of a @Configuration annotation :

Indicates a configuration class that declares one or more @Bean methods and also triggers auto-configuration and component scanning. This is a convenience annotation that is equivalent to declaring @Configuration, @EnableAutoConfiguration and @ComponentScan.

So yes what you want to do is valid.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

@Autowired annotation without spring boot

Creating KafkaListener without Annotation & without Spring Boot

Spring Boot tests: exclude one specific @Configuration class without using the @Profile annotation

How @Bean is used in a class without @Configuration when configuring Spring Security in a Spring Boot app?

Can an annotation type without a @Target be used anywhere?

Spring boot auto configuration with dependency and without @ComponentScan

Spring Boot jupiter annotation custom extension configuration not working

Why Spring Boot Application class needs to have @Configuration annotation?

@ConfigurationProperties Spring Boot Configuration Annotation Processor not found in classpath

@ConfigurationProperties Spring Boot Configuration Annotation Processor not found in classpath

How to configure for Spring Boot Configuration Annotation Processor using @ConfigurationProperties on IntelliJ?

@Given or @When annotation of cucumber is throwing errors related spring boot configuration

How can I define a custom ObjectMapper bean without overriding the one used by Spring Boot

Can I start with a spring boot application without the annotations componentscan,autoconfiguration,configuration,springbootapplication?

Can I use @Profile Tags on Components without creating an @Configuration class in Spring Boot or a Web.xml

can Spring boot be used in a "one shot" program

Weird behaviour of import tag in spring's configuration file

Spring Boot & Hibernate: NVARCHAR column type without @Type annotation

Spring-boot scheduler runs without @EnableScheduling annotation

eager load singleton object in spring boot without annotation

How to schedule a cron job in spring boot without using @Scheduled() annotation

Spring boot, declare Service and inject class without annotation

Spring Boot external configuration without ignoring the packaged configuration

Autowiring and annotation configuration in Spring

Spring custom configuration annotation

How can I use/import the @EnableRedisHttpSession Spring annotation?

Spring rest MultiPart file upload with Java configuration without Spring boot

Spring Boot 2.1 - @WebMvcTest without Spring Security Auto-Configuration

Spring boot - A single @Configuration @import will work but multiple will not, why?