Spring 从xml文件读取BeanDefinition,或者从properties文件读取BeanDefinition,大家或多或少都有了解,今儿就是对spring进行一个扩展来从json里面去读取BeanDefinition。
下面是我用fastjson转的GenericBeanDefinition的json文件,当然你去转的话可能会报错哦,开动你的脑筋,很好解决的
{"abstract":false,
"autowireCandidate":true,
"autowireMode":0,
"beanClassName":"org.spring.learn.service.impl.TestServiceImpl",
"constructorArgumentValues":{
"argumentCount":0,
"empty":true,
"genericArgumentValues":[],
"indexedArgumentValues":{}},
"dependencyCheck":0,
"enforceDestroyMethod":true,
"enforceInitMethod":true,
"lenientConstructorResolution":true,
"methodOverrides":{"empty":true,"overrides":[]},
"nonPublicAccessAllowed":true,
"primary":false,
"propertyValues":{"converted":false,"empty":true,"propertyValueList":[],"propertyValues":[]}
,"prototype":false,
"qualifiers":[],
"resolvableType":{"array":false,"componentType":{"$ref":"@"},
"generics":[],
"interfaces":[],
"source":{"typeName":"org.springframework.core.ResolvableType$EmptyType@5442a311"},
"superType":{"$ref":"@"},
"type":{"$ref":"$.resolvableType.source"}},
"resolvedAutowireMode":0,
"role":0,
"scope":"",
"singleton":true,
"synthetic":false
} {"abstract":false,
"autowireCandidate":true,
"autowireMode":0,
"beanClassName":"org.spring.learn.controller.TestControllerWithProperty",
"constructorArgumentValues":{"argumentCount":0,"empty":true,"genericArgumentValues":[],"indexedArgumentValues":{}},
"dependencyCheck":0,
"enforceDestroyMethod":true,
"enforceInitMethod":true,
"lenientConstructorResolution":true,
"methodOverrides":{"empty":true,"overrides":[]},
"nonPublicAccessAllowed":true,
"primary":false,
"propertyValues":{
"converted":false,
"empty":false,
"propertyValueList":[
{"converted":false,"name":"name","optional":false,"originalPropertyValue":{"$ref":"@"},"value":"property controller"},
{"converted":false,"name":"testService","optional":false,"originalPropertyValue":{"$ref":"@"},"value":{"beanName":"testServiceImpl","toParent":false}},
{"converted":false,"name":"key","optional":false,"originalPropertyValue":{"$ref":"@"},"value":"property"}],
"propertyValues":[{"$ref":"$.propertyValues.propertyValueList[0]"},{"$ref":"$.propertyValues.propertyValueList[1]"},{"$ref":"$.propertyValues.propertyValueList[2]"}]},
"prototype":false,
"qualifiers":[],
"resolvableType":{"array":false,"componentType":{"array":false,"componentType":{"$ref":"@"},"generics":[],"interfaces":[],"source":{"typeName":"org.springframework.core.ResolvableType$EmptyType@5442a311"},"superType":{"$ref":"@"},"type":{"$ref":"$.resolvableType.componentType.source"}},
"generics":[],
"interfaces":[],
"rawClass":"org.spring.learn.controller.TestControllerWithProperty",
"source":"org.spring.learn.controller.TestControllerWithProperty",
"superType":{"array":false,"componentType":{"$ref":"$.resolvableType.componentType"},
"generics":[],
"interfaces":[],
"rawClass":"java.lang.Object",
"source":"java.lang.Object",
"superType":{"$ref":"$.resolvableType.componentType"},
"type":"java.lang.Object"},
"type":"org.spring.learn.controller.TestControllerWithProperty"},
"resolvedAutowireMode":0,
"role":0,
"scope":"",
"singleton":true,
"synthetic":false
} {"abstract":false,"autowireCandidate":true,"autowireMode":0,"beanClassName":"org.spring.learn.controller.TestControllerWithConstructor","constructorArgumentValues":{"argumentCount":3,"empty":false,"genericArgumentValues":[],"indexedArgumentValues":{0:{"converted":false,"value":"constructor controller"},1:{"converted":false,"value":11123},2:{"converted":false,"value":{"beanName":"testServiceImpl","toParent":false}}}},"dependencyCheck":0,"enforceDestroyMethod":true,"enforceInitMethod":true,"lenientConstructorResolution":true,"methodOverrides":{"empty":true,"overrides":[]},"nonPublicAccessAllowed":true,"primary":false,"propertyValues":{"converted":false,"empty":true,"propertyValueList":[],"propertyValues":[]},"prototype":false,"qualifiers":[],"resolvableType":{"array":false,"componentType":{"array":false,"componentType":{"$ref":"@"},"generics":[],"interfaces":[],"source":{"typeName":"org.springframework.core.ResolvableType$EmptyType@5442a311"},"superType":{"$ref":"@"},"type":{"$ref":"$.resolvableType.componentType.source"}},"generics":[],"interfaces":[],"rawClass":"org.spring.learn.controller.TestControllerWithConstructor","source":"org.spring.learn.controller.TestControllerWithConstructor","superType":{"array":false,"componentType":{"$ref":"$.resolvableType.componentType"},"generics":[],"interfaces":[],"rawClass":"java.lang.Object","source":"java.lang.Object","superType":{"$ref":"$.resolvableType.componentType"},"type":"java.lang.Object"},"type":"org.spring.learn.controller.TestControllerWithConstructor"},"resolvedAutowireMode":0,"role":0,"scope":"","singleton":true,"synthetic":false}
那换成xml格式的话
<bean name="testServiceImpl" class="org.spring.learn.service.impl.TestServiceImpl"/> <bean name="testControllerWithConstructor" class="org.spring.learn.controller.TestControllerWithConstructor"> <constructor-arg index="0" value="constructor controller"/> <constructor-arg index="1" value="11123"/> <constructor-arg index="2" ref="testServiceImpl"/> </bean> <bean name="testControllerWithProperty" class="org.spring.learn.controller.TestControllerWithProperty"> <property name="key" value="property"/> <property name="name" value="property controller"/> <property name="testService" ref="testServiceImpl"/> </bean>
上面转json文件,就是把AbstractBeanDefinition的beanClass加上了transient关键字,不让此属性序列化,就行了
这个地方有个注意点,就是当property注入的时候,属性对象set方法注入,值类型,用fastjson是转换不了的
是RuntimeBeanReference类型。
代码一通瞎jb写,最后看看有没有问题
public static void main( String[] args ) { //1,注入进去 JsonContext jsonContext = new JsonContext("beanDefinition.json"); //2,把bean获取出来,看看是不是工作正常的 TestService testService = jsonContext.getBean(TestServiceImpl.class); TestControllerWithProperty testControllerWithProperty = jsonContext.getBean(TestControllerWithProperty.class); System.out.println(testService); System.out.println(testControllerWithProperty.getTestService()); }
org.spring.learn.service.impl.TestServiceImpl@fad74ee
org.spring.learn.service.impl.TestServiceImpl@fad74ee
结果是正常的。ok的,暂时写到这,等会再增加constructor构造函数的逻辑吧