Bean 继承

/**
 * 
 * @author hong
 *
 */
public class HelloWorld1 {
    private String msg1;
    private String msg2;
    public void setMsg1(String msg1) {
        this.msg1=msg1;
    }
    public void getMsg1() {
        System.out.println("msg1:" + msg1);
    }
    public void setMsg2(String msg2) {
        this.msg2=msg2;
    }
    public void getMsg2() {
        System.out.println("msg2:" + msg2);
    }
}


 

Bean 继承

/**
 * 
 * @author hong
 *
 */
public class HelloWorld2 {
    private String msg1;
    private String msg2;
    private String msg3;
    public void setMsg1(String msg1) {
        this.msg1=msg1;
    }
    public void getMsg1() {
        System.out.println("msg1:" + msg1);
    }
    public void setMsg2(String msg2) {
        this.msg2=msg2;
    }
    public void getMsg2() {
        System.out.println("msg2:" + msg2);
    }
    public void setMsg3(String msg3) {
        this.msg3=msg3;
    }
    public void getMsg3() {
        System.out.println("msg3:" + msg3);
    }
}

Bean 继承

 <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd">
    <bean id="HelloWorld1" class="HelloWorld1">
      <property name="msg1" value="Hello World1"/>
      <property name="msg2" value="Hello Second World1"/>
   </bean>
   <bean id="HelloWorld2" class="HelloWorld2" parent="HelloWorld1">
      <property name="msg1" value="Hello World2"/>
      <property name="msg3" value="Hello Third World2"/>
   </bean>
</beans>

Bean 继承

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
 * 
 * @author hong
 *
 */
public class Main {
    public static void main(String[] args) {
        @SuppressWarnings("resource")
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        HelloWorld1 obj1 = (HelloWorld1) context.getBean("HelloWorld1");
        obj1.getMsg1();
        obj1.getMsg2();
         HelloWorld2 obj2 = (HelloWorld2) context.getBean("HelloWorld2");
         obj2.getMsg1();
         obj2.getMsg2();
         obj2.getMsg3();
    }
}

Bean 继承

msg2被继承,msg1被修改。 

模板方法:

Bean 继承

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd">
    <bean id="beanTeamplate" abstract="true">
      <property name="msg1" value="HelloWorld1"/>
      <property name="msg2" value="Hello Second World1"/>
      <property name="msg3" value="Hello Third World1"/>
   </bean>
   <bean id="HelloWorld2" class="HelloWorld2" parent="beanTeamplate">
      <property name="msg1" value="Hello World2"/>
      <property name="msg3" value="Hello Third World2"/>
   </bean>
</beans>

Bean 继承

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
 * 
 * @author hong
 *
 */
public class Main {
    public static void main(String[] args) {
        @SuppressWarnings("resource")
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
         HelloWorld2 obj2 = (HelloWorld2) context.getBean("HelloWorld2");
         obj2.getMsg1();
         obj2.getMsg2();
         obj2.getMsg3();
    }
}

 

相关文章:

  • 2022-12-23
  • 2021-11-06
  • 2022-12-23
  • 2021-11-14
  • 2021-05-29
  • 2021-08-31
  • 2022-01-08
  • 2021-06-30
猜你喜欢
  • 2021-08-14
  • 2021-09-14
  • 2022-12-23
  • 2021-07-22
  • 2021-09-26
  • 2021-06-25
相关资源
相似解决方案