• Spring笔记(四)--公共属性的配置


    一、公共属性的注入配置:

    四个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)。

  • 相关阅读:
    Android系统设置— android.provider.Settings
    调用Android系统设置项
    一维数组当成二维使用
    WP系统推广难的原因之中的一个之我见
    Button和ImageButton
    Struts2 页面url请求怎样找action
    Python一日一练05----怒刷点击量
    OC中语法糖,最新语法总结
    Mybatis分页插件PageHelper正确的用法(网上有2篇不够科学的文章)
    辛星解读mysql的用户管理
  • 原文地址:https://www.cnblogs.com/zhongzhongdebokeyuan/p/5821360.html
Copyright © 2020-2023  润新知