Dynamically registering beans to Spring Framework container in Java development

Source: Internet
Author: User

The original design was to write the data through RMI to the remote server through an existing library, and now there is a need to support multiple, and a custom remote server IP.

Because the entire interface was originally through the spring configuration file, including the RMI address. At the same time, in order to maintain the convenience, not directly to the dozen jar bag to change, so the original bean definition to copy, dynamic registration of new beans into that spring container.

First you need to get the spring container in the jar package, where you need to convert the ApplicationContext interface into a real implementation: Defaultlistablebeanfactory, This allows you to use the registration and acquisition Beandefinition method.

When you want to get the bean, first determine whether the bean is already in the Beanfactory Containsbean method, and if not, continue with the following steps:

Rootbeandefinition remotedataserverclientdefinition =
(rootbeandefinition) beanfactory.getbeandefinition (base_bean_name); Get the definition of the template bean
Rootbeandefinition remotedataserverclientnewdefinition =
New Rootbeandefinition (remotedataserverclientdefinition); Deep copy
Mutablepropertyvalues Properties =
Remotedataserverclientnewdefinition.getpropertyvalues (); Get the properties of the original bean
Beandefinitionholder Defaultdatasourcefactorydefinitionholder =//Here get the property value
(Beandefinitionholder) Properties.getpropertyvalue ("Servicefactory"). GetValue ();
Omitted below, mainly to modify and reassign the value of the property

Registers a new bean with The Bean Factory, the first parameter is the bean's name, and the second parameter is the Bean's definition
Beanfactory.registerbeandefinition (Udasclientbeanname, remotedataserverclientnewdefinition);
Beanfactory.getbean (Udasclientbeanname); And then you can take the beans in the cooler.

The whole process is very simple, with a note of one place, the relationship between Mutablepropertyvalues and the spring configuration file. A beandefinition Mutablepropertyvalues is a series of property tags in the configuration file that can be used to get value. If the property is a bean, the object that gets the object is Beandefinitionholder, that is, the definition of the reference bean in the attribute, and so on.

For example, spring's configuration file has such a bean definition:

The code is as follows Copy Code
<bean id= "A" class= "X.Y.Z.A" >
<property name= "P1" value= "test"/>
<property name= "P2" >
<bean class= "x.y.z.b"/>
</propery>
</bean>



Then the mutablepropertyvalues obtained through the Getpropertyvalues () method contains two properties that can be passed through the getpropertyvalue of Mutablepropertyvalues ("P1″") Gets the value of test, GetPropertyValue ("P2″") gets the beandefinition that corresponds to this class of x.y.z.b.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.