is now
Spring Core - @Import annotation

In this post, we will learn about @Import annotation and its usage. You can see my previous post on how to create a simple spring core project.

What is @Import annotation and usage?

@Import annotation is equivalent to <import/> element in Spring XML configuration. It helps in splitting the single Java based configuration file into small, modular, maintainable and component based configuration. Let's see it with example.

@Import(value = { DBConfig.class, WelcomeGbConfig.class })
public class HelloGbAppConfig {


In above code snippet, we are importing two different configuration files viz. DBConfig, WelcomeGbConfig in application level configuration file HelloGbAppConfig.

The above code is equivalent to Spring XML based configuration below.

<beans xmlns=""

  <import resource="config/welcomegbconfig.xml"/>
  <import resource="config/dbconfig.xml"/>


You can see the full example code for Java based configuration on Github.

No comments :

Post a Comment