英文:
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("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;
}
答案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("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();
}
}
adding the @Configuration should do the trick if your project is setup properly.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论