spring 中的bean中的简单映射原理。首先会根据配置文件能够解析出对应的类的Class。比如如下的一段xml的片段
-
<bean id="news" class="model.News">
-
<property name="content" value="12312"></property>
-
</bean>
首先会解析到对应的class属性,然后通过解析xml获取到对应的字符串,此时spring 通过如下代码进行创建类的实体
-
String clName = "model.News";
-
//根据名称得到类的模型
-
-
Class targetClass = Class.forName(clName);
-
//根据类的模型得到具体的实例
-
-
News newx = (News) targetClass.newInstance();
然后继续解析property的name属性,然后按照约定,获得了对应的方法
-
String temStr ="123123";
-
Method method = targetClass.getMethod("setContent",temStr.getClass() );
-
method.invoke(temStr, temStr.getClass());
其中的getMethod的第一个参数是解析name属性的content然后自动加载变成setContent字符串,然后根据方法的名称以及参数的属性去获取对应的方法。然后点用method.invoke就可以对代理对象的对应方法传递值。
阅读(497) | 评论(0) | 转发(0) |