Vâng, bạn có thể làm điều đó với sự trợ giúp của tùy chỉnh BeanFactoryPostProcessor thực hiện của bạn.
Đây là một ví dụ đơn giản.
Giả sử chúng tôi có hai thành phần. Một là phụ thuộc cho người khác.
thành phần đầu tiên:
import org.springframework.beans.factory.InitializingBean;
import org.springframework.util.Assert;
public class MyFirstComponent implements InitializingBean{
private MySecondComponent asd;
private MySecondComponent qwe;
public void afterPropertiesSet() throws Exception {
Assert.notNull(asd);
Assert.notNull(qwe);
}
public void setAsd(MySecondComponent asd) {
this.asd = asd;
}
public void setQwe(MySecondComponent qwe) {
this.qwe = qwe;
}
}
Như bạn có thể thấy, không có gì là đặc biệt về thành phần này. Nó có sự phụ thuộc vào hai trường hợp khác nhau của MySecondComponent.
Thứ hai thành phần:
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.annotation.Qualifier;
@Qualifier(value = "qwe, asd")
public class MySecondComponent implements FactoryBean {
public Object getObject() throws Exception {
return new MySecondComponent();
}
public Class getObjectType() {
return MySecondComponent.class;
}
public boolean isSingleton() {
return true;
}
}
Đó là khó khăn hơn một chút. Đây là hai điều cần giải thích. Đầu tiên - @Qualifier - chú thích chứa tên của hạt MySecondComponent. Đó là một tiêu chuẩn, nhưng bạn được tự do thực hiện của riêng bạn. Bạn sẽ thấy một chút sau đó tại sao.
Điều thứ hai cần đề cập là triển khai FactoryBean. Nếu đậu thực hiện giao diện này, nó sẽ tạo ra một số trường hợp khác. Trong trường hợp của chúng ta, nó tạo ra các cá thể với kiểu MySecondComponent.
Phần khó khăn nhất là thực hiện BeanFactoryPostProcessor:
import java.util.Map;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
public class MyBeanFactoryPostProcessor implements BeanFactoryPostProcessor {
public void postProcessBeanFactory(ConfigurableListableBeanFactory configurableListableBeanFactory) throws BeansException {
Map<String, Object> map = configurableListableBeanFactory.getBeansWithAnnotation(Qualifier.class);
for(Map.Entry<String,Object> entry : map.entrySet()){
createInstances(configurableListableBeanFactory, entry.getKey(), entry.getValue());
}
}
private void createInstances(
ConfigurableListableBeanFactory configurableListableBeanFactory,
String beanName,
Object bean){
Qualifier qualifier = bean.getClass().getAnnotation(Qualifier.class);
for(String name : extractNames(qualifier)){
Object newBean = configurableListableBeanFactory.getBean(beanName);
configurableListableBeanFactory.registerSingleton(name.trim(), newBean);
}
}
private String[] extractNames(Qualifier qualifier){
return qualifier.value().split(",");
}
}
nó làm gì? Nó đi qua tất cả các bean được chú thích bằng @Qualifier, trích xuất các tên từ chú thích và sau đó tạo thủ công các hạt của loại này với các tên được chỉ định.
Đây là một cấu hình mùa xuân:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean class="MyBeanFactoryPostProcessor"/>
<bean class="MySecondComponent"/>
<bean name="test" class="MyFirstComponent">
<property name="asd" ref="asd"/>
<property name="qwe" ref="qwe"/>
</bean>
</beans>
Điều cuối cùng để nhận thấy ở đây là mặc dù bạn có thể làm điều đó bạn không nên trừ khi nó là điều bắt buộc, bởi vì đây là một cách không thực sự tự nhiên cấu hình. Nếu bạn có nhiều hơn một thể hiện của lớp, tốt hơn là nên kết nối với cấu hình XML.
Tôi không nghĩ bạn có thể. '@ Component' là một tiện ích nhẹ, nhưng không thay thế cho cấu hình XML. – skaffman
Tôi nghĩ thật đáng tiếc khi XML được coi là cách thích hợp để cấu hình một ứng dụng. –
Chỉ vì '@ Component' không thể làm điều đó không có nghĩa là XML là giải pháp. Tôi không biết về năm 2011, nhưng bạn có thể đạt được hiệu quả tương tự trong java '@ Configuration' ngay bây giờ. – apottere