5.笔记JAVA框架学习——Bean集合属性

5.笔记JAVA框架学习——Bean集合属性

Spring还支持可以使用专用的 <null/> 元素标签为 Bean 的字符串或其它对象类型的属性注入 null 值.

和 Struts、Hiberante 等框架一样,Spring 支持级联属性的配置。

集合属性

l  在 Spring中可以通过一组内置的 xml 标签(例如: <list>, <set> 或 <map>) 来配置集合属性.

l  配置 java.util.List 类型的属性, 需要指定 <list>  标签, 在标签里包含一些元素. 这些标签可以通过 <value> 指定简单的常量值, 通过 <ref> 指定对其他 Bean 的引用. 通过<bean>指定内置 Bean 定义. 通过 <null/> 指定空元素. 甚至可以内嵌其他集合.

l  数组的定义和 List 一样, 都使用<list>

l  配置 java.util.Set 需要使用 <set> 标签, 定义元素的方法与 List 一样.

l  Java.util.Map 通过 <map> 标签定义, <map> 标签里可以使用多个 <entry> 作为子标签. 每个条目包含一个键和一个值.

l  必须在 <key> 标签里定义键

l  因为键和值的类型没有限制, 所以可以自由地为它们指定 <value>, <ref>,<bean> 或 <null> 元素.

l  可以将 Map 的键和值作为 <entry> 的属性定义: 简单常量使用 key 和 value来定义; Bean 引用通过 key-ref 和 value-ref 属性定义

l  使用 <props> 定义 java.util.Properties, 该标签使用多个 <prop> 作为子标签. 每个 <prop> 标签必须定义 key 属性.

示例使用List

在app.xml中增加如下:

                  <!--装配集合属性 -->

                  <beanid="user" class="com.atguigu.spring.helloworld.User">

                                    <propertyname="userName" value="Jack"></property>

                                    <propertyname="cars">

                                                      <!--使用 list 元素来装配集合属性 -->

                                                      <list>

                                                                        <refbean="car"/>

                                                                        <refbean="car2"/>

                                                      </list>

                                    </property>

                  </bean>

然后增加User.java文件如下:

import java.util.List;

 

publicclass User {

 

      private String userName;

      privateList<Car> cars;

     

      private String wifeName;

     

      public StringgetWifeName() {

            returnwifeName;

      }

 

      publicvoidsetWifeName(String wifeName) {

            System.out.println("setWifhName: " + wifeName);

            this.wifeName = wifeName;

      }

 

      public StringgetUserName() {

            returnuserName;

      }

 

      publicvoidsetUserName(String userName) {

            this.userName = userName;

      }

 

      publicList<Car> getCars() {

            returncars;

      }

 

      publicvoidsetCars(List<Car> cars) {

            this.cars = cars;

      }

     

      public User() {

            System.out.println("User's Construtor...");

      }

 

      @Override

      public StringtoString() {

            return"User [userName=" + userName + ",cars=" + cars + "]";

      }

     

      publicvoid init(){

            System.out.println("init method...");

      }

     

      publicvoid destroy(){

            System.out.println("destroy method...");

      }

 

}

在User.java中定义了private List<Car> cars;

在main.java中定义如下:

importorg.springframework.context.ApplicationContext;

importorg.springframework.context.support.ClassPathXmlApplicationContext;

public class Main {                 

                  publicstatic void main(String[] args) {

                                    //1.创建 Spring IOC 容器

                                    ApplicationContextapx = new ClassPathXmlApplicationContext("app.xml");

                                    System.out.println(service);

                                    service.save();*/

                                    Useruser = (User) apx.getBean("user");

                                    System.out.println(user);

                  }                

}

输出如下:

User[userName=Jack, cars=[Car [company=ChangAnFord, brand=KUGA, maxSpeed=0,price=250000.0], Car [company=ChangAnMazda, brand=<ATARZA>, maxSpeed=180,price=0.0]]]

示例使用Map

App.xml增加如下:

      <bean id="car"class="Car">

            <constructor-arg value="KUGA"index="1"></constructor-arg>

            <constructor-arg value="ChangAnFord"index="0"></constructor-arg>

            <constructor-arg value="250000"type="float"></constructor-arg>

      </bean>

 

      <bean id="car2"class="Car">

            <constructor-arg value="ChangAnMazda"></constructor-arg>

            <!-- 若字面值中包含特殊字符, 则可以使用 DCDATA 来进行赋值. (了解) -->

            <constructor-arg>

                  <value><![CDATA[<ATARZA>]]></value>

            </constructor-arg>

            <constructor-arg value="180"type="int"></constructor-arg>

      </bean>

     

            <!-- 装配集合属性-->

      <bean id="user"class="User">

            <property name="userName"value="Jack"></property>

            <property name="cars">

                  <!-- 使用 list 元素来装配集合属性 -->

                  <map>

                        <entry key="AA"value-ref="car"/>

                        <entry key="BB"value-ref="car2"/>

 

                  </map>

            </property>

      </bean>

将user.java修改成如下:

import java.util.List;

import java.util.Map;

 

publicclass User {

 

      private String userName;

      privateMap<String,Car> cars;

     

      private String wifeName;

     

      public StringgetWifeName() {

            returnwifeName;

      }

 

      publicvoidsetWifeName(String wifeName) {

            System.out.println("setWifhName: " + wifeName);

            this.wifeName = wifeName;

      }

 

      public StringgetUserName() {

            returnuserName;

      }

 

      publicvoidsetUserName(String userName) {

            this.userName = userName;

      }

 

      publicMap<String,Car> getCars() {

            returncars;

      }

 

      publicvoidsetCars(Map<String,Car> cars) {

            this.cars = cars;

      }

     

      public User() {

            System.out.println("User's Construtor...");

      }

 

      @Override

      public StringtoString() {

            return"User [userName=" + userName + ",cars=" + cars + "]";

      }

     

      publicvoid init(){

            System.out.println("init method...");

      }

     

      publicvoid destroy(){

            System.out.println("destroy method...");

      }

 

}

将List改成了Map,其他不变。

其中main.java文件不发生变化,执行如下:

User[userName=Jack, cars={AA=Car [company=ChangAnFord, brand=KUGA, maxSpeed=0,price=250000.0], BB=Car [company=ChangAnMazda, brand=<ATARZA>,maxSpeed=180, price=0.0]}]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值