Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Spring applicationcontext to registerBeanDefinition

Tags:

java

spring

Java Spring 2.5.x

I Have ApplicationContext and i want to register bean with params

"beanName", beandefination

GenericBeanDefinition myBeanDef = new GenericBeanDefinition();

To register bean i need BeanDefinitionReader but don't know how to get any help appriciated

getRegistry().registerBeanDefinition
like image 200
d-man Avatar asked Feb 24 '26 00:02

d-man


1 Answers

Are you sure you need a bean definition? If you have a ConfigurableApplicationContext you can use the exposed bean factory:

ConfigurableApplicationContext context = ...;
ConfigurableListableBeanFactory beans = context.getBeanFactory();
Object bean = new ...; // create and initialize bean 
beans.registerSingleton(beanName, bean);

Instead of Object use the appropriate bean type and fill in the desired properties before registering. If you want to use autowire you can use

Object bean = beans.createBean(Object.class); 
beans.registerSingleton(beanName, bean);
like image 88
Arne Burmeister Avatar answered Feb 26 '26 14:02

Arne Burmeister



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!