Create N number of beans with BeanDefinitionRegistryPostProcessor
I'm trying to create N number of beans dynamically using BeanDefinitionRegistryPostProcessor. Based off of this question, I opted to use BeanDefinitionRegistryPostProcessor for my use case.
I have the following defined in my application.yml:
app:
downstream-services:
rest:
jsonPlaceHolder:
url: https://jsonplaceholder.typicode.com/todos
api-type: io.mateo.dynamicbeans.JsonPlaceHolderApi
Which gets wired up to a ConfigiruationProperties class here: https://github.com/ciscoo/dynamicbeans/blob/master/src/main/java/io/mateo/dynamicbeans/FeignConfigurationProperties.java
I then want to inject that ConfigiruationProperties class along with a factory bean that I defined here: https://github.com/ciscoo/dynamicbeans/blob/master/src/main/java/io/mateo/dynamicbeans/FeignClientAutoConfiguration.java
So now I have the following:
https://github.com/ciscoo/dynamicbeans/blob/master/src/main/java/io/mateo/dynamicbeans/FeignClientFactoryPostProcessor.java
@Component
public class FeignClientFactoryPostProcessor implements BeanDefinitionRegistryPostProcessor {
private final FeignConfigurationProperties properties;
private final FeignClientFactory feignClientFactory;
public FeignClientFactoryPostProcessor(FeignConfigurationProperties properties, FeignClientFactory feignClientFactory) {
this.properties = properties;
this.feignClientFactory = feignClientFactory;
}
@Override
public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
properties.getDownstreamServices().getRest().forEach((beanName, props) -> makeClient(beanName, props, registry));
}
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
// no-op
}
private void makeClient(String beanName, FeignClientProperties props, BeanDefinitionRegistry registry) {
GenericBeanDefinition beanDefinition = new GenericBeanDefinition();
beanDefinition.setBeanClass(props.getApiType());
beanDefinition.setInstanceSupplier(() -> feignClientFactory.create(props));
registry.registerBeanDefinition(beanName, beanDefinition);
}
}
The single bean it should create is to injected in a service class here: https://github.com/ciscoo/dynamicbeans/blob/master/src/main/java/io/mateo/dynamicbeans/JsonPlaceHolderService.java
The problem I'm running into is:
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [io.mateo.dynamicbeans.FeignClientFactoryPostProcessor]: No default constructor found; nested exception is java.lang.NoSuchMethodException: io.mateo.dynamicbeans.FeignClientFactoryPostProcessor.<init>()
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:83) ~[spring-beans-5.1.2.RELEASE.jar:5.1.2.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1262) ~[spring-beans-5.1.2.RELEASE.jar:5.1.2.RELEASE]
... 17 common frames omitted
Caused by: java.lang.NoSuchMethodException: io.mateo.dynamicbeans.FeignClientFactoryPostProcessor.<init>()
at java.base/java.lang.Class.getConstructor0(Class.java:3350) ~[na:na]
at java.base/java.lang.Class.getDeclaredConstructor(Class.java:2554) ~[na:na]
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:78) ~[spring-beans-5.1.2.RELEASE.jar:5.1.2.RELEASE]
... 18 common frames omitted
But when I remove the final keyword from the two properties and the defined constructor, I get a NullPointerException.
So how do I dynamically create N number of beans so that they will be available in time for any of my @Service classes to use?
I'm aware of https://spring.io/projects/spring-cloud-openfeign. I recreated my issue here to illustrate the same problem I'm having in a different project with dynamically creating SOAP clients.
Update: Doing the following changes: https://github.com/ciscoo/dynamicbeans/commit/4f16de9d03271025cd65d95932a3e854c0619c29, now I am able to accomplish my use case.
spring spring-boot
add a comment |
I'm trying to create N number of beans dynamically using BeanDefinitionRegistryPostProcessor. Based off of this question, I opted to use BeanDefinitionRegistryPostProcessor for my use case.
I have the following defined in my application.yml:
app:
downstream-services:
rest:
jsonPlaceHolder:
url: https://jsonplaceholder.typicode.com/todos
api-type: io.mateo.dynamicbeans.JsonPlaceHolderApi
Which gets wired up to a ConfigiruationProperties class here: https://github.com/ciscoo/dynamicbeans/blob/master/src/main/java/io/mateo/dynamicbeans/FeignConfigurationProperties.java
I then want to inject that ConfigiruationProperties class along with a factory bean that I defined here: https://github.com/ciscoo/dynamicbeans/blob/master/src/main/java/io/mateo/dynamicbeans/FeignClientAutoConfiguration.java
So now I have the following:
https://github.com/ciscoo/dynamicbeans/blob/master/src/main/java/io/mateo/dynamicbeans/FeignClientFactoryPostProcessor.java
@Component
public class FeignClientFactoryPostProcessor implements BeanDefinitionRegistryPostProcessor {
private final FeignConfigurationProperties properties;
private final FeignClientFactory feignClientFactory;
public FeignClientFactoryPostProcessor(FeignConfigurationProperties properties, FeignClientFactory feignClientFactory) {
this.properties = properties;
this.feignClientFactory = feignClientFactory;
}
@Override
public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
properties.getDownstreamServices().getRest().forEach((beanName, props) -> makeClient(beanName, props, registry));
}
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
// no-op
}
private void makeClient(String beanName, FeignClientProperties props, BeanDefinitionRegistry registry) {
GenericBeanDefinition beanDefinition = new GenericBeanDefinition();
beanDefinition.setBeanClass(props.getApiType());
beanDefinition.setInstanceSupplier(() -> feignClientFactory.create(props));
registry.registerBeanDefinition(beanName, beanDefinition);
}
}
The single bean it should create is to injected in a service class here: https://github.com/ciscoo/dynamicbeans/blob/master/src/main/java/io/mateo/dynamicbeans/JsonPlaceHolderService.java
The problem I'm running into is:
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [io.mateo.dynamicbeans.FeignClientFactoryPostProcessor]: No default constructor found; nested exception is java.lang.NoSuchMethodException: io.mateo.dynamicbeans.FeignClientFactoryPostProcessor.<init>()
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:83) ~[spring-beans-5.1.2.RELEASE.jar:5.1.2.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1262) ~[spring-beans-5.1.2.RELEASE.jar:5.1.2.RELEASE]
... 17 common frames omitted
Caused by: java.lang.NoSuchMethodException: io.mateo.dynamicbeans.FeignClientFactoryPostProcessor.<init>()
at java.base/java.lang.Class.getConstructor0(Class.java:3350) ~[na:na]
at java.base/java.lang.Class.getDeclaredConstructor(Class.java:2554) ~[na:na]
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:78) ~[spring-beans-5.1.2.RELEASE.jar:5.1.2.RELEASE]
... 18 common frames omitted
But when I remove the final keyword from the two properties and the defined constructor, I get a NullPointerException.
So how do I dynamically create N number of beans so that they will be available in time for any of my @Service classes to use?
I'm aware of https://spring.io/projects/spring-cloud-openfeign. I recreated my issue here to illustrate the same problem I'm having in a different project with dynamically creating SOAP clients.
Update: Doing the following changes: https://github.com/ciscoo/dynamicbeans/commit/4f16de9d03271025cd65d95932a3e854c0619c29, now I am able to accomplish my use case.
spring spring-boot
add a comment |
I'm trying to create N number of beans dynamically using BeanDefinitionRegistryPostProcessor. Based off of this question, I opted to use BeanDefinitionRegistryPostProcessor for my use case.
I have the following defined in my application.yml:
app:
downstream-services:
rest:
jsonPlaceHolder:
url: https://jsonplaceholder.typicode.com/todos
api-type: io.mateo.dynamicbeans.JsonPlaceHolderApi
Which gets wired up to a ConfigiruationProperties class here: https://github.com/ciscoo/dynamicbeans/blob/master/src/main/java/io/mateo/dynamicbeans/FeignConfigurationProperties.java
I then want to inject that ConfigiruationProperties class along with a factory bean that I defined here: https://github.com/ciscoo/dynamicbeans/blob/master/src/main/java/io/mateo/dynamicbeans/FeignClientAutoConfiguration.java
So now I have the following:
https://github.com/ciscoo/dynamicbeans/blob/master/src/main/java/io/mateo/dynamicbeans/FeignClientFactoryPostProcessor.java
@Component
public class FeignClientFactoryPostProcessor implements BeanDefinitionRegistryPostProcessor {
private final FeignConfigurationProperties properties;
private final FeignClientFactory feignClientFactory;
public FeignClientFactoryPostProcessor(FeignConfigurationProperties properties, FeignClientFactory feignClientFactory) {
this.properties = properties;
this.feignClientFactory = feignClientFactory;
}
@Override
public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
properties.getDownstreamServices().getRest().forEach((beanName, props) -> makeClient(beanName, props, registry));
}
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
// no-op
}
private void makeClient(String beanName, FeignClientProperties props, BeanDefinitionRegistry registry) {
GenericBeanDefinition beanDefinition = new GenericBeanDefinition();
beanDefinition.setBeanClass(props.getApiType());
beanDefinition.setInstanceSupplier(() -> feignClientFactory.create(props));
registry.registerBeanDefinition(beanName, beanDefinition);
}
}
The single bean it should create is to injected in a service class here: https://github.com/ciscoo/dynamicbeans/blob/master/src/main/java/io/mateo/dynamicbeans/JsonPlaceHolderService.java
The problem I'm running into is:
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [io.mateo.dynamicbeans.FeignClientFactoryPostProcessor]: No default constructor found; nested exception is java.lang.NoSuchMethodException: io.mateo.dynamicbeans.FeignClientFactoryPostProcessor.<init>()
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:83) ~[spring-beans-5.1.2.RELEASE.jar:5.1.2.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1262) ~[spring-beans-5.1.2.RELEASE.jar:5.1.2.RELEASE]
... 17 common frames omitted
Caused by: java.lang.NoSuchMethodException: io.mateo.dynamicbeans.FeignClientFactoryPostProcessor.<init>()
at java.base/java.lang.Class.getConstructor0(Class.java:3350) ~[na:na]
at java.base/java.lang.Class.getDeclaredConstructor(Class.java:2554) ~[na:na]
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:78) ~[spring-beans-5.1.2.RELEASE.jar:5.1.2.RELEASE]
... 18 common frames omitted
But when I remove the final keyword from the two properties and the defined constructor, I get a NullPointerException.
So how do I dynamically create N number of beans so that they will be available in time for any of my @Service classes to use?
I'm aware of https://spring.io/projects/spring-cloud-openfeign. I recreated my issue here to illustrate the same problem I'm having in a different project with dynamically creating SOAP clients.
Update: Doing the following changes: https://github.com/ciscoo/dynamicbeans/commit/4f16de9d03271025cd65d95932a3e854c0619c29, now I am able to accomplish my use case.
spring spring-boot
I'm trying to create N number of beans dynamically using BeanDefinitionRegistryPostProcessor. Based off of this question, I opted to use BeanDefinitionRegistryPostProcessor for my use case.
I have the following defined in my application.yml:
app:
downstream-services:
rest:
jsonPlaceHolder:
url: https://jsonplaceholder.typicode.com/todos
api-type: io.mateo.dynamicbeans.JsonPlaceHolderApi
Which gets wired up to a ConfigiruationProperties class here: https://github.com/ciscoo/dynamicbeans/blob/master/src/main/java/io/mateo/dynamicbeans/FeignConfigurationProperties.java
I then want to inject that ConfigiruationProperties class along with a factory bean that I defined here: https://github.com/ciscoo/dynamicbeans/blob/master/src/main/java/io/mateo/dynamicbeans/FeignClientAutoConfiguration.java
So now I have the following:
https://github.com/ciscoo/dynamicbeans/blob/master/src/main/java/io/mateo/dynamicbeans/FeignClientFactoryPostProcessor.java
@Component
public class FeignClientFactoryPostProcessor implements BeanDefinitionRegistryPostProcessor {
private final FeignConfigurationProperties properties;
private final FeignClientFactory feignClientFactory;
public FeignClientFactoryPostProcessor(FeignConfigurationProperties properties, FeignClientFactory feignClientFactory) {
this.properties = properties;
this.feignClientFactory = feignClientFactory;
}
@Override
public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
properties.getDownstreamServices().getRest().forEach((beanName, props) -> makeClient(beanName, props, registry));
}
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
// no-op
}
private void makeClient(String beanName, FeignClientProperties props, BeanDefinitionRegistry registry) {
GenericBeanDefinition beanDefinition = new GenericBeanDefinition();
beanDefinition.setBeanClass(props.getApiType());
beanDefinition.setInstanceSupplier(() -> feignClientFactory.create(props));
registry.registerBeanDefinition(beanName, beanDefinition);
}
}
The single bean it should create is to injected in a service class here: https://github.com/ciscoo/dynamicbeans/blob/master/src/main/java/io/mateo/dynamicbeans/JsonPlaceHolderService.java
The problem I'm running into is:
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [io.mateo.dynamicbeans.FeignClientFactoryPostProcessor]: No default constructor found; nested exception is java.lang.NoSuchMethodException: io.mateo.dynamicbeans.FeignClientFactoryPostProcessor.<init>()
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:83) ~[spring-beans-5.1.2.RELEASE.jar:5.1.2.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1262) ~[spring-beans-5.1.2.RELEASE.jar:5.1.2.RELEASE]
... 17 common frames omitted
Caused by: java.lang.NoSuchMethodException: io.mateo.dynamicbeans.FeignClientFactoryPostProcessor.<init>()
at java.base/java.lang.Class.getConstructor0(Class.java:3350) ~[na:na]
at java.base/java.lang.Class.getDeclaredConstructor(Class.java:2554) ~[na:na]
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:78) ~[spring-beans-5.1.2.RELEASE.jar:5.1.2.RELEASE]
... 18 common frames omitted
But when I remove the final keyword from the two properties and the defined constructor, I get a NullPointerException.
So how do I dynamically create N number of beans so that they will be available in time for any of my @Service classes to use?
I'm aware of https://spring.io/projects/spring-cloud-openfeign. I recreated my issue here to illustrate the same problem I'm having in a different project with dynamically creating SOAP clients.
Update: Doing the following changes: https://github.com/ciscoo/dynamicbeans/commit/4f16de9d03271025cd65d95932a3e854c0619c29, now I am able to accomplish my use case.
spring spring-boot
spring spring-boot
edited Nov 25 '18 at 16:34
Francisco Mateo
asked Nov 24 '18 at 22:26
Francisco MateoFrancisco Mateo
3,98021329
3,98021329
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
As the answer to the question that you have linked to suggests, you can’t inject dependencies into a bean factory post-processor. Rather than injecting your configuration properties class, you’ll need to bind it programmatically. In Spring Boot 2.x, that is achieved using the Binder API:
The new Binder API can also be used outside of
@ConfigurationPropertiesdirectly in your own code. For example, the following will bind to aListofPersonNameobjects:
List<PersonName> people = Binder.get(environment)
.bind("my.property", Bindable.listOf(PersonName.class))
.orElseThrow(IllegalStateException::new);
The configuration source could be represented in YAML like this:
my:
property:
- first-name: Jane
last-name: Doe
- first-name: John
last-name: Doe
That worked out perfectly, thank you! One side question, is it recommended that theBeanDefinitionRegistryPostProcessorbe marked as@Componentor defined as a@Beanwithin in@Configurationclass?
– Francisco Mateo
Nov 25 '18 at 15:57
1
I would define it as astatic@Beanmethod in a@Configurationclass. Declaring the method asstaticmeans that the bean method can be called without instantiating its class. This will prevent the creation of your post-processor (which needs to happen very early) from causing anything also to be created earlier than wanted.
– Andy Wilkinson
Nov 25 '18 at 17:29
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53462889%2fcreate-n-number-of-beans-with-beandefinitionregistrypostprocessor%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
As the answer to the question that you have linked to suggests, you can’t inject dependencies into a bean factory post-processor. Rather than injecting your configuration properties class, you’ll need to bind it programmatically. In Spring Boot 2.x, that is achieved using the Binder API:
The new Binder API can also be used outside of
@ConfigurationPropertiesdirectly in your own code. For example, the following will bind to aListofPersonNameobjects:
List<PersonName> people = Binder.get(environment)
.bind("my.property", Bindable.listOf(PersonName.class))
.orElseThrow(IllegalStateException::new);
The configuration source could be represented in YAML like this:
my:
property:
- first-name: Jane
last-name: Doe
- first-name: John
last-name: Doe
That worked out perfectly, thank you! One side question, is it recommended that theBeanDefinitionRegistryPostProcessorbe marked as@Componentor defined as a@Beanwithin in@Configurationclass?
– Francisco Mateo
Nov 25 '18 at 15:57
1
I would define it as astatic@Beanmethod in a@Configurationclass. Declaring the method asstaticmeans that the bean method can be called without instantiating its class. This will prevent the creation of your post-processor (which needs to happen very early) from causing anything also to be created earlier than wanted.
– Andy Wilkinson
Nov 25 '18 at 17:29
add a comment |
As the answer to the question that you have linked to suggests, you can’t inject dependencies into a bean factory post-processor. Rather than injecting your configuration properties class, you’ll need to bind it programmatically. In Spring Boot 2.x, that is achieved using the Binder API:
The new Binder API can also be used outside of
@ConfigurationPropertiesdirectly in your own code. For example, the following will bind to aListofPersonNameobjects:
List<PersonName> people = Binder.get(environment)
.bind("my.property", Bindable.listOf(PersonName.class))
.orElseThrow(IllegalStateException::new);
The configuration source could be represented in YAML like this:
my:
property:
- first-name: Jane
last-name: Doe
- first-name: John
last-name: Doe
That worked out perfectly, thank you! One side question, is it recommended that theBeanDefinitionRegistryPostProcessorbe marked as@Componentor defined as a@Beanwithin in@Configurationclass?
– Francisco Mateo
Nov 25 '18 at 15:57
1
I would define it as astatic@Beanmethod in a@Configurationclass. Declaring the method asstaticmeans that the bean method can be called without instantiating its class. This will prevent the creation of your post-processor (which needs to happen very early) from causing anything also to be created earlier than wanted.
– Andy Wilkinson
Nov 25 '18 at 17:29
add a comment |
As the answer to the question that you have linked to suggests, you can’t inject dependencies into a bean factory post-processor. Rather than injecting your configuration properties class, you’ll need to bind it programmatically. In Spring Boot 2.x, that is achieved using the Binder API:
The new Binder API can also be used outside of
@ConfigurationPropertiesdirectly in your own code. For example, the following will bind to aListofPersonNameobjects:
List<PersonName> people = Binder.get(environment)
.bind("my.property", Bindable.listOf(PersonName.class))
.orElseThrow(IllegalStateException::new);
The configuration source could be represented in YAML like this:
my:
property:
- first-name: Jane
last-name: Doe
- first-name: John
last-name: Doe
As the answer to the question that you have linked to suggests, you can’t inject dependencies into a bean factory post-processor. Rather than injecting your configuration properties class, you’ll need to bind it programmatically. In Spring Boot 2.x, that is achieved using the Binder API:
The new Binder API can also be used outside of
@ConfigurationPropertiesdirectly in your own code. For example, the following will bind to aListofPersonNameobjects:
List<PersonName> people = Binder.get(environment)
.bind("my.property", Bindable.listOf(PersonName.class))
.orElseThrow(IllegalStateException::new);
The configuration source could be represented in YAML like this:
my:
property:
- first-name: Jane
last-name: Doe
- first-name: John
last-name: Doe
answered Nov 25 '18 at 8:28
Andy WilkinsonAndy Wilkinson
57.3k9136145
57.3k9136145
That worked out perfectly, thank you! One side question, is it recommended that theBeanDefinitionRegistryPostProcessorbe marked as@Componentor defined as a@Beanwithin in@Configurationclass?
– Francisco Mateo
Nov 25 '18 at 15:57
1
I would define it as astatic@Beanmethod in a@Configurationclass. Declaring the method asstaticmeans that the bean method can be called without instantiating its class. This will prevent the creation of your post-processor (which needs to happen very early) from causing anything also to be created earlier than wanted.
– Andy Wilkinson
Nov 25 '18 at 17:29
add a comment |
That worked out perfectly, thank you! One side question, is it recommended that theBeanDefinitionRegistryPostProcessorbe marked as@Componentor defined as a@Beanwithin in@Configurationclass?
– Francisco Mateo
Nov 25 '18 at 15:57
1
I would define it as astatic@Beanmethod in a@Configurationclass. Declaring the method asstaticmeans that the bean method can be called without instantiating its class. This will prevent the creation of your post-processor (which needs to happen very early) from causing anything also to be created earlier than wanted.
– Andy Wilkinson
Nov 25 '18 at 17:29
That worked out perfectly, thank you! One side question, is it recommended that the
BeanDefinitionRegistryPostProcessor be marked as @Component or defined as a @Bean within in @Configuration class?– Francisco Mateo
Nov 25 '18 at 15:57
That worked out perfectly, thank you! One side question, is it recommended that the
BeanDefinitionRegistryPostProcessor be marked as @Component or defined as a @Bean within in @Configuration class?– Francisco Mateo
Nov 25 '18 at 15:57
1
1
I would define it as a
static @Bean method in a @Configuration class. Declaring the method as static means that the bean method can be called without instantiating its class. This will prevent the creation of your post-processor (which needs to happen very early) from causing anything also to be created earlier than wanted.– Andy Wilkinson
Nov 25 '18 at 17:29
I would define it as a
static @Bean method in a @Configuration class. Declaring the method as static means that the bean method can be called without instantiating its class. This will prevent the creation of your post-processor (which needs to happen very early) from causing anything also to be created earlier than wanted.– Andy Wilkinson
Nov 25 '18 at 17:29
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53462889%2fcreate-n-number-of-beans-with-beandefinitionregistrypostprocessor%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown