You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将含父Bean的XML配置转换为Java Bean配置并实现父Bean属性继承

How to Replicate Spring XML Bean Inheritance in Java-Based Configuration

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 parent attribute 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 @Bean method, 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.28 12:27:43