知识共享Spring注入属性值案例雷惊风.docx
- 文档编号:23198648
- 上传时间:2023-05-15
- 格式:DOCX
- 页数:9
- 大小:15.33KB
知识共享Spring注入属性值案例雷惊风.docx
《知识共享Spring注入属性值案例雷惊风.docx》由会员分享,可在线阅读,更多相关《知识共享Spring注入属性值案例雷惊风.docx(9页珍藏版)》请在冰豆网上搜索。
知识共享Spring注入属性值案例雷惊风
Spring注入属性值
注入属性值
通过PropertyPathFactoryBean类,可以注入某个实例的属性值。
PropertyPathFactoryBean用来获取目标bean的属性值。
获得的值可注入给其他bean,也可以直接定义成新的bean.
看例子:
-----------------------------------------------------------------------------------------
packageorg.viking.spring.imp;
publicclassPerson
{
privateintage;
privateSonson;
publicvoidsetAge(intage){
this.age=age;
}
publicvoidsetSon(Sonson){
this.son=son;
}
publicintgetAge(){
returnage;
}
publicSongetSon(){
returnson;
}
}
-----------------------------------------------------------------------------------------
packageorg.viking.spring.imp;
publicclassSon
{
privateintage;
publicvoidsetAge(intage){
this.age=age;
}
publicintgetAge(){
returnage;
}
}
-----------------------------------------------------------------------------------------
xmlversion="1.0"encoding="UTF-8"?
>
xmlns="http: //www.springframework.org/schema/beans" xmlns: xsi="http: //www.w3.org/2001/XMLSchema-instance" xsi: schemaLocation="http: //www.springframework.org/schema/beanshttp: //www.springframework.org/schema/beans/spring-beans-2.0.xsd">
-----------------------------------------------------------------------------------------
packageorg.viking.spring.test;
importorg.springframework.context.ApplicationContext;
importorg.springframework.context.support.ClassPathXmlApplicationContext;
importorg.viking.spring.imp.Son;
importjunit.framework.TestCase;
publicclasstestInjectextendsTestCase
{
publicvoidtestInjectAttribute()
{
ApplicationContextapp=newClassPathXmlApplicationContext("applicationContext.xml");
System.out.println("son2的age属性值:
"+((Son)app.getBean("son2")).getAge());
}
}
-----------------------------------------------------------------------------------------
一个bean实例的属性值,不仅可以注入另一个bean,还可将bean实例的属性值直接定义成bean实例,也是通过PropertyPathFactoryBean完成的。
对上面ApplicationContext.xml文件做下修改!
-----------------------------------------------------------------------------------------
xmlversion="1.0"encoding="UTF-8"?
>
xmlns="http: //www.springframework.org/schema/beans" xmlns: xsi="http: //www.w3.org/2001/XMLSchema-instance" xsi: schemaLocation="http: //www.springframework.org/schema/beanshttp: //www.springframework.org/schema/beans/spring-beans-2.0.xsd">
-----------------------------------------------------------------------------------------
packageorg.viking.spring.test;
importorg.springframework.context.ApplicationContext;
importorg.springframework.context.support.ClassPathXmlApplicationContext;
importorg.viking.spring.imp.Son;
importjunit.framework.TestCase;
publicclasstestInjectextendsTestCase
{
publicvoidtestInjectAttribute()
{
ApplicationContextapp=newClassPathXmlApplicationContext("applicationContext.xml");
System.out.println("son2的age属性值:
"+((Son)app.getBean("son2")).getAge());
System.out.println("系统获取的SON1:
"+app.getBean("son1"));
}
}
-----------------------------------------------------------------------------------------
使用PropertyPathFactoryBean必须指定以下两个属性。
1.targetBeanName:
用于指定目标bean,确定获取哪个bean的属性值。
2.propertyPath:
用于指定属性,确定获取目标bean的哪个属性值,此处的属性可直接使用属性表达式。
可以将基本数据类型的属性值定义成bean实例。
在配置文件中再增加如下代码:
-----------------------------------------------------------------------------------------
xmlversion="1.0"encoding="UTF-8"?
>
xmlns="http: //www.springframework.org/schema/beans" xmlns: xsi="http: //www.w3.org/2001/XMLSchema-instance" xsi: schemaLocation="http: //www.springframework.org/schema/beanshttp: //www.springframework.org/schema/beans/spring-beans-2.0.xsd">
-----------------------------------------------------------------------------------------
packageorg.viking.spring.test;
importorg.springframework.context.ApplicationContext;
importorg.springframework.context.support.ClassPathXmlApplicationContext;
importorg.viking.spring.imp.Son;
importjunit.framework.TestCase;
publicclasstestInjectextendsTestCase
{
publicvoidtestInjectAttribute()
{
ApplicationContextapp=newClassPathXmlApplicationContext("applicationContext.xml");
System.out.println("son2的age属性值:
"+((Son)app.getBean("son2")).getAge());
System.out.println("系统获取的SON1:
"+app.getBean("son1"));
System.out.println("系统获取的theAge的值:
"+app.getBean("theAge"));
}
}
-----------------------------------------------------------------------------------------
目标bean既可以是容器中已有的bean实例,也可以是嵌套bean实例。
-----------------------------------------------------------------------------------------
xmlversion="1.0"encoding="UTF-8"?
>
xmlns="http: //www.springframework.org/schema/beans" xmlns: xsi="http: //www.w3.org/2001/XMLSchema-instance" xsi: schemaLocation="http: //www.springframework.org/schema/beanshttp: //www.springframework.org/schema/beans/spring-beans-2.0.xsd"> --要注意,这里不是targetBeanName了噢! -->
-----------------------------------------------------------------------------------------
packageorg.viking.spring.test;
importorg.springframework.context.ApplicationContext;
importorg.springframework.context.support.ClassPathXmlApplicationContext;
importorg.viking.spring.imp.Son;
importjunit.framework.TestCase;
publicclasstestInjectextendsTestCase
{
publicvoidtestInjectAttribute()
{
ApplicationContextapp=newClassPathXmlApplicationContext("applicationContext.xml");
System.out.println("son2的age属性值:
"+((Son)app.getBean("son2")).getAge());
System.out.println("系统获取的SON1:
"+app.getBean("son1"));
System.out.println("系统获取的theAge的值:
"+app.getBean("theAge"));
System.out.println("系统获取的theAge2的值:
"+app.getBean("theAge2"));
}
}
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 知识 共享 Spring 注入 属性 案例 惊风