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

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

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

问题

@Component("example")
public class Example {

    private Consumer<String> exampleHandler = new Consumer<String>() {
        @Override
        public void accept(String s) {
            System.out.println(s);
        }
    };

    @Bean
    public WorkClass workClass(Example example){
        // The code inside this is not executed
        example.exampleHandler.accept("Hello");
        return new WorkClass();
    }
}

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.

@Bean
public DefaultMQPushConsumer canalSyncConsumer(CanalSyncConsumerConfig canalSyncConsumerConfig) throws MQClientException {
    RPCHook rpcHook = new AclClientRPCHook(new SessionCredentials(mqProperties.getAccessKey(), mqProperties.getSecretKey()));
    DefaultMQPushConsumer consumer = new DefaultMQPushConsumer(null, "GID_MEMBER_CENTER_CANAL_SYNC_GROUP", rpcHook);
    consumer.setNamesrvAddr(mqProperties.getNamesAddr());
    final String subE = StringUtils.join(MONITOR_TABLE_NAMES, "||");
    consumer.subscribe("mq_data_mid_platform_sync", subE);
    consumer.registerMessageListener(canalSyncConsumerConfig.consumerHandler);
    consumer.start();
    return consumer;
}
英文:
@Component(&quot;example&quot;)
public class Example {

    private Consumer&lt;String&gt; exampleHandler = new Consumer&lt;String&gt;() {
        @Override
        public void accept(String s) {
            System.out.println(s);
        }
    };

    @Bean
    public WorkClass workClass(Example example){
        // The code inside this is not executed
        example.exampleHandler.accept(&quot;Hello&quot;);
        return new WorkClass();
    }
}

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.

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

答案1

得分: 1

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

private Consumer<String> exampleHandler = new Consumer<String>() {
    @Override
    public void accept(String s) {
        System.out.println(s);
    }
};

@Bean
public WorkClass workClass(Example example){
    // The code inside this is not executed
    example.exampleHandler.accept("Hello");
    return new WorkClass();
}

}

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

英文:
    @Configuration
    @Component(&quot;example&quot;)
    public class Example {
    
        private Consumer&lt;String&gt; exampleHandler = new Consumer&lt;String&gt;() {
            @Override
            public void accept(String s) {
                System.out.println(s);
            }
        };
    
        @Bean
        public WorkClass workClass(Example example){
            // The code inside this is not executed
            example.exampleHandler.accept(&quot;Hello&quot;);
            return new WorkClass();
        }
    }

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:

确定