Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the use case of @Import annotation?

According to official doc:

Annotation Type Configuration

Indicates that a class declares one or more @Bean methods and may be processed by the Spring container to generate bean definitions...

@Configuration classes may be composed using the @Import annotation, not unlike the way that works in Spring XML. Because @Configuration objects are managed as Spring beans within the container..

But i can also use @Configuration annotation without @Import. I have tested the code listed below and it works as expected. So what is the purpose to use @Import?

DispatcherServletInitializer

public class ApplicationInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {

    @Override
    protected Class<?>[] getRootConfigClasses() {
        return new Class<?>[] { WebConfig.class };
    }

    @Override
    protected Class<?>[] getServletConfigClasses() {
        return null;
    }

    @Override
    protected String[] getServletMappings() {
        return new String[] { "/" };
    }
    
 }

WebMvcConfigurerAdapter

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = { "package.name" })
// @Import(OptionalConfig.class) 
public class WebConfig extends WebMvcConfigurerAdapter {
    // ...
}

OptionalConfig

@Configuration
public class OptionalConfig {
    
    @Bean(name = "myClass")
    public MyClass myClass() {
        return new MyClass();
    }
}

Service

@Service
public class MyServiceImpl implements MyService {
    
    @Autowired
    private MyClass myClass;    // yes, it works

    // ...
}
like image 425
enzo Avatar asked Feb 19 '16 09:02

enzo


People also ask

What is the use of @import annotation?

Indicates that a class declares one or more @Bean methods and may be processed by the Spring container to generate bean definitions... @Configuration classes may be composed using the @Import annotation, not unlike the way that works in Spring XML.

What is the use of @SpringBootApplication?

Spring Boot @SpringBootApplication annotation is used to mark a configuration class that declares one or more @Bean methods and also triggers auto-configuration and component scanning. It's same as declaring a class with @Configuration, @EnableAutoConfiguration and @ComponentScan annotations.

What is the @bean annotation?

One of the most important annotations in spring is the @Bean annotation which is applied on a method to specify that it returns a bean to be managed by Spring context. Spring Bean annotation is usually declared in Configuration classes methods. This annotation is also a part of the spring core framework.

What is @component annotation in Spring boot?

@Component is an annotation that allows Spring to automatically detect our custom beans. In other words, without having to write any explicit code, Spring will: Scan our application for classes annotated with @Component. Instantiate them and inject any specified dependencies into them. Inject them wherever needed.


2 Answers

If component scanning is enabled, you can split bean definitions in multi @Configuration classes without using @Import. And you don't need to provide all of them to the application context constructor.

I think the main purpose for @Import is to provide you a way to simplify multiple configurations registration if you'd like to avoid component scanning (as of Spring Framework 4.2, per reference manual).

There's a note in Spring Reference Documentation about @Import usage:

As of Spring Framework 4.2, @Import also supports references to regular component classes, analogous to the AnnotationConfigApplicationContext.register method. This is particularly useful if you’d like to avoid component scanning, using a few configuration classes as entry points for explicitly defining all your components.

like image 104
yixiang Avatar answered Sep 21 '22 22:09

yixiang


Thus far, we've seen how to break up bean definitions into multiple @Configuration classes and how to reference those beans across @Configuration boundaries. These scenarios have required providing all @Configuration classes to the constructor of a JavaConfigApplicationContext, and this is not always ideal. Often it is preferable to use an aggregation approach, where one @Configuration class logically imports the bean definitions defined by another.

The @Import annotation provides just this kind of support, and it is the direct equivalent of the <import/> element found in Spring beans XML files.

http://docs.spring.io/spring-javaconfig/docs/1.0.0.M4/reference/html/ch04s03.html

like image 30
Marcel Avatar answered Sep 20 '22 22:09

Marcel