如何将含父Bean的XML配置转换为Java Bean配置并实现父Bean属性继承
Problem Description
I need to convert the following Spring XML bean configuration to Java-based configuration:
<bean id="baseBook" class="com.practice.Book" > <property name="bookName" value="First book" /> </bean> <bean id="bookBean" class="com.practice.ChildBook" parent="baseBook"> <property name="bookAuthor" value="Raj" /> <property name="bookPrice" value="200" /> </bean>
I've tried writing the following Java configuration code:
@Bean public ChildBook bookBean(){ ChildBook bookBean = new ChildBook(); bookBean.setBookAuthor("Raj"); bookBean.setBookPrice(200); return bookBean; } @Bean public Book baseBook(){ Book book = new Book(); book.setBookName("First book"); return book; }
But the problem is that I can't set the bookName property for ChildBook in a way that mirrors the XML's parent bean inheritance. I've looked up some solutions that suggest creating a new initialization method and calling it in bookBean(), but I want a more accurate and convenient solution. Thanks in advance!
Solution: Inject and Reuse the Parent Bean's Properties
Spring's Java-based configuration doesn't have a direct equivalent to XML's parent bean attribute, but you can replicate the exact behavior by reusing the parent bean's properties in your child bean definition. Spring proxies @Bean methods, so you can safely call the parent bean's method directly (it returns the container-managed singleton, not a new instance) or inject the parent bean explicitly.
Option 1: Directly Call the Parent Bean Method
This is the closest match to your original XML, as it directly reuses the parent bean's configured properties:
@Configuration public class BookConfig { @Bean public Book baseBook(){ Book book = new Book(); book.setBookName("First book"); return book; } @Bean public ChildBook bookBean(){ // Fetch the container-managed parent bean Book baseBookInstance = baseBook(); ChildBook bookBean = new ChildBook(); // Reuse the parent's bookName property bookBean.setBookName(baseBookInstance.getBookName()); // Set child-specific properties bookBean.setBookAuthor("Raj"); bookBean.setBookPrice(200); return bookBean; } }
Option 2: Constructor Injection (Cleaner Explicit Dependency)
For more readable code and explicit dependency declaration, inject the parent bean directly into the child bean's @Bean method:
@Configuration public class BookConfig { @Bean public Book baseBook(){ Book book = new Book(); book.setBookName("First book"); return book; } @Bean public ChildBook bookBean(Book baseBook){ ChildBook bookBean = new ChildBook(); bookBean.setBookName(baseBook.getBookName()); bookBean.setBookAuthor("Raj"); bookBean.setBookPrice(200); return bookBean; } }
Why This Works
- XML's
parentattribute copies property values from the parent bean definition to the child. In Java config, we explicitly replicate this by fetching the parent bean and applying its properties to the child. - When you call
baseBook()inside another@Beanmethod, Spring's proxy ensures you get the singleton instance managed by the container (not a new object), which matches XML bean inheritance behavior.
If ChildBook extends Book, you could also refactor to share initialization logic, but the above approaches are the most direct equivalents to your original XML setup.
内容的提问来源于stack exchange,提问作者Sai

