У меня есть класс конфигурации, в котором я создаю компонент, который использую в другом компоненте, объявленном в файле xml. Когда я пытаюсь использовать его из определенного xml bean-компонента, он выдает NPE, потому что он никогда не был автоматически подключен. Я просканировал пакет, в котором я автоматически подключаю бин конфигурации, но даже при этом не принимаю его.
MyPropertyPlaceholderConfigurer.java
package org.util;
public class MyPropertyPlaceholderConfigurer extends PropertyPlaceholderConfigurer {
@Autowired
private FileBasedConfigurationBuilder<FileBasedConfiguration> propertiesConfigurationBuilder;
@Override
protected void processProperties(ConfigurableListableBeanFactory beanFactoryToProcess, Properties props) throws BeansException {
super.processProperties(beanFactoryToProcess, props);
//breaks due to NPE
FileBasedConfiguration configuration = propertiesConfigurationBuilder.getConfiguration();
}
}
applicationContext.xml
<bean id="myBean" class="org.util.MyPropertyPlaceholderConfigurer">
<property name="locations">
<list>
<ref bean="appProps" />
</list>
</property>
</bean>
<context:component-scan base-package="org.util" />
PropertiesFileConfiguration.java
package org.configuration;
@Configuration
public class PropertiesFileConfiguration {
@Bean
public FileBasedConfigurationBuilder<FileBasedConfiguration> propertiesConfigurationBuilder() {
PropertiesBuilderParameters properties = new Parameters().properties();
properties.setFileName("application.properties");
FileBasedConfigurationBuilder<FileBasedConfiguration> builder =
new FileBasedConfigurationBuilder<>(PropertiesConfiguration.class);
builder.setAutoSave(true);
return builder.configure(properties);
}
}
Я использую Spring 4.3.26. НЕ использует SpringBoot.