Spring 3 @Import annotation with JavaConfig Example

We have already learnt about JavaConfig in Spring 3Usually in spring we will split a large XML configuration files into one or more smaller xml files for maintainability and we will finally import it in a common configuration file. @Import annotation does the same functionality for JavaConfig in Spring 3. Lets take the below XML

<beans xmlns="http://www.springframework.org/schema/beans" 
xmlns:context="http://www.springframework.org/schema/context" 
xmlns:mvc="http://www.springframework.org/schema/mvc" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation=" http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd>

  <import resource="StaffConfig.xml"/>
  <import resource="StudentConfig.xml"/>
</beans>

In Spring 3 JavaConfig we will use @Import to achieve the same functionality.

package com.javainterviewpoint;

import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

@Configuration
@Import({StaffConfig.class,StudentConfig.class})
public class SpringConfig 
{
}

Lets now see how to use @Import with JavaConfig in Spring

Folder Structure:

  1. Create a new Java Project  SpringCoreTutorial” and create a package for our src files com.javainterviewpoint
  2. Add the required libraries to the build path. Java Build Path ->Libraries ->Add External JARs and add the below jars.

    commons-logging-1.1.1.jar
    spring-beans-3.2.9.RELEASE.jar
    spring-core-3.2.9.RELEASE.jar
    spring-context-3.2.9.RELEASE.jar
    spring-expression-3.2.9.RELEASE.jar

  3. Create the Java classes Staff.java, StaffConfig.java, Student.java, StudentConfig.java, SpringConfig.java and Logic.java under  com.javainterviewpoint folder.

Staff.java

Our Staff Class is a simple class with show() method

package com.javainterviewpoint;

public class Staff 
{
    public void show(String message)
    {
        System.out.println(message+" Welcomes you all");
    }
}

StaffConfig.java

The StaffConfig class is the JavaConfig class corresponding to the Staff class

package com.javainterviewpoint;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class StaffConfig 
{
    @Bean(name="staff")
    public Staff getStaff()
    {
        return new Staff();
    }
}

Student.java

Student Class is also similar to our Staff class which also has a show() method called from our Logic class.

package com.javainterviewpoint;

public class Student 
{
    public void show(String message)
    {
        System.out.println(message+" Welcomes you all");
    }
}

StudentConfig.java

The StudentConfig class is the JavaConfig class corresponding to the Student class

package com.javainterviewpoint;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class StudentConfig 
{
    @Bean(name="student")
    public Student getStudent()
    {
        return new Student();
    }
}

SpringConfig.java

The SpringConfig does the import work here, we will use @Import annotation to import the StaffConfig and StudentConfig classes. We will be reading the SpringConfig in our Logic.java to get bean details.

package com.javainterviewpoint;

import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

@Configuration
@Import({StaffConfig.class,StudentConfig.class})
public class SpringConfig 
{
}

Logic.java

  • Through AnnotationConfigApplicationContext we will get the context by reading our SpringConfig class
  • Both Staff and Student class instance is obtained by calling the getBean() method over the applicationContext.
  • The parameter passed to the getBean(“staff”) and getBean(“student”) should be the name defined in @Bean annotation @Bean(name=”staff”),@Bean(name=”student”)
package com.javainterviewpoint;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class Logic 
{
    public static void main(String args[])
    {
        //Reading the SpringConfig.xml
        ApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class);
        
        //Get the Staff instance
        Staff staff = (Staff)context.getBean("staff");
        staff.show("Staff Class");
        
        //Get the Student instance
        Student student = (Student)context.getBean("student");
        student.show("Studnet Class");
    }
}

Output

Upon running our Logic class we will get the below output

Staff Class Welcomes you all
Studnet Class Welcomes you all

Leave a Reply

Your email address will not be published. Required fields are marked *