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
Customerclass'sfirstNameandlastNameare private, so you can't access them directly indoWork(). Add getter methods to theCustomerclass: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
@Qualifieris specified, add@Primaryto its@Beanmethod. For example, adding@PrimarytodefaultCustomer()would mean any class with just@Autowired private Customer customer;gets the default constructor instance.
内容的提问来源于stack exchange,提问作者pritamprasad

