3
我動態地添加上下文來用下面的代碼的應用程序上下文:動力方面 - 自動裝配
@Component
@Scope("singleton")
public class DynamicContextLoader implements ApplicationContextAware {
private static ApplicationContext context;
private Map<String, InterfacePropertyDto> contextMap;
@Autowired
IJpaDao jpaDao;
@PostConstruct
public void init() {
contextMap = (Map<String, InterfacePropertyDto>) context.getBean("contextMap");
contextMap.forEach((contextName, property) -> {
String p = jpaDao.getProperty(property.getPropertyName(), property.getPropertyType());
if (p != null) {
ConfigurableApplicationContext ctx = new ClassPathXmlApplicationContext(
new String[]{"/META-INF/spring/integration/" + contextName},
false, context);
ctx.refresh();
}
});
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
context = applicationContext;
}
}
這種運作良好,並在新的語境中定義的所有的bean被創建。但@Autowired不適用於這些新的bean。例如,在新的上下文定義爲豆
:
<bean id="outboundContractJdbcFileMapper" class="com.......integration.model.contract.ContractMapper"/>
具有以下自動裝配:
public class ContractMapper implements RowMapper<ContractFile> {
@Autowired
IIntegrationDao integrationDao;
@Override
public ContractFile mapRow(ResultSet rs, int rowNum) throws SQLException {
......
}
}
在運行時,outboundContractJdbcFileMapper屬性integrationDao爲空。
有沒有辦法在創建bean時強制自動裝配發生?我希望ctx.refresh()能做到這一點。
This works great。謝謝! – alan