XML Anmerkung Verwendung, ich bin eine Karte Injektion des unter config -Frühling Annotations - Injizieren Karte von Objekten
<bean id = "customerfactory" class = "com.brightstar.CustomerFactory">
<property name = "getCustomerMap">
<map key-type = "java.lang.String" value-type = "com.brightstar.CustomerImpl">
<entry key = "DEFAULT" value-ref = "getDefaultImpl"></entry>
<entry key = "PERSON" value-ref = "getPersonImpl"></entry>
<entry key = "COMPANY" value-ref = "getCompanyImpl"></entry>
</map>
</property>
</bean>
Ich habe 3 Bohnen erstellt - DefaultImpl, PersonImpl und CompanyImpl. Wie kann ich diese als Map mit Spring Annotation injizieren?
EDIT: Vorerst habe ich die folgenden, aber nicht sicher durchgeführt, wenn es der empfohlene Ansatz ist
private Map<String, CustomerImpl> getCustomerMap ;
@Autowired
private GetDefaultImpl getDefaultImpl;
@Autowired
private GetPersonImpl getPersonImpl;
@Autowired
private GetCompanyImpl getCompanyImpl;
private static final String DEFAULT = "DEFAULT";
private static final String COM = "PERSON";
private static final String SOM = "COMPANY";
@PostConstruct
public void init(){
getCustomerMap = new LinkedHashMap<String,CustomerImpl>();
getCustomerMap.put(DEFAULT, getDefaultImpl);
getCustomerMap.put(PERSON, getPersonImpl);
getCustomerMap.put(COMPANY, getCompanyImpl);
}
Vielen Dank. In meinem Fall muss ich Objekte injizieren, die nicht in der Eigenschaftendatei vorhanden sein können. –
Danke, das hilft. Kannst du bitte auch meinen Ansatz kommentieren? Ich werde die Antwort akzeptieren. –
Ich bin mir nicht sicher, @Postconstruct ist die beste Methode, Bean zu initialisieren oder den Lebenszyklus von Bean zu verwalten, da es JEE-Standard ist, aber keine Abhängigkeiten injiziert. Aber, Ja, Best Practices sind keine Regeln. –