Spring XML设置bean的构造参数和属性方法

XML设置bean的构造参数和属性方法

构造参数
  • 最简单的,不用指定contructor parameter的index和type
    Bean:

    package x.y;
    
    public class Foo {
    
    public Foo(Bar bar, Baz baz) {
        // ...
    }
    }

    XML:

    <beans>
    <bean name="foo" class="x.y.Foo">
        <constructor-arg>
            <bean class="x.y.Bar"/>
        </constructor-arg>
        <constructor-arg>
            <bean class="x.y.Baz"/>
        </constructor-arg>
    </bean>
    </beans>

    这种适合Contructor的参数type不相同,并且bean没有继承的关系。默认的匹配方法是by type.如果是简单类型的话就不行了,因为<value>xx</value>这样的形式,spring不能知道是具体的哪种类型,可能是int也可能是String.参照下面的例子.

  • Constructor Argument Type Matching
    Bean:

    package examples;
    
    public class ExampleBean {
    
     // No. of years to the calculate the Ultimate Answer
     private int years;
    
     // The Answer to Life, the Universe, and Everything
     private String ultimateAnswer;
    
     public ExampleBean(int years, String ultimateAnswer) {
         this.years = years;
         this.ultimateAnswer = ultimateAnswer;
     }
    } 

    XML:

    <bean id="exampleBean" class="examples.ExampleBean">
    <constructor-arg type="int" value="7500000"/>
    <constructor-arg type="java.lang.String" value="42"/>
    </bean>
  • Constructor Argument Index
    还是使用上面的bean

    <bean id="exampleBean" class="examples.ExampleBean">
    <constructor-arg index="0" value="7500000"/>
    <constructor-arg index="1" value="42"/>
    </bean>

Constructor和Properties的写法:
  • Properties的bean ref写法有两种:
    • 单独使用ref:

      <!-- setter injection using the nested <ref/> element -->
      <property name="beanOne"><ref bean="anotherExampleBean"/> </property>
    • 使用内嵌的ref:

      <property name="beanTwo" ref="yetAnotherBean"/>
  • Constructor的bean ref写法有两种:
    • 单独使用ref:

      <!-- constructor injection using the nested <ref/> element -->
      <constructor-arg>
      <ref bean="anotherExampleBean"/>
      </constructor-arg>
    • 使用内嵌的ref:

      <!-- constructor injection using the neater 'ref' attribute -->
      <constructor-arg ref="yetAnotherBean"/>
其他的设置(包括property和contructor)
  • Straight values (primitives, Strings, etc.)
    • 方式一:

      <property name="driverClassName">
      <value>com.mysql.jdbc.Driver</value>
      </property>
    • 方式二:

      <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
  • Collections
    • List

      <property name="someList">
      <list>
      <value>a list element followed by a reference</value>
      <ref bean="myDataSource" />
      </list>
      </property>
    • Map

      <property name="someMap">
      <map>
      <entry>
      <key>
      <value>an entry</value>
      </key>
      <value>just some string</value>
      </entry>
      <entry>
      <key>
      <value>a ref</value>
      </key>
      <ref bean="myDataSource" />
      </entry>
      </map>
      </property>
    • Set

      <property name="someSet">
      <set>
      <value>just some string</value>
      <ref bean="myDataSource" />
      </set>
      </property>

另外值得一提的还有一种写法:

The p-namespace(从Spring2.0之后开始)

使用这种需要包括一个schemahttp://www.springframework.org/schema/p但是这个特殊的schema不需要schemaLocation,所以可以设置为任何的字段.

直接看一个例子:

<bean name="john-classic" class="com.example.Person">
   <property name="name" value="John Doe"/>
   <property name="spouse" ref="jane"/>
</bean>

<bean name="john-modern" 
   class="com.example.Person"
   p:name="John Doe"
   p:spouse-ref="jane"/>

<bean name="jane" class="com.example.Person">
    <property name="name" value="Jane Doe"/>
</bean>

根据官方备注,这种写法需要仔细考虑,因为像例子中提到的spouse-ref实际是一个spouse字段,如果实际bean中包括一个spouse字段就会产生冲突,而且不容易阅读。所以需要仔细考虑这么写的必要性.

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值