Spring注入有继承关系的类(1)

一个类一个类的注入
1.AClass类
package com.bijian.spring.test2;

public class AClass {

    String a;
    String b;
   
    public String getA() {
        return a;
    }
    public void setA(String a) {
        this.a = a;
    }
    public String getB() {
        return b;
    }
    public void setB(String b) {
        this.b = b;
    }
}
 
2.BClass类
package com.bijian.spring.test2;

public class BClass extends AClass {

    String c;

    public String getC() {
        return c;
    }

    public void setC(String c) {
        this.c = c;
    }

    String fn() {
        return a + b + c;
    }
}
 
3.AClass类配置文件aclass2.xml
<?xml version="1.0" encoding="UTF-8"?>
  <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
  "http://www.springframework.org/dtd/spring-beans.dtd">

<beans>
  <bean id="bean1" class="com.bijian.spring.test2.AClass">
      <property name="a">
          <value>1</value>
      </property>
      <property name="b">
          <value>2</value>
      </property>
  </bean>
</beans>
 
4.BClass类配置文件bclass2.xml
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"  
  "http://www.springframework.org/dtd/spring-beans.dtd">  
   
<beans>  
  <bean id="bean2" class="com.bijian.spring.test2.BClass">
      <property name="c">
          <value>3</value>
     </property>
  </bean>
</beans>
 
5.应用测试类Test.java
package com.bijian.spring.test2;

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

public class Test {

public static void main(String[] args) {
       
        ApplicationContext ac = new ClassPathXmlApplicationContext(new String[]{"aclass2.xml","bclass2.xml"});
       
        //得到子类,结果是:nullnull3
        BClass bclass = (BClass)ac.getBean("bean2");
        String res = bclass.fn();
        System.out.println(res);
    }
}
      
        由结果不难看出,子类没有拥有 注入给父类的属性值。
        如果我们想要子类拥有注入给父类的属性值,该怎么办呢?可以通过修改配置文件和应用测试类来达到,如下所示:
1.AClass类
package com.bijian.spring.test3;

public class AClass {

    String a;
    String b;
   
    public String getA() {
        return a;
    }
    public void setA(String a) {
        this.a = a;
    }
    public String getB() {
        return b;
    }
    public void setB(String b) {
        this.b = b;
    }
}
 
2.BClass类
package com.bijian.spring.test3;

public class BClass extends AClass {

    String c;

    public String getC() {
        return c;
    }

    public void setC(String c) {
        this.c = c;
    }

    String fn() {
        return a + b + c;
    }
}
 
3.配置一个父类的配置文件parent3.xml
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"  
  "http://www.springframework.org/dtd/spring-beans.dtd">  

<beans>  
  <bean id="abstractbean" abstract="true">
      <property name="a">
          <value>1</value>
      </property>
      <property name="b">
          <value>2</value>
      </property>
  </bean>
</beans>
 
4.AClass类配置文件aclass3.xml
<?xml version="1.0" encoding="UTF-8"?>
  <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
  "http://www.springframework.org/dtd/spring-beans.dtd">

<beans>
  <bean id="bean1" class="com.bijian.spring.test3.AClass" parent="abstractbean"/>  
</beans>
 
5.BClass类配置文件bclass3.xml
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"  
  "http://www.springframework.org/dtd/spring-beans.dtd">  

<beans>
  <bean id="bean2" class="com.bijian.spring.test3.BClass" parent="abstractbean">
      <property name="c">
          <value>3</value>
     </property>
  </bean>
</beans>
 
6.应用测试类Test.java
package com.bijian.spring.test3;

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

public class Test {

public static void main(String[] args) {
       
        ApplicationContext ac = new ClassPathXmlApplicationContext(new String[]{"parent3.xml","aclass3.xml","bclass3.xml"});
       
        //得到子类,结果是:123
        BClass bclass = (BClass)ac.getBean("bean2");
        String res = bclass.fn();
        System.out.println(res);
    }
}
 

你可能感兴趣的:(java,spring)