spring学习笔记(3)——bean配置细节注意

1. 一个bean引用另外一个bean

当Person类中有一个属性是Car,那么该如何配置呢

person:

package com.zj.spring;

public class Person {

    private String name;
    private int age;
    private Car car;

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public Car getCar() {
        return car;
    }
    public void setCar(Car car) {
        this.car = car;
    }
    @Override
    public String toString() {
        return "Person [name=" + name + ", age=" + age +
                ", car=" + car + "]";
    }

}

car:
package com.zj.spring;

public class Car {

    private String brand;
    private double price;
    public String getBrand() {
        return brand;
    }
    public void setBrand(String brand) {
        this.brand = brand;
    }
    public double getPrice() {
        return price;
    }
    public void setPrice(double price) {
        this.price = price;
    }
    @Override
    public String toString() {
        return "Car [brand=" + brand + ", price=" + price + "]";
    }

}

使用ref来引用其他bean

    <bean id="car" class="com.zj.spring.Car">
        <property name="brand" value="bmw"></property>
        <property name="price" value="1111111"></property>
    </bean>

    <bean id="person" class="com.zj.spring.Person">
        <property name="name" value="tom"></property>
        <!-- 使用ref来引用其他的bean -->
        <property name="car" ref="car"></property>
    </bean>

2. 当value中有特殊符号

如果value值有 < 这样子的特殊符号,将会出错,因为该符号在xml文件中是节点的起始 
我们可以使用<![CDATA[value]]>来解决    <bean id="car" class="com.zj.spring.Car">

        <property name="brand">
            <value><![CDATA[<bmw>]]></value>
        </property>
        <property name="price" value="1111111"></property>
    </bean>

这里写图片描述

3. 使用内部bean

之前person引入car类,我们也可以使用内部类

    <bean id="person" class="com.zj.spring.Person">
        <property name="name" value="tom"></property>
        <!-- 使用ref来引用其他的bean -->
        <!-- <property name="car" ref="car"></property>  -->

        <property name="car"><!-- 内部bean -->
            <bean id="car" class="com.zj.spring.Car">
                <property name="brand" value="bmw"></property>
                <property name="price" value="1111111"></property>
            </bean>
        </property>
    </bean>

两者有什么区别

内部bean可以不写id,内部bean不能被其他bean使用,有点像匿名类,内部类的感觉

4. null值

如果你想给属性赋null值,应该这样写

<property name="brand"><null/></property>

5. 级联属性配置

    <bean id="person" class="com.zj.spring.Person">
        <property name="name"><null/></property>
        <!-- 使用ref来引用其他的bean -->
        <!-- 如果没有引用,那么使用级联属性时候将有异常 -->
        <property name="car" ref="car"></property>  
        <property name="car.brand" value="bwm"></property>
        <property name="car.price" value="4444444"></property>
    </bean>

6. 集合属性

假设一个person有好几辆Car 
我们改造一下person

package com.zj.collection;

import java.util.List;

public class Person {

    private String name;
    private int age;

    private List<Car> cars;

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public List<Car> getCars() {
        return cars;
    }
    public void setCars(List<Car> cars) {
        this.cars = cars;
    }
    @Override
    public String toString() {
        return "Person [name=" + name + ", age=" + age + ", cars=" + cars + "]";
    }
}

car还是和原来一样。那么该怎么配置呢

    <!-- 集合屬性 -->
    <bean id="car1" class="com.zj.collection.Car">
        <property name="brand" value="bwm"></property>
        <property name="price" value="111111"></property>
    </bean>
        <bean id="car2" class="com.zj.collection.Car">
        <property name="brand" value="auti"></property>
        <property name="price" value="222222"></property>
    </bean>
        <bean id="car3" class="com.zj.collection.Car">
        <property name="brand" value="KIA"></property>
        <property name="price" value="333333"></property>
    </bean>
    <bean id="person1" class="com.zj.collection.Person">
        <property name="name" value="tom"></property>
        <property name="age" value="22"></property>
        <property name="cars">
            <list> <!-- 使用list节点配置集合属性 -->
                <ref bean="car1"/>
                <ref bean="car2"/>
                <ref bean="car3"/>
            </list>
        </property>
    </bean>
  • 同样的,除了使用list节点,也可以使用set节点和map节点
  • map比较特殊一些,因为他是键值对的形式

map可以这样配置

    <map>
        <entry key="bwm" value-ref="car1"></entry>
        <entry key="auti" value-ref="car2"></entry>
        <entry key="KIA" value-ref="car3"></entry>
    </map>

Java.util.Properties类型 
在配置数据库的时候,会需要properties,那么该如何配置这种类型的bean呢 
先写一个DataSource类模拟一下

package com.zj.collection;

import java.util.Properties;

public class DataSource {

    private Properties properties;

    public Properties getProperties() {
        return properties;
    }

    public void setProperties(Properties properties) {
        this.properties = properties;
    }

    @Override
    public String toString() {
        return "DataSource [properties=" + properties + "]";
    }


}

配置

    <bean id="dataSource" class="com.zj.collection.DataSource">

        <property name="properties">
            <props>
                <prop key="user">root</prop>
                <prop key="password">123</prop>
                <prop key="jdbcUrl">jdbc:mysql://xxxx</prop>
                <prop key="driverClass">com.mysql.jdbc.Driver</prop>
            </props>
        </property>
    </bean>

mian方法

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

打印结果 
这里写图片描述

7. 配置独立的集合bean,供多个bean引用

上面的集合属性,都是在bean的内部,不能被其他的bean引用,我们可以使用util:list来配置集合,就可以被多个bean引用

    <!-- 需要 引入util命名空間-->
    <!-- 
    <util:list id="cars">
        <ref bean="car1"/>
        <ref bean="car2"/>
    </util:list>
     -->
    <util:map id="cars">
        <entry key="bwm" value-ref="car1"></entry>
        <entry key="audi" value-ref="car2"></entry>
    </util:map>
    <bean id="person2" class="com.zj.collection.Person">
        <property name="name" value="tom"></property>
        <property name="age" value="22"></property>
        <property name="cars" ref="cars"></property>
    </bean>

还有util:properties等,用法都差不多

  • 注意,需要引入util命名空间 
    xmlns:util="http://www.springframework.org/schema/util"

8. 使用p命名空间

p命名空间可以简化我们配置bean的过程,减少工作量 
先导入p命名空间

xmlns:p="http://www.springframework.org/schema/p"

配置

    <bean id="person3" class="com.zj.collection.Person" p:name="tom" p:age="33" p:cars-ref="cars"></bean
梦想这东西和经典一样,永远不会随时间而褪色,反而更加珍贵!
原文地址:https://www.cnblogs.com/haoxiu1004/p/7298968.html