Spring Java Based Configuration Example

In this article, we will quickly discuss how to develop a simple Spring application using Java-based configuration.

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")
    public MessageService twitterService(){
        return new TwitterService();
    }
  
    @Bean
    public MessageProcessor messageProcessor(){
        return new MessageProcessorImpl(twitterService());
    }
}

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


@Configuration
@ComponentScan("com.javadevsguide.springframework.di")
@Import(AppConfiguration2.class)
public class AppConfiguration {

    @Bean(name="emailService")
    public MessageService emailService(){
        return new EmailService();
    }
 
    @Bean(name="smsService")
    public MessageService smsService(){
        return new SMSService();
    }
}

Create MessageService interface and it's multiple implementations.


public interface MessageService {
    public void sendMsg(String message);
}


public class SMSService implements MessageService{
    public void sendMsg(String message) {
        System.out.println(message);
    }
}


public class EmailService implements MessageService{
    public void sendMsg(String message) {
        System.out.println(message);
    }
}


public class TwitterService implements MessageService{
    public void sendMsg(String message) {
        System.out.println(message);
    }
}


public interface MessageProcessor {
    public void processMsg(String message);
}


public class MessageProcessorImpl implements MessageProcessor {
    private MessageService messageService;
    
    @Autowired
    public MessageProcessorImpl(@Qualifier("TwitterService") MessageService messageService) {
        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) {
        ApplicationContext applicationContext = new AnnotationConfigApplicationContext(AppConfiguration.class);
        MessageProcessor  userService = applicationContext.getBean(MessageProcessor.class);
    }
}

Output:

twitter message sending 

Articles Related to Java-based Container Configuration -

>> Spring Java Based Configuration Basics

In this article, we will briefly look into basics of Spring Java-based container configuration.

>> Spring Java Based Configuration Example

In this post, we will demonstrate the usage of Spring Java-based container configuration.

>> Spring @Bean Annotation with Example

In this article, we will discuss Spring Java configuration based @Bean annotation with examples. We will also discuss different scenarios of how and when to use @Bean annotation.

>> Spring @Configuration Annotation with Example

In this article, we will discuss a very important Spring Java based configuration annotation that is a @Configuration annotation with examples.

>> Spring @PropertySource Annotation with Example

In Spring, you can use @PropertySource annotation to externalize your configuration to a properties file. In this article, we will discuss how to use @PropertySource to read a properties file and display the values with @Value and Environment.

>> Spring @Import Annotation with Example

In this article, we will discuss how to use Spring @Import annotation with examples.

>> Spring @ImportResource Annotation Example

In this article, we will quickly discuss how to use @ImportResource annotation with a simple Spring boot application.