/**
*
* @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);
}
}
/**
*
* @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);
}
}
<?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>
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();
}
}
msg2被继承,msg1被修改。
模板方法:
<?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>
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();
}
}