springboot3 native ImportBeanDefinitionRegistrar
does not execute, there is no problem running it on the JVM.
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Import(BeanPostProcessorsRegistrar.class)
public @interface EnableDemo {
}
@Slf4j
public class BeanPostProcessorsRegistrar implements ImportBeanDefinitionRegistrar {
@Override
public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
NacosConf.addr = "192";
}
}
public class NacosConf {
public static String addr;
}
@Slf4j
@Configuration
@EnableDemo
public class DemoConfig {
}
Comment From: snicoll
@c409998649 what do you mean by "not invoked"? It should not be invoked at runtime as it is processed by AOT at build-time.
Comment From: c409998649
@c409998649 what do you mean by "not invoked"? It should not be invoked at runtime as it is processed by AOT at build-time.
but I created a Properties bean through environment and then used the following code
public static void registerInfrastructureBean(BeanDefinitionRegistry registry,
String beanName, Class<?> beanClass, Object... constructorArgs) {
// Build a BeanDefinition for NacosServiceFactory class
BeanDefinitionBuilder beanDefinitionBuilder = BeanDefinitionBuilder
.rootBeanDefinition(beanClass);
for (Object constructorArg : constructorArgs) {
beanDefinitionBuilder.addConstructorArgValue(constructorArg);
}
// ROLE_INFRASTRUCTURE
beanDefinitionBuilder.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
// Register
registry.registerBeanDefinition(beanName,
beanDefinitionBuilder.getBeanDefinition());
}
it doesn't take effect after AOT compilation.
Comment From: snicoll
but I created a Properties bean through environment and then used the following code
Sorry I don't know what you mean by that. Can you share the code in the form of a small sample we can run? I am afraid partial code snippet in text isn't useful.
Comment From: c409998649
but I created a Properties bean through environment and then used the following code
Sorry I don't know what you mean by that. Can you share the code in the form of a small sample we can run? I am afraid partial code snippet in text isn't useful.
I'm sorry if I wasn't clear before, below is my demo. demo I'm not sure if there's a problem with my code, but the results of running on JVM and AOT are different.
Comment From: snicoll
Thank you for the sample. registerSingleton
is the culprit here, it registers an instance, not a bean definition. When AOT runs, it tracks the bean definitions and does the necessary to reflect their state in the generated code. If you register a singleton you've created programmatically, there is no way for us to replicate that state.
That being said, you're not supposed to do that and that's why you have to cast the registry to give you access to the registerSingleton
method. In an earlier comment you said that you were registering a bean definition, but I don't see that in the sample.
Comment From: c409998649
Thank you for the sample.
registerSingleton
is the culprit here, it registers an instance, not a bean definition. When AOT runs, it tracks the bean definitions and does the necessary to reflect their state in the generated code. If you register a singleton you've created programmatically, there is no way for us to replicate that state.That being said, you're not supposed to do that and that's why you have to cast the registry to give you access to the
registerSingleton
method. In an earlier comment you said that you were registering a bean definition, but I don't see that in the sample.
I understand what you mean. The code started encountering environment variable inconsistencies here, which caused errors in the parameters of the beans later on, making me think that they were not injected into the container. Thank you for your response, I will close it now.