课程咨询 :186 8716 1620      qq:2066486918

昆明Java培训 > 达内新闻 > 公共属性的配置
  • 公共属性的配置

    发布:昆明Java培训      来源:达内新闻      时间:2016-08-31

  • 昆明Java培训班的老师给大家讲公共属性的注入配置:

    四个bean:

    1 package com.dwr.spring.bean;

    2

    3 public class Bean1 {

    4    private Bean2 bean2;

    5    private Bean3 bean3;

    6    private Bean4 bean4;

    7

    8    public Bean2 getBean2() {

    9        return bean2;

    10    }

    11

    12    public void setBean2(Bean2 bean2) {

    13        this.bean2 = bean2;

    14    }

    15

    16    public Bean3 getBean3() {

    17        return bean3;

    18    }

    19

    20    public void setBean3(Bean3 bean3) {

    21        this.bean3 = bean3;

    22    }

    23

    24    public Bean4 getBean4() {

    25        return bean4;

    26    }

    27

    28    public void setBean4(Bean4 bean4) {

    29        this.bean4 = bean4;

    30    }

    31 }

    1 package com.dwr.spring.bean;

    2

    3 public class Bean2 {

    4    private int id;

    5    private String name;

    6    private String password;

    7

    8    public int getId() {

    9        return id;

    10    }

    11

    12    public void setId(int id) {

    13        this.id = id;

    14    }

    15

    16    public String getName() {

    17        return name;

    18    }

    19

    20    public void setName(String name) {

    21        this.name = name;

    22    }

    23

    24    public String getPassword() {

    25        return password;

    26    }

    27

    28    public void setPassword(String password) {

    29        this.password = password;

    30    }

    31 }

    1 package com.dwr.spring.bean;

    2

    3 public class Bean3 {

    4    private int id;

    5    private String name;

    6

    7    public int getId() {

    8        return id;

    9    }

    10

    11    public void setId(int id) {

    12        this.id = id;

    13    }

    14

    15    public String getName() {

    16        return name;

    17    }

    18

    19    public void setName(String name) {

    20        this.name = name;

    21    }

    22 }

    1 package com.dwr.spring.bean;

    2

    3 public class Bean4 {

    4    private int age;

    5

    6    public int getAge() {

    7        return age;

    8    }

    9

    10    public void setAge(int age) {

    11        this.age = age;

    12    }

    13 }

    spring.xml

    1<bean id="bean1" class="com.dwr.spring.bean.Bean1">

    2        <property name="bean2" ref="bean2"/>

    3        <property name="bean3" ref="bean3"/>

    4        <property name="bean4" ref="bean4"/>

    5    </bean>

    6    <bean id="bean2" class="com.dwr.spring.bean.Bean2">

    7        <property name="id" value="1"/>

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

    9        <property name="password" value="123456"/>

    10    </bean>

    11    <bean id="bean3" class="com.dwr.spring.bean.Bean3">

    12        <property name="id" value="1"/>

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

    14    </bean>

    15    <bean id="bean4" class="com.dwr.spring.bean.Bean4">

    16        <property name="age" value="20"/>

    17    </bean>

    测试:

    1 @org.junit.Test

    2    public void testMethod(){

    3        ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");

    4        Bean1 bean1 = (Bean1) context.getBean("bean1");

    5

    6        System.out.println("bean1.getBean2().getId():" + bean1.getBean2().getId());

    7        System.out.println("bean1.getBean2().getName():" + bean1.getBean2().getName());

    8        System.out.println("bean1.getBean2().getPassword():" + bean1.getBean2().getPassword());

    9        System.out.println("bean1.getBean3().getId():" + bean1.getBean3().getId());

    10        System.out.println("bean1.getBean3().getName():" + bean1.getBean3().getName());

    11        System.out.println("bean1.getBean4().getAge():" + bean1.getBean4().getAge());

    12

    13    }

    测试结果:

    1 bean1.getBean2().getId():1

    2 bean1.getBean2().getName():Jack

    3 bean1.getBean2().getPassword():123456

    4 bean1.getBean3().getId():1

    5 bean1.getBean3().getName():Jack

    6 bean1.getBean4().getAge():20

    bean2和bean3的部分属性配置信息相同,因此可以将公共部分提出来。xml文件配置如下:

    1<bean id="beanAbstract" abstract="true">

    2        <property name="id" value="1"/>

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

    4    </bean>

    5    <bean id="bean1" class="com.dwr.spring.bean.Bean1">

    6        <property name="bean2" ref="bean2"/>

    7        <property name="bean3" ref="bean3"/>

    8        <property name="bean4" ref="bean4"/>

    9    </bean>

    10    <bean id="bean2" class="com.dwr.spring.bean.Bean2" parent="beanAbstract">

    11        <property name="password" value="123456"/>

    12    </bean>

    13    <bean id="bean3" class="com.dwr.spring.bean.Bean3" parent="beanAbstract"></bean>

    14    <bean id="bean4" class="com.dwr.spring.bean.Bean4">

    15        <property name="age" value="20"/>

    16    </bean>

    beanAbstract是抽象出来的,令abstract="true"后,可以不指定class属性。

    测试结果与上面相同。

    若bean2的name不同,则可以在配置bean2是声明(配置property)。

    学Java开发就到昆明达内Java培训班!了解详情请登陆昆明达内Java培训官网(km.Java.tedu.cn)!

    推荐文章

上一篇:使用CGLib实现

下一篇:JAVA类与实例

最新开班日期  |  更多

Java--零基础全日制班

Java--零基础全日制班

开班日期:11/30

Java--零基础业余班

Java--零基础业余班

开班日期:11/30

Java--周末提升班

Java--周末提升班

开班日期:11/30

Java--零基础周末班

Java--零基础周末班

开班日期:11/30

  • 网址:http://km .java.tedu.cn      地址:昆明市官渡区春城路62号证券大厦附楼6楼
  • 课程培训电话:186 8716 1620      qq:2066486918    全国服务监督电话:400-827-0010
  • 服务邮箱 ts@tedu.cn
  • 2001-2016 达内国际公司(TARENA INTERNATIONAL,INC.) 版权所有 京ICP证08000853号-56