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.