学习Spring(二) -- Spring配置文件基本属性详解

经过构造函数初始化对象java

建立一个类后,为类编写带参的构造函数(顺便要编写无参的),在spring配置文件中初始化对象。spring

package cn.net.bysoft.lesson1;

public class Car {

    public Car() {

    }

    public Car(String name, int maxSpeed, String color) {
        this.name = name;
        this.maxSpeed = maxSpeed;
        this.color = color;
    }

    public Car(String name, double price, String color) {
        this.name = name;
        this.price = price;
        this.color = color;
    }

    @Override
    public String toString() {
        return "Car [name=" + name + ", maxSpeed=" + maxSpeed + ", price="
                + price + ", color=" + color + "]";
    }
    
    //getter and setter ...

    private String name;
    private int maxSpeed;
    private double price;
    private String color;
}

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util"
    xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="
http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/util 
http://www.springframework.org/schema/util/spring-util-4.2.xsd">

    <!-- 经过构造函数初始化bean对象 -->
    <bean id="car" class="cn.net.bysoft.lesson1.Car">
        <constructor-arg index="0" value="BMW"></constructor-arg>
        <constructor-arg index="1" value="100" type="int"></constructor-arg>
        <constructor-arg index="2" value="Red"></constructor-arg>
    </bean>

</beans>

package cn.net.bysoft.lesson1;

import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Lesson1Test {

    private ApplicationContext ctx;

    @Before
    public void init() {
        // 建立Spring容器
        ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
    }

    @Test
    public void testInitByConstructor() {
        // 使用对象的构造函数初始化对象
        Car car = (Car) ctx.getBean("car");
        System.out.println(car);
        /**
         * output: Car [name=BMW, maxSpeed=100, price=0.0, color=Red]
         * */
    }

}

    类中有两个带参的构造函数。配置文件中使用constructor-arg为构造函数的参数赋值,能够使用index来声明该arg是构造函数中的第几个参数,在用type来区分参数类型。数据库

字段中带有特殊字符app

<!-- 包含特殊字符的参数值 -->
    <bean id="car2" class="cn.net.bysoft.lesson1.Car">
        <constructor-arg index="0">
            <value><![CDATA[<Audi>]]></value>
        </constructor-arg>
        <constructor-arg index="1" value="500000" type="double"></constructor-arg>
        <constructor-arg index="2" value="Red"></constructor-arg>
    </bean>

@Test
    public void testSpecialCharacter() {
        // 字段中带有特殊字符
        Car car = (Car) ctx.getBean("car2");
        System.out.println(car);
        /**
         * output:Car [name=<Audi>, maxSpeed=0, price=500000.0, color=Red]
         * */
    }

    若是字段中带特殊字符,能够使用<![CDATA[...]]>修饰字符串。less

字段是引用类型ide

    若是一个类中的字段是引用类型,好比有一个Person类,该类有一个字段为Car类型,能够使用ref属性进行初始化,具体代码以下:函数

package cn.net.bysoft.lesson1;

public class Person {

    public Person() {

    }

    @Override
    public String toString() {
        return "Person [name=" + name + ", car=" + car + "]";
    }

    //getter and setter

    private String name;
    private Car car;
}

<!-- 初始化引用类型 -->
    <bean id="person" class="cn.net.bysoft.lesson1.Person">
        <property name="name" value="Jack"></property>
        <!-- 使用ref指向一个bean表明传递引用类型,car2在上面的配置文件中配置好了 -->
        <property name="car" ref="car2"></property>
    </bean>

@Test
    public void testInitRef() {
        Person person = (Person) ctx.getBean("person");
        System.out.println(person.toString());
        /**
         * output:Person [name=Jack, car=Car [name=<Audi>, maxSpeed=0,
         * price=500000.0, color=Red]]
         * 
         * */
    }

    还能够使用内部bean初始化引用类型的字段,内部bean不能够被引用。this

<!-- 初始化引用类型时内部bean -->
    <bean id="person2" class="cn.net.bysoft.lesson1.Person">
        <property name="name" value="Kobe"></property>
        <!-- 内部bean -->
        <property name="car">
            <bean class="cn.net.bysoft.lesson1.Car">
                <constructor-arg index="0">
                    <value><![CDATA[<Ferrari>]]></value>
                </constructor-arg>
                <constructor-arg index="1" value="1000000" type="double"></constructor-arg>
                <constructor-arg index="2" value="Black"></constructor-arg>
            </bean>
        </property>
    </bean>

@Test
    public void testInitRefByInner() {
        Person person = (Person) ctx.getBean("person2");
        System.out.println(person.toString());
        /**
         * output:Person [name=Kobe, car=Car [name=<Ferrari>, maxSpeed=0,
         * price=1000000.0, color=Black]]
         * */
    }

初始化字段为NULLspa

<!-- 属性初始化为空 -->
    <bean id="person3" class="cn.net.bysoft.lesson1.Person">
        <property name="name" value="Mark"></property>
        <!-- 引用null -->
        <property name="car">
            <null />
        </property>
    </bean>

@Test
    public void testInitRefByNull() {
        Person person3 = (Person) ctx.getBean("person3");
        System.out.println(person3.toString());
        /**
         * output:Person [name=Mark, car=null]
         * */
    }

级联设置引用类型的字段.net

在使用ref引用了对象后,能够经过car.price,用对象.字段的方式为引用对象赋值其属性:

<!-- 级联设置引用类型的属性 -->
    <bean id="person4" class="cn.net.bysoft.lesson1.Person">
        <property name="name" value="Mary"></property>
        <!-- 引用null -->
        <property name="car" ref="car"></property>
        <!-- 级联属性设置 -->
        <property name="car.price" value="250000"></property>
    </bean>

@Test
    public void testInitRefByCascade() {
        Person person4 = (Person) ctx.getBean("person4");
        System.out.println(person4.toString());
        /**
         * output:Person [name=Mary, car=Car [name=BMW, maxSpeed=100,
         * price=250000.0, color=Red]]
         * */
    }

初始化集合属性

    有的对象中,存在集合属性,好比List或者Map。例如,有一个Company类中有两个属性,一个是List<Car>一个是Map<Car>。在spring中能够list和map标签进行设置,具体以下:

package cn.net.bysoft.lesson1;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Company {

    public Company() {

    }

    @Override
    public String toString() {
        return "Company [name=" + name + ", cars=" + cars + ", carList="
                + carList + "]";
    }

    // getter and setter

    private String name;
    private List<Car> cars = new ArrayList<Car>();
    private Map<String, Car> carList = new HashMap<String, Car>();
}

<!-- 初始化集合属性 -->
    <bean id="company" class="cn.net.bysoft.lesson1.Company">
        <property name="name" value="Spring"></property>
        <!-- list集合 -->
        <property name="cars">
            <list>
                <ref bean="car" />
                <ref bean="car2" />
            </list>
        </property>
        <!-- map集合 -->
        <property name="carList">
            <map>
                <entry key="FirstCar" value-ref="car"></entry>
                <entry key="SecondCar" value-ref="car2"></entry>
            </map>
        </property>
    </bean>

@Test
    public void testInitListOrMap() {
        Company company = (Company) ctx.getBean("company");
        System.out.println(company.toString());
        /**
         * output:Company [name=Spring, cars=[Car [name=BMW, maxSpeed=100,
         * price=250000.0, color=Red], Car [name=<Audi>, maxSpeed=0,
         * price=500000.0, color=Red]], carList={FirstCar=Car [name=BMW,
         * maxSpeed=100, price=250000.0, color=Red], SecondCar=Car [name=<Audi>,
         * maxSpeed=0, price=500000.0, color=Red]}]
         * */
    }

初始化Properties对象

    spring为咱们提供了java.util.Properties对象的初始化功能,例若有一个DataSource类,存放着数据库的链接信息,能够在spring配置文件中初始化它:

package cn.net.bysoft.lesson1;

import java.util.Properties;

public class DataSource {

    public DataSource() {
        
    }

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

    // getter and setter

    private Properties prop;
}

<!-- 配置properties属性 -->
    <bean id="dataSource" class="cn.net.bysoft.lesson1.DataSource">
        <property name="prop">
            <props>
                <prop key="user">root</prop>
                <prop key="password">root</prop>
            </props>
        </property>
    </bean>

@Test
    public void testInitProperties() {
        DataSource ds = (DataSource) ctx.getBean("dataSource");
        System.out.println(ds.toString());
        /**
         * output:DataSource [prop={user=root, password=root}]
         * */
    }

使用util标签设置公用数据

    上诉的初始化集合都是在bean的内部,若是好多个bean都须要初始化一样的集合就须要定义多边,spring为咱们提供了公用数据的配置,在spring配置文件的顶部,导入util的信息:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:util="http://www.springframework.org/schema/util"
    xsi:schemaLocation="
http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/util 
http://www.springframework.org/schema/util/spring-util-4.2.xsd">

接着配置util:list与util:map的信息:

<!-- 通用数据 -->
    <util:list id="util-cars-list">
        <ref bean="car" />
        <ref bean="car2" />
    </util:list>
    <util:map id="util-cars-map">
        <entry key="FirstCar" value-ref="car"></entry>
        <entry key="SecondCar" value-ref="car2"></entry>
    </util:map>

    <!-- 初始化集合属性 -->
    <bean id="company2" class="cn.net.bysoft.lesson1.Company">
        <property name="name" value="Hibernate"></property>
        <!-- list集合 -->
        <property name="cars" ref="util-cars-list"></property>
        <!-- map集合 -->
        <property name="carList" ref="util-cars-map"></property>
    </bean>

@Test
    public void testInitByUtil() {
        Company company2 = (Company) ctx.getBean("company2");
        System.out.println(company2);
        /**
         * output:Company [name=Hibernate, cars=[Car [name=BMW, maxSpeed=100...
         * */
    }

使用p标签

    导入p标签的xmlns,使用p标签初始化对象的属性:

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

<bean id="xx" class="xxx.Person" p:car-ref="car" p:name="Nick"></bean>
相关文章
相关标签/搜索