@Spring Boot 中标记为 @Bean 的函数中的代码未被执行。

huangapple go评论81阅读模式
英文:

The code in the @Bean marked function in Spring Boot is not executed

问题

  1. @Component("example")
  2. public class Example {
  3. private Consumer<String> exampleHandler = new Consumer<String>() {
  4. @Override
  5. public void accept(String s) {
  6. System.out.println(s);
  7. }
  8. };
  9. @Bean
  10. public WorkClass workClass(Example example){
  11. // The code inside this is not executed
  12. example.exampleHandler.accept("Hello");
  13. return new WorkClass();
  14. }
  15. }
  16. I don't have any ideas to solve this problem.
  17. As expected, an instance of Example should be created first, and then the workClass function should be executed
  18. ----------------update---------------------------------
  19. The specific code in the workClass function is as follows, which is the code in my real project.
  20. @Bean
  21. public DefaultMQPushConsumer canalSyncConsumer(CanalSyncConsumerConfig canalSyncConsumerConfig) throws MQClientException {
  22. RPCHook rpcHook = new AclClientRPCHook(new SessionCredentials(mqProperties.getAccessKey(), mqProperties.getSecretKey()));
  23. DefaultMQPushConsumer consumer = new DefaultMQPushConsumer(null, "GID_MEMBER_CENTER_CANAL_SYNC_GROUP", rpcHook);
  24. consumer.setNamesrvAddr(mqProperties.getNamesAddr());
  25. final String subE = StringUtils.join(MONITOR_TABLE_NAMES, "||");
  26. consumer.subscribe("mq_data_mid_platform_sync", subE);
  27. consumer.registerMessageListener(canalSyncConsumerConfig.consumerHandler);
  28. consumer.start();
  29. return consumer;
  30. }
英文:
  1. @Component(&quot;example&quot;)
  2. public class Example {
  3. private Consumer&lt;String&gt; exampleHandler = new Consumer&lt;String&gt;() {
  4. @Override
  5. public void accept(String s) {
  6. System.out.println(s);
  7. }
  8. };
  9. @Bean
  10. public WorkClass workClass(Example example){
  11. // The code inside this is not executed
  12. example.exampleHandler.accept(&quot;Hello&quot;);
  13. return new WorkClass();
  14. }
  15. }

I don't have any ideas to solve this problem.

As expected, an instance of Example should be created first, and then the workClass function should be executed

----------------update---------------------------------

The specific code in the workClass function is as follows, which is the code in my real project.

  1. @Bean
  2. public DefaultMQPushConsumer canalSyncConsumer(CanalSyncConsumerConfig canalSyncConsumerConfig) throws MQClientException {
  3. RPCHook rpcHook = new AclClientRPCHook(new SessionCredentials(mqProperties.getAccessKey(), mqProperties.getSecretKey()));
  4. DefaultMQPushConsumer consumer = new DefaultMQPushConsumer(null, &quot;GID_MEMBER_CENTER_CANAL_SYNC_GROUP&quot;, rpcHook);
  5. consumer.setNamesrvAddr(mqProperties.getNamesAddr());
  6. final String subE = StringUtils.join(MONITOR_TABLE_NAMES, &quot;||&quot;);
  7. consumer.subscribe(&quot;mq_data_mid_platform_sync&quot;, subE);
  8. consumer.registerMessageListener(canalSyncConsumerConfig.consumerHandler);
  9. consumer.start();
  10. return consumer;
  11. }

答案1

得分: 1

@Configuration
@Component("example")
public class Example {

  1. private Consumer<String> exampleHandler = new Consumer<String>() {
  2. @Override
  3. public void accept(String s) {
  4. System.out.println(s);
  5. }
  6. };
  7. @Bean
  8. public WorkClass workClass(Example example){
  9. // The code inside this is not executed
  10. example.exampleHandler.accept("Hello");
  11. return new WorkClass();
  12. }

}

添加 @Configuration 应该可以解决问题,如果你的项目设置正确的话。

英文:
  1. @Configuration
  2. @Component(&quot;example&quot;)
  3. public class Example {
  4. private Consumer&lt;String&gt; exampleHandler = new Consumer&lt;String&gt;() {
  5. @Override
  6. public void accept(String s) {
  7. System.out.println(s);
  8. }
  9. };
  10. @Bean
  11. public WorkClass workClass(Example example){
  12. // The code inside this is not executed
  13. example.exampleHandler.accept(&quot;Hello&quot;);
  14. return new WorkClass();
  15. }
  16. }

adding the @Configuration should do the trick if your project is setup properly.

huangapple
  • 本文由 发表于 2023年3月1日 11:40:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/75599372.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定