1. 程式人生 > >2.spring集合屬性

2.spring集合屬性

curl util -- ctx data print 標簽 java car

1.list

Person.java

public class Person {
    private String name;
    private int age;
    private List<car> cars;
  //setter...
}

applicationContext.xml

<!-- list -->
<bean id="person3" class="com.MrChengs2.Collection.Person">
    <property name="name" value="Mike"></property>
    <property name="
age" value="24"></property> <property name="cars"> <list>
       <!-- 1中的Person類 --> <bean class="com.MrChengs1.HelloSpring.car" p:brand="XinXin" p:corp="AnHui"
           p:maxSpeech="100" p:price="12345"></bean> <ref bean="car
"/> <ref bean="car1"/> </list> </property> </bean>

測試:

           Person p = (Person) ctx.getBean("person3");
           System.out.println(p);
Person [name=Mike, age=24, cars=[car [brand=XinXin, corp=AnHui, price=12345.0,
maxSpeech=100],
car [brand=LeiNuo, corp=shanghai, price=0.0
, maxSpeech=120],
car [brand=AoDI, corp=<shanghai>, price=0.0, maxSpeech=245]]]

2.Map

<Map>:標簽裏面嵌套<entry>,在<entry>中進行設置

PersonMap.java

public class PersonMap {
    private String name;
    private int age;
    private Map<String,car> cars;
  //setter...
}

applicationContext.xml

<bean id="Person4" class="com.MrChengs2.Collection.PersonMap">
    <property name="name" value="Rose"></property>
    <property name="age" value="77"></property>
    <property name="cars">
        <map >
            <entry key="AA" value-ref="car"></entry>
            <entry key="BB" value-ref="car1"></entry>
        </map>
    </property>
</bean>

測試:

          PersonMap p1 = (PersonMap) ctx.getBean("Person4");
          System.out.println(p1);
PersonMap [name=Rose, age=77, cars={AA=car [brand=LeiNuo, corp=shanghai, price=0.0, maxSpeech=120], 
BB=car [brand=AoDI, corp=<shanghai>, price=0.0, maxSpeech=245]}]

3.Properties

DatasourceProperties.java

public class DatasourceProperties {
    private Properties properties;
  //setter...
}

applicationContext.xml

<bean id="properties" class="com.MrChengs2.Collection.DatasourceProperties">
    <property name="properties">
        <props>
            <prop key="user">root</prop>
            <prop key="password">1234</prop>
            <prop key="jdbcUrl">jdbc:mysql://</prop>
            <prop key="driverClass">com.mysql.jdbc.Driver</prop>
        </props>
    </property>
</bean>

測試:

          DataSource dataSource = (DataSource) ctx.getBean("properties");
          System.out.println(dataSource);

4.配置獨立集合的Bean

appliactionContext.xml

<!-- 配置獨立的集合Bean
    此時需要在NameSpace導入util命名空間
    此時引用cars就等於直接引用在這裏面的整體內容
 -->
<util:list id="cars">
    <ref bean="car"/>
    <ref bean="car1" />
</util:list>

<bean id="Persons" class="com.MrChengs2.Collection.Person">
  <property name="name" value="zhangshan"></property>
  <property name="age" value="25"></property>
  <property name="cars" ref="cars"></property>
</bean>

測試:

          Person p2 = (Person) ctx.getBean("Persons");
           System.out.println(p2);
Person [name=zhangshan, age=25, cars=[car [brand=LeiNuo, corp=shanghai, price=0.0, maxSpeech=120],
car [brand=AoDI, corp=<shanghai>, price=0.0, maxSpeech=245]]]

2.spring集合屬性