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

Spring基于构造函数多态的@Autowire注入:多构造函数Customer类注入问题

Alright, let's figure out how to make this work. You want to use Spring's @Autowired to inject different Customer instances—one from the default constructor, another from the parameterized one—into separate classes, right? Here's a straightforward approach using Spring's bean naming and qualifier annotations:

Step 1: Create Named Customer Beans in a Configuration Class

Spring needs a way to distinguish between the two Customer instances, so we'll define them as named beans using @Bean in a configuration class. This lets us explicitly control which constructor is used for each bean.

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class CustomerConfig {

    // Bean created with the default constructor (your "dummy" customer)
    @Bean("defaultCustomer")
    public Customer defaultCustomer() {
        return new Customer();
    }

    // Bean created with the parameterized constructor (custom values)
    @Bean("customCustomer")
    public Customer customCustomer() {
        // You can pass any first/last name here, adjust as needed
        return new Customer("Jane", "Smith");
    }
}

Step 2: Autowire the Correct Bean into Each Class

Now, in your target classes, you'll use @Autowired along with @Qualifier to specify which named bean to inject. Also, quick correction: Spring's annotation is spelled @Autowired (not @Autowire—that typo would cause issues!).

For MainDefault (inject default constructor Customer):

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

class MainDefault {
    // Inject the "defaultCustomer" bean created via the no-arg constructor
    @Autowired
    @Qualifier("defaultCustomer")
    private Customer customer;

    public void doWork() {
        // Wait—your Customer class has private fields! Add getters first (see note below)
        System.out.println(customer.getFirstName());
    }
}

For a MainCustom class (inject parameterized constructor Customer):

If you have another class that needs the custom-named customer, it would look like this:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

class MainCustom {
    // Inject the "customCustomer" bean created via the parameterized constructor
    @Autowired
    @Qualifier("customCustomer")
    private Customer customer;

    public void doWork() {
        System.out.println(customer.getFirstName() + " " + customer.getLastName());
    }
}

Critical Fixes & Extra Tips

  • Fix private field access: Your Customer class's firstName and lastName are private, so you can't access them directly in doWork(). Add getter methods to the Customer class:
    class Customer {
        private String firstName;
        private String lastName;
    
        public Customer() {
            firstName = "dummy";
            lastName = "dummy";
        }
    
        public Customer(String first, String last) {
            firstName = first;
            lastName = last;
        }
    
        // Add these getters
        public String getFirstName() {
            return firstName;
        }
    
        public String getLastName() {
            return lastName;
        }
    }
    
  • Use @Primary for default bean: If you want one of the beans to be the "default" when no @Qualifier is specified, add @Primary to its @Bean method. For example, adding @Primary to defaultCustomer() would mean any class with just @Autowired private Customer customer; gets the default constructor instance.

内容的提问来源于stack exchange,提问作者pritamprasad

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.21 04:19:46