VOOZH about

URL: https://www.javacodegeeks.com/2015/04/spicy-spring-dynamically-create-your-own-beandefinition.html

⇱ Spicy Spring : Dynamically create your own BeanDefinition - Java Code Geeks


When we a have Spring managed application, we want to let Spring manage all of our beans. Beside the regular way of creating beans with known solutions like Annotated beans, Java Configuration and XML Configuration, there is also a way in which we can create our own BeanDefinition.

With a BeanDefinitionRegistryPostProcessor it is possible to create a specific post processor which can add BeanDefinitions to the BeanDefinitionRegistry.

It differs from the BeanPostProcessor, which only has hooks for Bean Initialization (construction of your POJO), where the BeanDefinitionRegistryPostProcessor has a hook on the BeanDefinitionRegistry. This gives us the ability to define our own BeanDefinition.

First we create a BeanDefinitionRegistryPostProcessor implementation as listed in the example. We implement the required method, and will be able to add our own bean definition to the registry. The defined BeanDefinition will be picked up by the ApplicationContext and the POJO will be constructed. Our result is A Spring managed bean

package com.jdriven;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.stereotype.Component;

@Component
public class LogicServiceRegistryPostProcessor 
 implements BeanDefinitionRegistryPostProcessor {

 @Override
 public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry)
 throws BeansException {

 RootBeanDefinition beanDefinition = 
 new RootBeanDefinition(MyServiceImpl.class); //The service implementation
 serviceDefinition.setTargetType(MyService.class); //The service interface
 serviceDefinition.setRole(BeanDefinition.ROLE_APPLICATION);
 registry.registerBeanDefinition("myBeanName", beanDefinition );
 }
}
Reference: Spicy Spring : Dynamically create your own BeanDefinition from our JCG partner Willem Cheizoo at the JDriven blog.
Do you want to know how to develop your skillset to become a Java Rockstar?
Subscribe to our newsletter to start Rocking right now!
To get you started we give you our best selling eBooks for FREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
I agree to the Terms and Privacy Policy

Thank you!

We will contact you soon.

Tags
Spring
πŸ‘ Photo of Willem Cheizoo
Willem Cheizoo
April 20th, 2015Last Updated: April 17th, 2015
1 88 1 minute read
Subscribe

This site uses Akismet to reduce spam. Learn how your comment data is processed.

1 Comment
Oldest
Newest Most Voted
Riccardo
11 years ago

What’s the advantage over standard Java Configuration initialization ?
I mean instead of defining a bean with something like:

@Bean
public MyService myBeanName() {
return new MyServiceImpl(…);
}

0
Reply
Back to top button
Close
wpDiscuz