Spring @Configurable类没有自动连接其依赖项的问题

时间:2014-10-22 11:52:51

标签: spring aspectj spring-boot javaagents load-time-weaving

我正在尝试为我的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的依赖关系没有自动装配?

0 个答案:

没有答案