我正在尝试为我的Spring Boot应用程序配置加载时间编织,以正确地自动装配@Configurable
java类的依赖项。
这是我的配置/主要类:
package com.bignibou;
@Configuration
@EnableAutoConfiguration(exclude = { SecurityAutoConfiguration.class, ThymeleafAutoConfiguration.class, FlywayAutoConfiguration.class })
@EnableSpringConfigured
@EnableLoadTimeWeaving(aspectjWeaving = AspectJWeaving.ENABLED)
@ComponentScan
public class Application {
public static void main(String[] args) throws Exception {
SpringApplication.run(Application.class, args);
}
}
以下是我启动应用程序的方法(我的gradle build重命名为spring-instrument jar):
java -javaagent:build/lib/springinstrument.jar -jar myapp.jar
这是@Configurable
类,它没有自动连接其依赖项:
package com.bignibou.converter;
@Configurable
public class StringToDayToTimeSlotConverter implements Converter<String, DayToTimeSlot> {
@Autowired
private DayToTimeSlotRepository dayToTimeSlotRepository;//NOT AUTOWIRED!!
@Override
public DayToTimeSlot convert(String id) {
return dayToTimeSlotRepository.findOne(Long.parseLong(id));//NPE HERE!!
}
}
这是实例化类的地方(使用new
):
@Configuration
@EnableWebMvc
@ComponentScan(basePackages = { "com.bignibou.controller" }, useDefaultFilters = false, includeFilters = { @Filter(type = FilterType.ANNOTATION, value = Controller.class),
@Filter(type = FilterType.ANNOTATION, value = ControllerAdvice.class) })
@Import(ApplicationControllerAdvice.class)
public class WebMvcConfiguration extends WebMvcConfigurerAdapter {
...
@Override
public void addFormatters(FormatterRegistry registry) {
registry.addConverter(new DayToTimeSlotToStringConverter());
registry.addConverter(new StringToDayToTimeSlotConverter());//INSTANTIATED HERE!
registry.addConverter(new LanguageToStringConverter());
registry.addConverter(new StringToLanguageConverter());
registry.addConverter(new AddressToStringConverter());
registry.addConverter(new StringToAddressConverter());
super.addFormatters(registry);
}
任何人都可以帮忙弄清楚为什么StringToDayToTimeSlotConverter
的依赖关系没有自动装配?