Spring之配置依赖

一、配置依赖
1 .value元素
<? xml version = " 1.0 "  encoding = " gb2312 " ?>
<! DOCTYPE beans PUBLIC  " -//SPRING//DTD BEAN//EN "
    
" http://www.springframework.org/dtd/spring-beans.dtd " >
< beans >
    
< bean id = " exampleBean "   class = " prolove.ExampleBean " >
        
< property name = " integerProperty " >
            
< value > 1 </ value >
        
</ property >
        
< property name = " doubleProperty " >
            
< value > 2.3 </ value >
        
</ property >
    
</ bean >
</ beans >

public   class  ExampleBean
{
    
private int integerProperty;
    
private double doubleProperty;
    
public void setIntegerProperty(int i) {
        
this.integerProperty = i;
    }

    
public void setDoubleProperty(double d) {
        
this.doubleProperty = d;
    }

}


< bean  class = " ExampleBean " >
    
< property name = " email " >< null /></ property >
</ bean >
< bean  class = " ExampleBean " >
    
< property name = " email " >< value ></ value ></ property >
</ bean >

2 .ref元素
< bean id = " steelAxe "   class = " prolove.SteelAxe " />
< bean id = " chinese "   class = " prolove.Chinese " >
    
< property name = " axe " >
        
< ref local = " steelAxe " />
    
</ property >
</ bean >
< bean id = " steelAxe "   class = " prolove.SteelAxe " />
< bean id = " chinese "   class = " prolove.Chinese " >
    
< property name = " axe " >
        
< value > steelAxe </ value >
    
</ property >
</ bean >

3 .bean元素(嵌套bean)
< bean id = " chinese "   class = " prolove.Chinese " >
    
< property name = " axe " >
        
< bean  class = " prolove.SteelAxe " />
    
</ property >
</ bean >

4 .list,set,map以及props元素
public   class  Chinese  implements  Person
{
    
private List schools = new ArrayList();
    
private Map score = new HashMap();
    
private Properties health = new Properties();
    
private Set axes = new HashSet();
    
public Chinese() {
        System.out.println(
"Spring实例化主调bean:Chinese实例");
    }

    
public void setSchools(list l) {
        
this.schools = l;
    }

    
public void setScore(Map m) {
        
this.score = m;
    }

    
public void setHealth(Properties p) {
        
this.health = p;
    }

    
public void setAxes(Set s) {
        
this.axes = s;;
    }

    
public void test() {
        System.out.println(schools);
        System.out.println(score);
        System.out.println(health);
        System.out.println(axes);
    }

}


<? xml version = " 1.0 "  encoding = " gb2312 " ?>
<! DOCTYPE beans PUBLIC  " -//SPRING//DTD BEAN//EN "
    
" http://www.springframework.org/dtd/spring-beans.dtd " >
< beans >
    
< bean id = " stoneAxe "   class = " prolove.SteelAxe " />
    
< bean id = " chinese "   class = " prolove.Chinese " >
        
< property name = " schools " >
            
< list >
                
< value > 小学 </ value >
                
< value > 中学 </ value >
                
< value > 大学 </ value >
            
</ list >
        
</ property >
        
< property name = " score " >
            
< map >
                
< entry key = " 数学 " >
                    
< value > 99 </ value >
                
</ entry >
                
< entry key = " 英语 " >
                    
< value > 100 </ value >
                
</ entry >
                
< entry key = " 语文 " >
                    
< value > 82 </ value >
                
</ entry >
            
</ map >
        
</ property >
        
< property name = " health " >
            
< props >
                
< prop key = " 血压 " > 正常 </ prop >
                
< prop key = " 身高 " > 182 </ prop >
            
</ props >
        
</ property >
        
< property name = " axes " >
            
< set >
                
< value > 字符串斧子 </ value >
                
< bean  class = " prolove.SteelAxe " />
                
< ref local = " stoneAxe " />
            
</ set >
        
</ property >
    
</ bean >
</ beans >

二、注入属性值
<? xml version = " 1.0 "  encoding = " gb2312 " ?>
<! DOCTYPE beans PUBLIC  " -//SPRING//DTD BEAN//EN "
    
" http://www.springframework.org/dtd/spring-beans.dtd " >
< beans >
    
< bean id = " person "   class = " prolove.Person "  singleton = " false " >
        
< property name = " age " >< value > 30 </ value ></ property >
            
< property name = " son " >
                
< bean  class = " prolove.Son " >
                
< property name = " age " >< value > 11 </ value ></ property >
                
</ bean >
            
</ property >
    
</ bean >
    
< bean id = " son2 "   class = " prolove.Son " >
        
< property name = " age " >
            
< bean id = " person.son.age "
                
class = " org.springframework.beans.factory.config.
                    PropertyPathFactoryBean " />
         </ property >
    
</ bean >
</ beans >

public   class  SpringTest
{
    
public static void main(String[] args) {
        ApplicatonContext ctx 
=
            
new FileSystemXmlApplicationContext("bean.xml");
        System.out.println(
"son2的age属性值:" + 
            ((Son)ctx.getBean(
"son2")).getAge());
    }

}

执行结果如下:
[java]系统获取son2的age属性值:
11
直接将bean实例的属性值定义成bean实例
< bean id = " son1 "   class = " org.springframework.beans.factory.config.
    PropertyPathFactoryBean " >
     < property name = " targetBeanName " >
        
< value > person </ value >
    
</ property >
    
< property name = " propertyPath " >
        
< value > son </ value >
    
</ property >
</ bean >

System.out.println(
" 系统获取的son1: "   +  ctx.getBean( " son1 " ));
执行结果如下:
[java]系统获取的son1:prolove.Son@25d2b2

< bean id = " theAge "   class = " org.springframework.beans.factory.config.
    PropertyPathFactory " >
     < property name = " targetBeanName " >
        
< value > person </ value >
    
</ property >
    
< property name = " propertyPath " >
        
< value > son.age </ value >
    
</ property >
</ bean >

System.out.println(
" 系统获取theAge的值: "   +  ctx.getBean( " theAge " ));
执行结果如下:
[java]系统获取theAge的值:
11

< bean id = " theAge2 "   class = " org.springframework.beans.factory.config.
    PropertyPathFactoryBean " >
     < property name = " targetObject " >
        
< bean  class = " prolove.Person " >
            
< property name = " age " >< value > 12 </ value ></ property >
        
</ bean >
    
</ property >
    
< property name = " propertyPath " >< value > age </ value ></ property >
</ bean >

三、注入field值
<? xml version = " 1.0 "  encoding = " gb2312 " ?>
<! DOCTYPE beans PUBLIC  " -//SPRING//DTD BEAN//EN "
    
" http://www.springframework.org/dtd/spring-beans.dtd " >
< beans >
    
< bean id = " son3 "   class = " prolove.Person " >
        
< property name = " age " >
            
< bean id = " java.sql.Connection.TRANSACTION_SERIALIZABLE "  
                
class = " org.springframework.beans.factory.config.
                FieldRetrievingFactoryBean " />
         </ property >
    
</ bean >
</ beans >

public   class  SpringTest
{
    
public static void main(String[] args) {
        ApplicationContext ctx 
= 
            
new FileSystemXmlApplicationContext("bean.xml");
        Son son3 
= (Son)ctx.getBean("son3");
        System.out.println(
"系统获取son3的age属性值:" + son3.getAge());
    }

}

执行结果如下:
[java]系统获取son3的age属性值:
8

< bean id = " theAge3 "   class = " org.springframework.beans.factory.config.
    FieldRetrievingFactoryBean " >
     < property name = " staticField " >
        
< value > java.sql.Connection.TRANSACTION_SERIALIZABLE </ value >
    
</ property >
</ bean >

主程序部分增加如下输出:
System.out.println(
" 系统获取theAge3的值: "   +  ctx.getBean( " theAge3 " ));
输出结果如下:
[java]系统获取theAge3的值:
8
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值