Inject a list of processes to execute using spring DI

Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;
up vote
2
down vote
favorite
I have couple of MessageProcessors. One of the Processor will log the payload. Other Processor will save into an embedded database.
Here is the code I have written so far:
I am using annotation-driven configuration so I have omitted other classes which are not used in this use case.
@Component
public class ProcessorConfiguration
@Bean
@Qualifier("textMp")
public MessageProcessor textMessageProcessor()
return new TextMessageProcessor();
@Bean
@Qualifier("employMp")
public MessageProcessor employMessageProcessor()
return new EmployMessageProcessor();
@Bean
private List<MessageProcessor> messageProcessorList(@Qualifier("textMp") MessageProcessor textMessageProcessor,
@Qualifier("employMp") MessageProcessor employMessageProcessor)
List<MessageProcessor> list = new ArrayList<>();
list.add(textMessageProcessor);
list.add(employMessageProcessor);
return list;
This class is responsible for all the JMS messages the application receives.
public class MessageHandler
@Autowired
private List<MessageProcessor> messageProcessors;
public void handleMessage(Notification notification)
messageProcessors.forEach(processor -> processor.doProcess(notification));
public interface MessageProcessor
void doProcess(Notification notification);
public class TextMessageProcessor implements MessageProcessor
private static final Logger logger = Logger.getLogger(TextMessageProcessor.class);
@Override
public void doProcess(Notification notification)
logger.info("The payload is " + notification.getText());
I have created a builder which takes String as input and returns an Employ object.
@Service
public class EmployMessageProcessor implements MessageProcessor
@Autowired
private EmployDao dao;
@Override
public void doProcess(Notification notification)
Employ employ = EmployBuilder.buildEmploy(notification.getText());
dao.save(employ);
public interface Notification
String getText();
I think, the way I am injecting the processors can be improved. Please review my code and provide your valuable feedback.
java spring
add a comment |Â
up vote
2
down vote
favorite
I have couple of MessageProcessors. One of the Processor will log the payload. Other Processor will save into an embedded database.
Here is the code I have written so far:
I am using annotation-driven configuration so I have omitted other classes which are not used in this use case.
@Component
public class ProcessorConfiguration
@Bean
@Qualifier("textMp")
public MessageProcessor textMessageProcessor()
return new TextMessageProcessor();
@Bean
@Qualifier("employMp")
public MessageProcessor employMessageProcessor()
return new EmployMessageProcessor();
@Bean
private List<MessageProcessor> messageProcessorList(@Qualifier("textMp") MessageProcessor textMessageProcessor,
@Qualifier("employMp") MessageProcessor employMessageProcessor)
List<MessageProcessor> list = new ArrayList<>();
list.add(textMessageProcessor);
list.add(employMessageProcessor);
return list;
This class is responsible for all the JMS messages the application receives.
public class MessageHandler
@Autowired
private List<MessageProcessor> messageProcessors;
public void handleMessage(Notification notification)
messageProcessors.forEach(processor -> processor.doProcess(notification));
public interface MessageProcessor
void doProcess(Notification notification);
public class TextMessageProcessor implements MessageProcessor
private static final Logger logger = Logger.getLogger(TextMessageProcessor.class);
@Override
public void doProcess(Notification notification)
logger.info("The payload is " + notification.getText());
I have created a builder which takes String as input and returns an Employ object.
@Service
public class EmployMessageProcessor implements MessageProcessor
@Autowired
private EmployDao dao;
@Override
public void doProcess(Notification notification)
Employ employ = EmployBuilder.buildEmploy(notification.getText());
dao.save(employ);
public interface Notification
String getText();
I think, the way I am injecting the processors can be improved. Please review my code and provide your valuable feedback.
java spring
Does it work as expected so far?
â Mast
May 13 at 13:49
@Mast : Yes, I do not have any bugs so far.
â NewUser
May 13 at 14:00
add a comment |Â
up vote
2
down vote
favorite
up vote
2
down vote
favorite
I have couple of MessageProcessors. One of the Processor will log the payload. Other Processor will save into an embedded database.
Here is the code I have written so far:
I am using annotation-driven configuration so I have omitted other classes which are not used in this use case.
@Component
public class ProcessorConfiguration
@Bean
@Qualifier("textMp")
public MessageProcessor textMessageProcessor()
return new TextMessageProcessor();
@Bean
@Qualifier("employMp")
public MessageProcessor employMessageProcessor()
return new EmployMessageProcessor();
@Bean
private List<MessageProcessor> messageProcessorList(@Qualifier("textMp") MessageProcessor textMessageProcessor,
@Qualifier("employMp") MessageProcessor employMessageProcessor)
List<MessageProcessor> list = new ArrayList<>();
list.add(textMessageProcessor);
list.add(employMessageProcessor);
return list;
This class is responsible for all the JMS messages the application receives.
public class MessageHandler
@Autowired
private List<MessageProcessor> messageProcessors;
public void handleMessage(Notification notification)
messageProcessors.forEach(processor -> processor.doProcess(notification));
public interface MessageProcessor
void doProcess(Notification notification);
public class TextMessageProcessor implements MessageProcessor
private static final Logger logger = Logger.getLogger(TextMessageProcessor.class);
@Override
public void doProcess(Notification notification)
logger.info("The payload is " + notification.getText());
I have created a builder which takes String as input and returns an Employ object.
@Service
public class EmployMessageProcessor implements MessageProcessor
@Autowired
private EmployDao dao;
@Override
public void doProcess(Notification notification)
Employ employ = EmployBuilder.buildEmploy(notification.getText());
dao.save(employ);
public interface Notification
String getText();
I think, the way I am injecting the processors can be improved. Please review my code and provide your valuable feedback.
java spring
I have couple of MessageProcessors. One of the Processor will log the payload. Other Processor will save into an embedded database.
Here is the code I have written so far:
I am using annotation-driven configuration so I have omitted other classes which are not used in this use case.
@Component
public class ProcessorConfiguration
@Bean
@Qualifier("textMp")
public MessageProcessor textMessageProcessor()
return new TextMessageProcessor();
@Bean
@Qualifier("employMp")
public MessageProcessor employMessageProcessor()
return new EmployMessageProcessor();
@Bean
private List<MessageProcessor> messageProcessorList(@Qualifier("textMp") MessageProcessor textMessageProcessor,
@Qualifier("employMp") MessageProcessor employMessageProcessor)
List<MessageProcessor> list = new ArrayList<>();
list.add(textMessageProcessor);
list.add(employMessageProcessor);
return list;
This class is responsible for all the JMS messages the application receives.
public class MessageHandler
@Autowired
private List<MessageProcessor> messageProcessors;
public void handleMessage(Notification notification)
messageProcessors.forEach(processor -> processor.doProcess(notification));
public interface MessageProcessor
void doProcess(Notification notification);
public class TextMessageProcessor implements MessageProcessor
private static final Logger logger = Logger.getLogger(TextMessageProcessor.class);
@Override
public void doProcess(Notification notification)
logger.info("The payload is " + notification.getText());
I have created a builder which takes String as input and returns an Employ object.
@Service
public class EmployMessageProcessor implements MessageProcessor
@Autowired
private EmployDao dao;
@Override
public void doProcess(Notification notification)
Employ employ = EmployBuilder.buildEmploy(notification.getText());
dao.save(employ);
public interface Notification
String getText();
I think, the way I am injecting the processors can be improved. Please review my code and provide your valuable feedback.
java spring
edited May 13 at 13:48
Mast
7,32563484
7,32563484
asked May 13 at 13:40
NewUser
1154
1154
Does it work as expected so far?
â Mast
May 13 at 13:49
@Mast : Yes, I do not have any bugs so far.
â NewUser
May 13 at 14:00
add a comment |Â
Does it work as expected so far?
â Mast
May 13 at 13:49
@Mast : Yes, I do not have any bugs so far.
â NewUser
May 13 at 14:00
Does it work as expected so far?
â Mast
May 13 at 13:49
Does it work as expected so far?
â Mast
May 13 at 13:49
@Mast : Yes, I do not have any bugs so far.
â NewUser
May 13 at 14:00
@Mast : Yes, I do not have any bugs so far.
â NewUser
May 13 at 14:00
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
1
down vote
accepted
This is a learning exercise for me as well so I hope I can be helpful with my answer.
- I think you can loose the Configuration class entirely. Add
@Component/@Serviceannotation to yourTextMessageProcessor. Spring should be
capable of figuring outMessageProcessor Listwiring on its own. Have you already tried that?
If you are worried about the order of the beans inside MessageProcessor List then you can use @Order annotation on your Service and/or Component.
@Component
@Order(value=1)
public class TextMessageProcessor implements MessageProcessor
Your Configuration class in its current form is conflicting with Open Closed principle. In other words, whenever you add a new
MessageProcessor, your configuration will grow. Imagine that configuration after you have 20 processors.Spring creates qualifier names automatically as your bean names.
From Spring documentation:
For a fallback match, the bean name is considered as a default qualifier value.
I recommend doing your wiring through constructors instead of fields (I am especially eyeballing that DAO of yours). That way you preserve an ability to test your code later on. You will need to inject objects through constructors in your tests.
You could move
LoggingtoMessageProcessorif it was an abstract class. Make a field or a method forLoggingwhich returns the logging instance for current class. (Stackoverflow link)
Thanks for the feedback. I agree with all your points but, I am a bit confused with the feedback about Logging. I will do some homework on this and update. Appreciate your help, thanks.
â NewUser
May 14 at 10:54
With abstract class you can implement a functionality beforehand. So when that class is extended, it will inherit part of the functionality. You can do the same with interface default method as well though. The advantage of implementing the logging in the PARENT blueprint is that you could force yourself to log in every child element if you deem necessary. Also you would not repeat yourself. TLDR: you don't need to implement logging in children since parent does it for you.
â Miko Nukka
May 14 at 11:13
I happened to think over the advantages and disadvantages of movingloggerto ParentClass. In the current approach, I have created one Logger instance per class as it is astaticfield. If I change to non-static, every object will have its own instance ofloggerwhich kinda freaks me out for the classes which serve most common requests in my application. It would be of great help if we get into a chat room and discuss your point of view on this? Please join this chat.stackexchange.com/rooms/77498/miko-nukka-dibya
â NewUser
May 15 at 6:56
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
This is a learning exercise for me as well so I hope I can be helpful with my answer.
- I think you can loose the Configuration class entirely. Add
@Component/@Serviceannotation to yourTextMessageProcessor. Spring should be
capable of figuring outMessageProcessor Listwiring on its own. Have you already tried that?
If you are worried about the order of the beans inside MessageProcessor List then you can use @Order annotation on your Service and/or Component.
@Component
@Order(value=1)
public class TextMessageProcessor implements MessageProcessor
Your Configuration class in its current form is conflicting with Open Closed principle. In other words, whenever you add a new
MessageProcessor, your configuration will grow. Imagine that configuration after you have 20 processors.Spring creates qualifier names automatically as your bean names.
From Spring documentation:
For a fallback match, the bean name is considered as a default qualifier value.
I recommend doing your wiring through constructors instead of fields (I am especially eyeballing that DAO of yours). That way you preserve an ability to test your code later on. You will need to inject objects through constructors in your tests.
You could move
LoggingtoMessageProcessorif it was an abstract class. Make a field or a method forLoggingwhich returns the logging instance for current class. (Stackoverflow link)
Thanks for the feedback. I agree with all your points but, I am a bit confused with the feedback about Logging. I will do some homework on this and update. Appreciate your help, thanks.
â NewUser
May 14 at 10:54
With abstract class you can implement a functionality beforehand. So when that class is extended, it will inherit part of the functionality. You can do the same with interface default method as well though. The advantage of implementing the logging in the PARENT blueprint is that you could force yourself to log in every child element if you deem necessary. Also you would not repeat yourself. TLDR: you don't need to implement logging in children since parent does it for you.
â Miko Nukka
May 14 at 11:13
I happened to think over the advantages and disadvantages of movingloggerto ParentClass. In the current approach, I have created one Logger instance per class as it is astaticfield. If I change to non-static, every object will have its own instance ofloggerwhich kinda freaks me out for the classes which serve most common requests in my application. It would be of great help if we get into a chat room and discuss your point of view on this? Please join this chat.stackexchange.com/rooms/77498/miko-nukka-dibya
â NewUser
May 15 at 6:56
add a comment |Â
up vote
1
down vote
accepted
This is a learning exercise for me as well so I hope I can be helpful with my answer.
- I think you can loose the Configuration class entirely. Add
@Component/@Serviceannotation to yourTextMessageProcessor. Spring should be
capable of figuring outMessageProcessor Listwiring on its own. Have you already tried that?
If you are worried about the order of the beans inside MessageProcessor List then you can use @Order annotation on your Service and/or Component.
@Component
@Order(value=1)
public class TextMessageProcessor implements MessageProcessor
Your Configuration class in its current form is conflicting with Open Closed principle. In other words, whenever you add a new
MessageProcessor, your configuration will grow. Imagine that configuration after you have 20 processors.Spring creates qualifier names automatically as your bean names.
From Spring documentation:
For a fallback match, the bean name is considered as a default qualifier value.
I recommend doing your wiring through constructors instead of fields (I am especially eyeballing that DAO of yours). That way you preserve an ability to test your code later on. You will need to inject objects through constructors in your tests.
You could move
LoggingtoMessageProcessorif it was an abstract class. Make a field or a method forLoggingwhich returns the logging instance for current class. (Stackoverflow link)
Thanks for the feedback. I agree with all your points but, I am a bit confused with the feedback about Logging. I will do some homework on this and update. Appreciate your help, thanks.
â NewUser
May 14 at 10:54
With abstract class you can implement a functionality beforehand. So when that class is extended, it will inherit part of the functionality. You can do the same with interface default method as well though. The advantage of implementing the logging in the PARENT blueprint is that you could force yourself to log in every child element if you deem necessary. Also you would not repeat yourself. TLDR: you don't need to implement logging in children since parent does it for you.
â Miko Nukka
May 14 at 11:13
I happened to think over the advantages and disadvantages of movingloggerto ParentClass. In the current approach, I have created one Logger instance per class as it is astaticfield. If I change to non-static, every object will have its own instance ofloggerwhich kinda freaks me out for the classes which serve most common requests in my application. It would be of great help if we get into a chat room and discuss your point of view on this? Please join this chat.stackexchange.com/rooms/77498/miko-nukka-dibya
â NewUser
May 15 at 6:56
add a comment |Â
up vote
1
down vote
accepted
up vote
1
down vote
accepted
This is a learning exercise for me as well so I hope I can be helpful with my answer.
- I think you can loose the Configuration class entirely. Add
@Component/@Serviceannotation to yourTextMessageProcessor. Spring should be
capable of figuring outMessageProcessor Listwiring on its own. Have you already tried that?
If you are worried about the order of the beans inside MessageProcessor List then you can use @Order annotation on your Service and/or Component.
@Component
@Order(value=1)
public class TextMessageProcessor implements MessageProcessor
Your Configuration class in its current form is conflicting with Open Closed principle. In other words, whenever you add a new
MessageProcessor, your configuration will grow. Imagine that configuration after you have 20 processors.Spring creates qualifier names automatically as your bean names.
From Spring documentation:
For a fallback match, the bean name is considered as a default qualifier value.
I recommend doing your wiring through constructors instead of fields (I am especially eyeballing that DAO of yours). That way you preserve an ability to test your code later on. You will need to inject objects through constructors in your tests.
You could move
LoggingtoMessageProcessorif it was an abstract class. Make a field or a method forLoggingwhich returns the logging instance for current class. (Stackoverflow link)
This is a learning exercise for me as well so I hope I can be helpful with my answer.
- I think you can loose the Configuration class entirely. Add
@Component/@Serviceannotation to yourTextMessageProcessor. Spring should be
capable of figuring outMessageProcessor Listwiring on its own. Have you already tried that?
If you are worried about the order of the beans inside MessageProcessor List then you can use @Order annotation on your Service and/or Component.
@Component
@Order(value=1)
public class TextMessageProcessor implements MessageProcessor
Your Configuration class in its current form is conflicting with Open Closed principle. In other words, whenever you add a new
MessageProcessor, your configuration will grow. Imagine that configuration after you have 20 processors.Spring creates qualifier names automatically as your bean names.
From Spring documentation:
For a fallback match, the bean name is considered as a default qualifier value.
I recommend doing your wiring through constructors instead of fields (I am especially eyeballing that DAO of yours). That way you preserve an ability to test your code later on. You will need to inject objects through constructors in your tests.
You could move
LoggingtoMessageProcessorif it was an abstract class. Make a field or a method forLoggingwhich returns the logging instance for current class. (Stackoverflow link)
edited May 14 at 4:23
Mast
7,32563484
7,32563484
answered May 13 at 22:13
Miko Nukka
262
262
Thanks for the feedback. I agree with all your points but, I am a bit confused with the feedback about Logging. I will do some homework on this and update. Appreciate your help, thanks.
â NewUser
May 14 at 10:54
With abstract class you can implement a functionality beforehand. So when that class is extended, it will inherit part of the functionality. You can do the same with interface default method as well though. The advantage of implementing the logging in the PARENT blueprint is that you could force yourself to log in every child element if you deem necessary. Also you would not repeat yourself. TLDR: you don't need to implement logging in children since parent does it for you.
â Miko Nukka
May 14 at 11:13
I happened to think over the advantages and disadvantages of movingloggerto ParentClass. In the current approach, I have created one Logger instance per class as it is astaticfield. If I change to non-static, every object will have its own instance ofloggerwhich kinda freaks me out for the classes which serve most common requests in my application. It would be of great help if we get into a chat room and discuss your point of view on this? Please join this chat.stackexchange.com/rooms/77498/miko-nukka-dibya
â NewUser
May 15 at 6:56
add a comment |Â
Thanks for the feedback. I agree with all your points but, I am a bit confused with the feedback about Logging. I will do some homework on this and update. Appreciate your help, thanks.
â NewUser
May 14 at 10:54
With abstract class you can implement a functionality beforehand. So when that class is extended, it will inherit part of the functionality. You can do the same with interface default method as well though. The advantage of implementing the logging in the PARENT blueprint is that you could force yourself to log in every child element if you deem necessary. Also you would not repeat yourself. TLDR: you don't need to implement logging in children since parent does it for you.
â Miko Nukka
May 14 at 11:13
I happened to think over the advantages and disadvantages of movingloggerto ParentClass. In the current approach, I have created one Logger instance per class as it is astaticfield. If I change to non-static, every object will have its own instance ofloggerwhich kinda freaks me out for the classes which serve most common requests in my application. It would be of great help if we get into a chat room and discuss your point of view on this? Please join this chat.stackexchange.com/rooms/77498/miko-nukka-dibya
â NewUser
May 15 at 6:56
Thanks for the feedback. I agree with all your points but, I am a bit confused with the feedback about Logging. I will do some homework on this and update. Appreciate your help, thanks.
â NewUser
May 14 at 10:54
Thanks for the feedback. I agree with all your points but, I am a bit confused with the feedback about Logging. I will do some homework on this and update. Appreciate your help, thanks.
â NewUser
May 14 at 10:54
With abstract class you can implement a functionality beforehand. So when that class is extended, it will inherit part of the functionality. You can do the same with interface default method as well though. The advantage of implementing the logging in the PARENT blueprint is that you could force yourself to log in every child element if you deem necessary. Also you would not repeat yourself. TLDR: you don't need to implement logging in children since parent does it for you.
â Miko Nukka
May 14 at 11:13
With abstract class you can implement a functionality beforehand. So when that class is extended, it will inherit part of the functionality. You can do the same with interface default method as well though. The advantage of implementing the logging in the PARENT blueprint is that you could force yourself to log in every child element if you deem necessary. Also you would not repeat yourself. TLDR: you don't need to implement logging in children since parent does it for you.
â Miko Nukka
May 14 at 11:13
I happened to think over the advantages and disadvantages of moving
logger to ParentClass. In the current approach, I have created one Logger instance per class as it is a static field. If I change to non-static, every object will have its own instance of logger which kinda freaks me out for the classes which serve most common requests in my application. It would be of great help if we get into a chat room and discuss your point of view on this? Please join this chat.stackexchange.com/rooms/77498/miko-nukka-dibyaâ NewUser
May 15 at 6:56
I happened to think over the advantages and disadvantages of moving
logger to ParentClass. In the current approach, I have created one Logger instance per class as it is a static field. If I change to non-static, every object will have its own instance of logger which kinda freaks me out for the classes which serve most common requests in my application. It would be of great help if we get into a chat room and discuss your point of view on this? Please join this chat.stackexchange.com/rooms/77498/miko-nukka-dibyaâ NewUser
May 15 at 6:56
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f194310%2finject-a-list-of-processes-to-execute-using-spring-di%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Does it work as expected so far?
â Mast
May 13 at 13:49
@Mast : Yes, I do not have any bugs so far.
â NewUser
May 13 at 14:00