概述
感谢ITCAST发布的免费视频。
packagejunit.test;
importjava.lang.annotation.ElementType;
importjava.lang.annotation.Retention;
importjava.lang.annotation.RetentionPolicy;
importjava.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD, ElementType.METHOD})
public@interfaceMyResource{
publicString name()default"";
}
packagejunit.test;
importjava.beans.IntrospectionException;
importjava.beans.Introspector;
importjava.beans.PropertyDescriptor;
importjava.lang.reflect.Field;
importjava.lang.reflect.InvocationTargetException;
importjava.lang.reflect.Method;
importjava.net.URL;
importjava.util.ArrayList;
importjava.util.HashMap;
importjava.util.List;
importjava.util.Map;
importorg.apache.commons.beanutils.ConvertUtils;
importorg.dom4j.Document;
importorg.dom4j.Element;
importorg.dom4j.XPath;
importorg.dom4j.io.SAXReader;
publicclassMyClassPathXMLApplicationContext {
privateListbeanDefines=newArrayList();
privateMapsigletons=newHashMap();
publicMyClassPathXMLApplicationContext(String fileName) {
this.readXML(fileName);
this.instanceBeans();
this.annotationInject();
this.injectObject();
}
privatevoidannotationInject() {
//TODOAuto-generated method stub
for(String beanName :sigletons.keySet()) {
Object bean =sigletons.get(beanName);
if(bean !=null) {
try{
PropertyDescriptor[] ps = Introspector.getBeanInfo(bean.getClass()).getPropertyDescriptors();
for(PropertyDescriptor properdesc : ps) {
Method setter = properdesc.getWriteMethod();
if(setter !=null&& setter.isAnnotationPresent(MyResource.class)) {
MyResourceresource = setter.getAnnotation(MyResource.class);
Object value =null;
if(resource.name() !=null&& !resource.name().equals("")) {
String name = resource.name();
value =sigletons.get(name);
}else{
value =sigletons.get(properdesc.getName());
if(value ==null) {
for(String key :sigletons.keySet()) {
if(properdesc.getPropertyType().isAssignableFrom(sigletons.get(key).getClass())) {
value =sigletons.get(key);
break;
}
}
}
}
setter.setAccessible(true);
setter.invoke(bean, value);
}
}
Field[] fields = bean.getClass().getDeclaredFields();
for(Field field : fields) {
if(field.isAnnotationPresent(MyResource.class)) {
MyResourceresource = field.getAnnotation(MyResource.class);
Object value =null;
if(resource.name() !=null&& !resource.name().equals("")) {
String name = resource.name();
value =sigletons.get(name);
}else{
value =sigletons.get(field.getName());
if(value ==null) {
for(String key :sigletons.keySet()) {
if(field.getType().isAssignableFrom(sigletons.get(key).getClass())) {
value =sigletons.get(key);
break;
}
}
}
}
field.setAccessible(true);
field.set(bean, value);
}
}
}catch(IntrospectionException e) {
//TODOAuto-generated catch block
e.printStackTrace();
}catch(IllegalArgumentException e) {
//TODOAuto-generated catch block
e.printStackTrace();
}catch(IllegalAccessException e) {
//TODOAuto-generated catch block
e.printStackTrace();
}catch(InvocationTargetException e) {
//TODOAuto-generated catch block
e.printStackTrace();
}
}
}
}
privatevoidinjectObject() {
//TODOAuto-generated method stub
for(BeanDefinition beanDefinition :beanDefines) {
Object bean =sigletons.get(beanDefinition.getId());
if(bean !=null) {
try{
PropertyDescriptor[] ps = Introspector.getBeanInfo(bean.getClass()).getPropertyDescriptors();
for(PropertyDefinition properties : beanDefinition.getProperties()) {
for(PropertyDescriptor properDesc : ps) {
if(properties.getName().equals(properDesc.getName())) {
Method setter = properDesc.getWriteMethod();//get the setter method
if(setter !=null) {
Object value =null;
if(properties.getRef() !=null&& !properties.getRef().equals("")) {
value =sigletons.get(properties.getRef());
}else{
value = ConvertUtils.convert(properties.getValue(), properDesc.getPropertyType());
}
setter.setAccessible(true);
setter.invoke(bean, value);//set ref object to the property
}
break;
}
}
}
}catch(Exception e) {
//TODOAuto-generated catch block
e.printStackTrace();
}
}
}
//List properties =
}
privatevoidinstanceBeans() {
//TODOAuto-generated method stub
for(BeanDefinition beanDefinition :beanDefines) {
try{
if(beanDefinition.getClassName() !=null&& !beanDefinition.getClassName().equals("")) {
sigletons.put(beanDefinition.getId(), Class.forName(beanDefinition.getClassName()).newInstance());
}
}catch(Exception e) {
e.printStackTrace();
}
}
}
privatevoidreadXML(String fileName) {
//TODOAuto-generated method stub
SAXReader saxReader =newSAXReader();
Document document =null;
try{
URL xmlpath =this.getClass().getClassLoader().getResource(fileName);
Map nsMap =newHashMap();
XPath xsub =null;
List beans =null;
document = saxReader.read(xmlpath);
nsMap.put("ns","http://www.springframework.org/schema/beans");//add namespace
xsub = document.createXPath("//ns:beans/ns:bean");//create beans/bean query path
xsub.setNamespaceURIs(nsMap);//set namespace
beans = xsub.selectNodes(document);//get nodes of the document
for(Element element : beans) {
String id = element.attributeValue("id");
String clazz = element.attributeValue("class");
BeanDefinition beanDefine =newBeanDefinition(id, clazz);
XPath propertySub = element.createXPath("ns:property");
List properties =null;
propertySub.setNamespaceURIs(nsMap);
properties = propertySub.selectNodes(element);
for(Element property : properties) {
String propertyName = property.attributeValue("name");
String propertyRef = property.attributeValue("ref");
String propertyValue = property.attributeValue("value");
PropertyDefinition pro =newPropertyDefinition();
pro.setName(propertyName);
pro.setRef(propertyRef);
pro.setValue(propertyValue);
beanDefine.getProperties().add(pro);
}
beanDefines.add(beanDefine);
}
}catch(Exception e) {
e.printStackTrace();
}
}
publicObject getBean(String beanName) {
returnthis.sigletons.get(beanName);
}
}
最后
以上就是大气早晨为你收集整理的java中itcast_ITCAST视频-Spring学习笔记(编码剖析@Resource注解的实现原理)的全部内容,希望文章能够帮你解决java中itcast_ITCAST视频-Spring学习笔记(编码剖析@Resource注解的实现原理)所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复