我正在尝试为我的 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 构建重命名了 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
没有自动装配依赖项吗?