美文网首页
Spring属性配置细节

Spring属性配置细节

作者: BlueSkyBlue | 来源:发表于2020-03-17 19:56 被阅读0次

    字面值

    可以用字符串表示的值。可以使用<value>元素标签或value属性进行注入。

    基本数据类型及其封装类、String等类型都可以采用字面值注入的方式。

    如果字面值包含特殊字符,可以使用<![CDATA[]]>将其包裹进来。

    引用其它的Bean

    组成应用程序的Bean经常需要相互协作以完成应用程序的功能。要使Bean能够互相访问就必须在Bean配置文件中指定对Bean的引用。

    在Bean配置文件中,可以通过<ref>元素或者ref属性为Bean的属性或构造器参数指定对Bean的引用。

    也可以在属性或构造器里包含Bean是的声明。这样的Bean称为内部Bean。

    示例:
    首先定义一个Person类

    public class Person {
        private String name;
        private int age;
    
        private Car car;
    
        public Person(String name, int age, Car car) {
            this.name = name;
            this.age = age;
            this.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 +
                    '}';
        }
    }
    

    编写配置文件

    <bean id="car" class="com.spring.beans.Car">
        <constructor-arg value="Audi" type="java.lang.String"></constructor-arg>
        <constructor-arg value="Shanghai" type="java.lang.String"></constructor-arg>
        <constructor-arg value="10000" type="double"></constructor-arg>
    </bean>
    
    <bean id="person" class="com.spring.beans.Person">
        <constructor-arg value="person1"></constructor-arg>
        <constructor-arg value="25"></constructor-arg>
        <constructor-arg ref="car"></constructor-arg>
    </bean>
    

    输出注入的类

    public static void main(String[] args) {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        Person person = (Person)applicationContext.getBean("person");
        System.out.println(person);
    }
    

    内部Bean

    除了上述的方式,我们还可以定义内部bean来完成类之间的引用。

    当Bean实例仅仅给一个特定的属性使用时,可以将其声明为内部Bean。内部Bean的声明直接包含在<property>或<constructor-arg>元素内部,不需要设置任何id或者name属性。

    内部bean是不能被外部引用的。

    <bean id="person" class="com.spring.beans.Person">
        <constructor-arg value="person1"></constructor-arg>
        <constructor-arg value="25"></constructor-arg>
        <constructor-arg name="car">
            <bean id="car2" class="com.spring.beans.Car">
                <constructor-arg value="BMW" type="java.lang.String"></constructor-arg>
                <constructor-arg value="Shanghai" type="java.lang.String"></constructor-arg>
                <constructor-arg value="10000" type="double"></constructor-arg>
            </bean>
        </constructor-arg>
    </bean>
    

    为级联属性赋值

    <bean id="person" class="com.spring.beans.Person">
        <constructor-arg value="person1"></constructor-arg>
        <constructor-arg value="25"></constructor-arg>
        <constructor-arg ref="car"></constructor-arg>
        <property name="car.price" value="300000"></property>
    </bean>
    

    需要注意的是属性需要初始化,之后才可以为级联属性赋值,否则将抛出异常。使用级联属性赋值时相应的属性需要有set赋值方法。

    集合属性

    在Spring中可以定义一组xml标签(例如<list>,<set>,<map>)来配置集合属性。

    配置java.util.List类型的属性,需要指定<list>类型的标签。在标签内包含一些元素,这些标签可以
    通过<value>指定属性值。
    通过<ref>指定对其它Bean的引用。
    通过<bean>指定内部Bean的引用。
    通过<null/>指定空元素。甚至可以内嵌其它集合。
    数组的定义和List一样,使用<list>标签。
    配置java.util.Set需要使用<set>标签,定义元素的方法和List一样。
    java.util.Map通过<map>标签定义,map标签里可以使用多个<entry>作为子标签。每个条目包含一个键和一个值。
    必须在<key>标签里定义键。
    因为键和值的类型没有限制。所以可以自由的为它们指定<value>, <ref>, <bean>或<null/>。
    可以将Map的键和值作为<entry>的属性和定义。简单常量使用key和value来定义。Bean引用通过key-ref和value-ref定义。
    使用<props>定义java.util.Properties,该标签使用多个<prop>作为子标签。每个<prop>标签必须定义key属性。

    Map属性的使用示例

    首先创建一个新的Java对象

    public class NewPerson {
        private String name;
        private int age;
        private Map<String, Car> cars;
    
        public void setName(String name) {
            this.name = name;
        }
    
        public void setAge(int age) {
            this.age = age;
        }
    
        public void setCars(Map<String, Car> cars) {
            this.cars = cars;
        }
    
        @Override
        public String toString() {
            return "NewPerson{" +
                    "name='" + name + '\'' +
                    ", age=" + age +
                    ", cars=" + cars +
                    '}';
        }
    }
    

    编写配置文件

    <bean id="newPerson" class="com.spring.beans.NewPerson">
        <property name="name" value="Rose"></property>
        <property name="age" value="28"></property>
        <property name="cars">
            <map>
                <entry key="AA" value-ref="car"></entry>
                <entry key="BB" value-ref="car2"></entry>
            </map>
        </property>
    </bean>
    

    编写运行文件

    public static void main(String[] args) {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        NewPerson newPerson = (NewPerson)applicationContext.getBean("newPerson");
        System.out.println(newPerson );
    }
    

    使用Properties集合

    编写类

    public class DataSource {
        private Properties properties;
    
        public void setProperties(Properties properties) {
            this.properties = properties;
        }
    
        @Override
        public String toString() {
            return "DataSource{" +
                    "properties=" + properties +
                    '}';
        }
    }
    

    编写配置文件

    <bean id="dataSource" class="com.spring.beans.DataSource">
        <property name="properties">
            <props>
                <prop key="user">root</prop>
                <prop key="password">1234</prop>
            </props>
        </property>
    </bean>
    

    编写主函数

    public static void main(String[] args) {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        DataSource dataSource = applicationContext.getBean(DataSource.class);
        System.out.println(dataSource);
     }
    

    使用utility Schema定义集合

    使用基本的集合标签定义集合时,不能将集合作为独立的Bean定义,导致其它Bean无法使用该集合。所以无法在不同的Bean之间共享。

    可以使用util schema里的集合标签定义独立的集合Bean。需要注意的是,必须在<beans>根元素里添加util schema的定义。

    示例

    配置文件

    <util:list id="cars">
        <ref bean="car"/>
        <ref bean="car2"/>
    </util:list>
    
    <bean id="person2" class="com.spring.beans.Person">
        <property name="name" value="Mike"></property>
        <property name="age" value="27"></property>
        <property name="cars"  ref="cars"></property>
    </bean>
    

    使用p命名空间

    Spring从2.5版本开始使用了一种新的p命名空间。可以通过<bean>元素属性的方式配置Bean的属性。

    使用p命名空间后,进一步简化了XML的配置方式

    示例

    <bean 
        id="newPerson" 
        class="com.spring.beans.NewPerson" 
        p:name="Rose" 
        p:age="28">
    </bean>
    

    相关文章

      网友评论

          本文标题:Spring属性配置细节

          本文链接:https://www.haomeiwen.com/subject/ibhxyhtx.html