Skip to content

Commit 370f77d

Browse files
committed
1 parent 6b0cddc commit 370f77d

File tree

4 files changed

+95
-0
lines changed

4 files changed

+95
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package basic.ioc.config.inheritance;
2+
3+
import java.util.StringJoiner;
4+
5+
public abstract class Author {
6+
7+
private String name;
8+
private Integer age;
9+
10+
public String getName() {
11+
return name;
12+
}
13+
14+
public void setName(String name) {
15+
this.name = name;
16+
}
17+
18+
public Integer getAge() {
19+
return age;
20+
}
21+
22+
public void setAge(Integer age) {
23+
this.age = age;
24+
}
25+
26+
@Override
27+
public String toString() {
28+
return new StringJoiner(", ", Author.class.getSimpleName() + "[", "]")
29+
.add("name='" + name + "'")
30+
.add("age=" + age)
31+
.toString();
32+
}
33+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package basic.ioc.config.inheritance;
2+
3+
import java.util.StringJoiner;
4+
5+
public class BookAuthor extends Author {
6+
7+
private String bookName;
8+
9+
public String getBookName() {
10+
return bookName;
11+
}
12+
13+
public void setBookName(String bookName) {
14+
this.bookName = bookName;
15+
}
16+
17+
@Override
18+
public String toString() {
19+
return new StringJoiner(", ", BookAuthor.class.getSimpleName() + "[", "]")
20+
.add(super.toString())
21+
.toString();
22+
}
23+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<beans xmlns="http://www.springframework.org/schema/beans"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://www.springframework.org/schema/beans
5+
http://www.springframework.org/schema/beans/spring-beans.xsd">
6+
7+
<bean id="abstractAuthor" abstract="true"
8+
class="basic.ioc.config.inheritance.Author">
9+
<property name="name" value="author"/>
10+
<property name="age" value="18"/>
11+
</bean>
12+
13+
<bean id="bookAuthor" parent="abstractAuthor"
14+
class="basic.ioc.config.inheritance.BookAuthor">
15+
<!-- override - age -->
16+
<property name="age" value="20"/>
17+
<property name="bookName" value="The Book"/>
18+
</bean>
19+
20+
</beans>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package basic.ioc.config.inheritance;
2+
3+
import org.junit.jupiter.api.Test;
4+
import org.springframework.context.ApplicationContext;
5+
import org.springframework.context.support.ClassPathXmlApplicationContext;
6+
7+
public class TestConfigInheritance {
8+
9+
@Test
10+
public void testBeanConfigInheritance(){
11+
ApplicationContext aContext
12+
= new ClassPathXmlApplicationContext("basic.ioc.bean-config.inheritance.xml");
13+
14+
Author author = aContext.getBean(Author.class);
15+
BookAuthor bookAuthor = aContext.getBean(BookAuthor.class);
16+
System.out.println(author);
17+
System.out.println(bookAuthor);
18+
}
19+
}

0 commit comments

Comments
 (0)