In previous tutorial we saw how to use annotation based load configuration file also how to import multiple files in XML based configuration. Here you will ho

Spring 3 Import multiple files through java annotation

In previous tutorial we saw how to use annotation based load configuration file also how to import multiple files in XML based configuration. Here you will how to import multiple files if you are using java annotation based configuration to load beans below is tag to load config files:

@Configuration
@Import ({PersonConfig.class, Test.class})
public class ApplicationConfiguration {

}

Please see full working example below:

  • Project structure:

Spring 3 Import multiple files through java annotation

 

  • Create Person.java interface inside com.javahonk.bean package
package com.javahonk.bean;

public interface Person {

    public String getName();
}
  • implements Person interface in class PersonImpl inside package com.javahonk.bean
package com.javahonk.bean;

public class PersonImpl implements Person {

    public String getName() {
        return "Java Honk";
    }

}

  • Create PersonConfig.java inside package com.javahonk
package com.javahonk;

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

import com.javahonk.bean.Person;
import com.javahonk.bean.PersonImpl;

@Configuration
public class PersonConfig {

    @Bean(name = "personBean")
    public Person getName(){
        return new PersonImpl();
    }
}

  • Create main ApplicationConfiguration.java configuration file where will import PersonConfig.java file:
package com.javahonk;

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

@Configuration
@Import ({PersonConfig.class})
public class ApplicationConfiguration {

}

  • Now create BeanAnnotationTest.java to load and test our configuration:
package com.javahonk;

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

import com.javahonk.bean.Person;

public class BeanAnnotationTest {

    public static void main(String[] args) {

        
        System.out.println("Load application context "
                + "through annotation");
        ApplicationContext context = new 
                AnnotationConfigApplicationContext(
                        ApplicationConfiguration.class);
        Person person = (Person) context.getBean("personBean");
        System.out.println("Name: "+person.getName());
        ((ConfigurableApplicationContext)context).close();

    }

}

  • To test right click BeanAnnotationTest.java –> Run As –> Java Application you will below output on console:

Spring 3 Import multiple files through java annotation

That’s it Spring 3 Import multiple files through java annotation

Leave a Reply

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