Quick Contact

    Hibernate

    Spring Java Based Configuration Example

    We use @Configuration and @Bean annotations to develop spring application. Note that we haven’t used @Service or @Component annotation in this example. (annotation based configuration)

    In this article, we will use @Configuration, @Bean and @Import annotations to demonstrate Spring Java-based configuration example.

    Let’s create AppConfiguration and AppConfiguration2 Java classes and annotated with @Configuration annotation. In Java-based configuration auto component scanning not required because Spring beans are created by using factory methods annotated with @Bean annotation.

    @Import – much as the element is used within Spring XML files to aid in modularizing configurations, the @Import annotation allows for loading @Bean definitions from another configuration class.

    We have created two configuration files and now we will use @Import annotation to import an AppConfiguration2 file into a main AppConfiguration config file.

    	@Configuration
    	public class AppConfiguration2 {
    
    	@Bean(name="twitterService")
    	publicMessageServicetwitterService(){
    	return new TwitterService();
    	    }
    
    	    @Bean
    	publicMessageProcessormessageProcessor(){
    	return new MessageProcessorImpl(twitterService());
    	    }
    	}
    

    Let’s create main app Java-based configuration class.

    	@Configuration
    	@ComponentScan("com.ducatindia.springframework.di")
    	@Import(AppConfiguration2.class)
    	publicclassAppConfiguration {
    
    	@Bean(name="emailService")
    	publicMessageServiceemailService(){
    	returnnewEmailService();
    	    }
    
    	@Bean(name="smsService")
    	publicMessageServicesmsService(){
    	returnnewSMSService();
    	    }
    	}
    

    Create MessageService interface and it’s multiple implementations.

    	public interface MessageService {
    	public void sendMsg(String message);
    	}
    
    	publicclassSMSServiceimplementsMessageService{
    	publicvoidsendMsg(Stringmessage) {
    	System.out.println(message);
    	    }
    	}
    
    	publicclassEmailServiceimplementsMessageService{
    	publicvoidsendMsg(Stringmessage) {
    	System.out.println(message);
    	    }
    	}
    
    	publicclassTwitterServiceimplementsMessageService{
    	publicvoidsendMsg(Stringmessage) {
    	System.out.println(message);
    	    }
    	}
    
    	publicinterfaceMessageProcessor {
    	publicvoidprocessMsg(Stringmessage);
    	}
    	public class MessageProcessorImpl implements MessageProcessor {
    	privateMessageServicemessageService;
    
    	    @Autowired
    	publicMessageProcessorImpl(@Qualifier("TwitterService") MessageServicemessageService) {
    	this.messageService = messageService;
    	    }
    
    	public void processMsg(String message) {
    	messageService.sendMsg(message);
    	    }
    	}
    

    Let’s test this application:

    	public class TestApplication {
    	public static void main(String[] args) {
    	ApplicationContextapplicationContext = new AnnotationConfigApplicationContext(AppConfiguration.class);
    	MessageProcessoruserService = applicationContext.getBean(MessageProcessor.class);
    	userService.processMsg("Student message sending ");
    	    }
    	}
    

    Output:

    Student message sending

    In this example, we used some of the annotations like @Configuration, @Bean, and @Import annotations to demonstrate Spring Java-based configuration. The implementation of this simple Spring Java-based-configuration example can be found in the GitHub project – this is an Eclipse based project, so it should be easy to import and run as it is.

    Apply now for Advanced Java Training Course

    Copyright 1999- Ducat Creative, All rights reserved.

    Anda bisa mendapatkan server slot online resmi dan terpercaya tentu saja di sini. Sebagai salah satu provider yang menyediakan banyak pilihan permainan.