📘 Premium Read: Access my best content on Medium member-only articles — deep dives into Java, Spring Boot, Microservices, backend architecture, interview preparation, career advice, and industry-standard best practices.
✅ Some premium posts are free to read — no account needed. Follow me on Medium to stay updated and support my writing.
🎓 Top 10 Udemy Courses (Huge Discount): Explore My Udemy Courses — Learn through real-time, project-based development.
▶️ Subscribe to My YouTube Channel (172K+ subscribers): Java Guides on YouTube
@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.
@Configuration
public class AppConfiguration2 {
@Bean(name="twitterService")
public MessageService twitterService(){
return new TwitterService();
}
@Bean
public MessageProcessor messageProcessor(){
return new MessageProcessorImpl(twitterService());
}
}
@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();
}
}
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);
}
}
public class TestApplication {
public static void main(String[] args) {
ApplicationContext applicationContext = new AnnotationConfigApplicationContext(AppConfiguration.class);
MessageProcessor userService = applicationContext.getBean(MessageProcessor.class);
userService.processMsg("twitter message sending ");
}
}
twitter 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.
Github Repository: Spring Java-based configuration
Articles Related to Java-based Container Configuration -
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.
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.
In this article, we will discuss how to use Spring @Import annotation with examples.
>> Spring @ImportResource Annotation Example
Comments
Post a Comment
Leave Comment