【笔记】Spring4框架系列 [ 4 ] 之 Bean 的细枝末节(三)

基于xml注入包含setter、构造、命名空间、集合属性、byName、byType、SPEL、匿名Bean、同类抽象、异类抽象、配置多子配置文件。

【Student】

package com.athl;

public class Student {
    private String name;
    private int age;
    private School school;
    public Student() {
    }
    public Student(String name, int age, School school) {
        super();
        this.name = name;
        this.age = age;
        this.school = school;
    }
    public void setName(String name) {
        this.name = name;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public void setSchool(School school) {
        this.school = school;
    }
    @Override
    public String toString() {
        return "Student [name=" + name + ", age=" + age + ", school=" + school
                + "]";
    }

}

【School 】

package com.athl;

public class School {
    private String name;
    private String address;

    public School() {
    }
    public School(String name, String address) {
        super();
        this.name = name;
        this.address = address;
    }
    public void setName(String name) {
        this.name = name;
    }
    public void setAddress(String address) {
        this.address = address;
    }
    @Override
    public String toString() {
        return "School [name=" + name + ", address=" + address + "]";
    }

}

【Collect】

package com.athl;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

public class Collect {
    private School[] schools;
    private String[] arrs;
    private List<String> list;
    private Set<String> set;
    private Map<String,Object> map;
    private Properties pro;
    public Collect() {
    }
    public Collect(School[] schools, String[] arrs, List<String> list,
            Set<String> set, Map<String, Object> map ,Properties pro) {
        this.schools = schools;
        this.arrs = arrs;
        this.list = list;
        this.set = set;
        this.map = map;
        this.pro = pro;
    }
    public void setSchools(School[] schools) {
        this.schools = schools;
    }
    public void setArrs(String[] arrs) {
        this.arrs = arrs;
    }
    public void setList(List<String> list) {
        this.list = list;
    }
    public void setSet(Set<String> set) {
        this.set = set;
    }
    public void setMap(Map<String, Object> map) {
        this.map = map;
    }
    public void setPro(Properties pro) {
        this.pro = pro;
    }
    @Override
    public String toString() {
        return "Collect [schools=" + Arrays.toString(schools) + ", arrs="
                + Arrays.toString(arrs) + ", list=" + list + ", set=" + set
                + ", map=" + map + ", pro=" + pro + "]";
    }


}

【Person】

package com.athl;

public class Person {
    private String name;
    private int age;

    public Person() {
    }
    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }
    public void setName(String name) {
        this.name = name;
    }
    public void setAge(int age) {
        this.age = age;
    }

    public String getName() {
        return name;
    }
    public int getAge() {
        return age;
    }
    @Override
    public String toString() {
        return "Person [name=" + name + ", age=" + age + "]";
    }



}

【Worker】

package com.athl;

public class Worker {
    private String name;
    private int age;
    public Worker() {
    }

    public Worker(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public void setName(String name) {
        this.name = name;
    }
    public void setAge(int age) {
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public int getAge() {
        return age;
    }

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


}

【applicationContext.xml】

<?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:p="http://www.springframework.org/schema/p"
    xmlns:c="http://www.springframework.org/schema/c"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans.xsd">

<!-- 配置多个子配置文件(包含关系) -->
<!-- 
    <import resource="spring-*"/>
    注意:总配置文件名不要以"spring-"作为前缀。
 -->

<!-- constructor -->
    <bean id="school" class="com.athl.School">
        <!-- 
        <constructor-arg index="0" value="哈佛" />
        <constructor-arg index="1" value="美国" /> 
        -->
        <constructor-arg name="name" value="哈佛" />
        <constructor-arg name="address" value="美国" />
    </bean> 

<!-- setter -->
    <!-- 
        byName方式自动注入:
            将域属性名作为id到容器中查找相同名称的Bean进行自动注入  
        byType方式自动注入:
            根据域属性类型到容器中查找继承关系(is-a)的Bean进行自动注入
    -->
    <bean id="student" class="com.athl.Student" autowire="byName">
        <property name="name" value="张三"/>
        <property name="age" value="24"/>
        <!-- <property name="school" ref="school"/> -->
    </bean>
    <bean id="student2" class="com.athl.Student">
        <property name="name" value="张三"/>
        <property name="age" value="24"/>
        <property name="school">
        <!-- 匿名内部Bean (无法用getBean()获得直接获得)-->
            <bean class="com.athl.School">
                <constructor-arg index="0" value="哈佛2" />
                <constructor-arg index="1" value="美国2" /> 
            </bean> 
        </property>
    </bean>

<!-- 命名空间(property) -->
    <bean id="pschool" class="com.athl.School" p:name="清华"  p:address="中国" />
    <bean id="pstudent" class="com.athl.Student" p:name="李四" p:age="22" p:school-ref="pschool" />

<!-- 命名空间(constructor) -->
    <bean id="cschool" class="com.athl.School" c:name="清华"  c:address="中国" />
    <bean id="cstudent" class="com.athl.Student" c:name="李四" c:age="22" c:school-ref="cschool" />
    <!-- 注意:构造方法有参无参都要写 -->

<!-- 集合设值注入 -->
    <bean id="collect" class="com.athl.Collect" >
        <property name="schools">
            <array>
                <ref bean="school"/>
                <ref bean="pschool"/>
            </array>
        </property>
        <property name="arrs" value="张三,李四" />
        <!-- 
        <property name="arrs">
            <array>
                <value>张三</value>
                <value>李四</value>
            </array>
        </property> 
        -->
        <property name="list" value="张三,李四" />
        <!-- 
        <property name="list">
            <list>
                <value>张三</value>
                <value>李四</value>
            </list>
        </property> 
        -->
        <property name="set" value="张三,李四" />
        <!-- 
        <property name="set">
            <set>
                <value>张三</value>
                <value>李四</value>
            </set>
        </property> 
        -->
        <property name="map">
            <map>
                <entry key="001" value="张三" />
                <entry key="002" value="李四" />
            </map>
        </property>
        <property name="pro">
            <props>
                <prop key="001">张三</prop>
                <prop key="002">李四</prop>
            </props>
        </property>
    </bean>

<!-- SPEL表达式注入 -->
    <bean id="person" class="com.athl.Person">
        <property name="name" value="nike" />
        <property name="age" value="#{T(java.lang.Math).random()*50 }" />
    </bean>
    <!-- worker与person值保持一致 -->
    <bean id="worker" class="com.athl.Worker">
        <property name="name" value="#{person.name.toUpperCase() }" />
        <property name="age" value="#{person.age }" />
    </bean>
<!-- 同类抽象 -->
    <bean id="baseWorker" class="com.athl.Worker">
        <property name="name" value="码农"/>
    </bean>
    <bean id="worker1" parent="baseWorker">
        <property name="age" value="12" />
    </bean>
    <bean id="worker2" parent="baseWorker">
        <property name="age" value="34" />
    </bean>
<!-- 异类抽象 -->
    <bean id="base" abstract="true">
        <property name="name" value="小明" />
    </bean>
    <bean id="worker3" class="com.athl.Worker" parent="base">
        <property name="age" value="45" />
    </bean>
    <bean id="student3" class="com.athl.Student" autowire="byName" parent="base">
        <property name="age" value="24"/>
    </bean>
</beans>

【Test】

package com.athl.test;

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

import com.athl.Collect;
import com.athl.Person;
import com.athl.Student;
import com.athl.Worker;


public class Mytest {

    @Test
    public void test(){

        /*加载Spring配置文件,创建Spring容器对象,找的是src根目录下配置文件*/
        ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
        /*配置多子配置文件(平等关系)*/
        /*new ClassPathXmlApplicationContext("spring-*.xml");*/
        /*new ClassPathXmlApplicationContext("spring1.xml","spring2.xml");*/

        /*数组方式*/
        /*
         String[] spring={"spring1.xml","spring2.xml"};
         new ClassPathXmlApplicationContext(spring);
        */
        /*从容器中获取指定Bean对象*/
        System.out.println((Student) ac.getBean("student"));
        System.out.println((Student) ac.getBean("student2"));
        System.out.println((Student) ac.getBean("pstudent"));
        System.out.println((Student) ac.getBean("cstudent"));
        System.out.println((Collect) ac.getBean("collect"));
        System.out.println((Person) ac.getBean("person"));
        System.out.println((Worker) ac.getBean("worker"));
        System.out.println((Worker) ac.getBean("worker1"));
        System.out.println((Worker) ac.getBean("worker2"));
        System.out.println((Worker) ac.getBean("worker3"));
        System.out.println((Student) ac.getBean("student3"));

    }
}

源码下载:http://download.csdn.net/detail/jul_11th/9741069

谢谢支持!

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值