知识共享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">
30
11
-----------------------------------------------------------------------------------------
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">
30
11
person
son
-----------------------------------------------------------------------------------------
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">
30
11
person
son
person
son.age
-----------------------------------------------------------------------------------------
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">
30
11
person
son
person
son.age
--要注意,这里不是targetBeanName了噢!
-->
12
age
-----------------------------------------------------------------------------------------
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"));
}
}