2016-04-18 1 views
8

フィールド注入が@SpringBootApplicationクラスで動作し、コンストラクタインジェクションがなぜ動作しないのだろうと思います。スプリングブート@SpringBootApplicationクラスにデフォルトコンストラクタが見つかりません

マイApplicationTypeBeanで期待どおりに動作しますが、私はCustomTypeServiceのコンストラクタ・インジェクションを持つようにしたいとき、私はこの例外を受け取る:

Failed to instantiate [at.eurotours.ThirdPartyGlobalAndCustomTypesApplication$$EnhancerBySpringCGLIB$$2a56ce70]: No default constructor found; nested exception is java.lang.NoSuchMethodException: at.eurotours.ThirdPartyGlobalAndCustomTypesApplication$$EnhancerBySpringCGLIB$$2a56ce70.<init>() 

それは@SpringBootApplicationクラスでは動作しない理由何らかの理由はありますか?


マイSpringBootApplicationクラス:

@SpringBootApplication 
public class ThirdPartyGlobalAndCustomTypesApplication implements CommandLineRunner{ 

@Autowired 
ApplicationTypeBean applicationTypeBean; 

private final CustomTypeService customTypeService; 

@Autowired 
public ThirdPartyGlobalAndCustomTypesApplication(CustomTypeService customTypeService) { 
    this.customTypeService = customTypeService; 
} 

@Override 
public void run(String... args) throws Exception { 
    System.out.println(applicationTypeBean.getType()); 
    customTypeService.process(); 
} 

public static void main(String[] args) { 
    SpringApplication.run(ThirdPartyGlobalAndCustomTypesApplication.class, args); 
} 

public CustomTypeService getCustomTypeService() { 
    return customTypeService; 
} 

マイ@Serviceクラス:

@Service 
public class CustomTypeService { 

    public void process(){ 
     System.out.println("CustomType"); 
    } 
} 

マイ@Componentクラス:

@Component 
@ConfigurationProperties("application.type") 
public class ApplicationTypeBean { 

    private String type; 

答えて

6

SpringBootApplicationはメタANあります

// Other annotations 
@Configuration 
@EnableAutoConfiguration 
@ComponentScan 
public @interface SpringBootApplication { ... } 

だからbaiscally、あなたのThirdPartyGlobalAndCustomTypesApplicationも春Configurationクラスです:表記。 Configurationさんjavadoc状態として:

@Configurationがあるので @Configurationクラスはコンポーネント(通常は春XMLの要素を使用して)スキャン と の候補もの利点がかかる場合がありますのであり、@Componentでメタ注釈付き@ Autowired/@ Injectフィールドに とメソッドレベル(がありますが、コンストラクターレベルではではありません)。

したがって、Configurationクラスのコンストラクタインジェクションは使用できません。どうやら、this answerjira ticketに基づいて4.3リリースで修正される予定です。

+1

ご理解いただきありがとうございます。 – Patrick

+1

引用符が鍵です。私は4.3からダウングレードする必要がありました。これはどこで実行可能です。 – sschrass

関連する問題