问题
My goal is to register bean (with a custom bean name) programmatically.
@ComponentScan({ "com.test" })
public class AppConfiguration {
//@Bean("test-bean")
@Bean
public Definition definition() {
return () -> Test.class;
}
}
@Named
@Scope("prototype")
public class Test extends DefinitionActor<?> {
....
}
Here, I use Akka and hence I have to go with @Scope("prototype").
I don't want to hard code the bean name to test-bean for some reason.
Hence, I am using the BeanPostProcessor.
@Component
public class BeanDefinitionRegistryPP implements BeanDefinitionRegistryPostProcessor, EnvironmentAware {
private Environment env;
@Override
public void setEnvironment(final Environment environment) {
this.env = environment;
}
@Override
public void postProcessBeanDefinitionRegistry(final BeanDefinitionRegistry registry) throws BeansException {
}
I am getting AKKA error if I go with this way.
Caused by: akka.actor.ActorInitializationException: You cannot create an instance of [com.test.Test] explicitly using the constructor (new). You have to use one of the 'actorOf' factory methods to create a new actor. See the documentation.
at akka.actor.ActorInitializationException$.apply(Actor.scala:181) ~[akka-actor_2.11-2.4.19.jar:na]
The bean post processor work with non-Akka project. Is there any other way to set the bean name programmatically even I use prototype scope.
来源:https://stackoverflow.com/questions/54356119/register-bean-with-a-custom-bean-name-programmatically