Jakov
Jakov

Reputation: 989

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

@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);
    }
}

Upvotes: 1

Views: 1653

Answers (1)

davidxxx
davidxxx

Reputation: 131326

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.

Upvotes: 2

Related Questions