2016-04-04 7 views
0

私はそうのようなその子クラスを注入したいビーンがあります。これは、次のエラーが発生しSpringでは、親Beanに子Beanを挿入するにはどうしたらいいですか?

package test; 

@Component 
public class Child extends Parent { 
} 

org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [test.Child] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency 

親:

package test; 

@Component 
public class Parent { 
    @Autowired 
    Child child; 

    public Child getChild() { 
    return child; 
    } 
} 

子供を

extends Parentビットを削除すると、すべてがt o期待どおりに動作するので、Springが何らかの理由でシャドーイングしているように、SpringがChildを見つけることができないように見えます。これらの子インスタンスを正しく結び付けるためにSpringを設定するにはどうすればよいですか?私は、次のように、Javaクラスの設定を使用しています:

@Configuration 
@ComponentScan(basePackages = "test") 
public class AppConfig { 
} 

私は@Qualifier注釈で遊んと助けにはならなかった、以下、同様にAppConfig内の異なる名前を割り当てる試してみました:

@Bean(name = "parent") 
public Parent parent() { 
    return new Parent(); 
} 

@Bean(name = "child") 
public Child child() { 
    return new Child(); 
} 

SpringにChildクラスがそれ自身の別個のエンティティとして見えるようにするために欠けている成分が何であるか分かりません。これは不可能ですか? Childので

答えて

2

を私は次のように親の定義を変更することで問題を解決しました:

package test; 

@Primary 
@Component 
public class Parent { 
    @Resource 
    Child child; 

    public Child getChild() { 
    return child; 
    } 
} 

そしてまた次のように子供を修正する:

package test; 

@Component("child") 
public class Child extends Parent { 
} 

私はタイプする前に、名前で検索を試み@Resource@Autowired注釈を、交換し、子クラスに明示的に名前を追加しています。親をオートワイヤリングしようとしたときに親から子供を曖昧にするために、私は@Primary注釈を親に追加しました。

タイプ別のルックアップが失敗する理由をまだ完全に理解していないため、代わりに@Resourceを使用して名前でBeanを検索する必要があります。

+0

初期のエラーでこれは@ user108471で失敗します。私は、Spring 4.2.3をLinux上で使用するSpring Boot 1.3.3とJDK 8を使用しています。 –

+0

@JanNielsen申し訳ありませんが、私は別の重要な変更を省いたようです:子クラスに明示的な名前を追加します。 – user108471

0

Parentを拡張し、ParentChildを構築するために構築されなければならないが、Parent建設用Childを必要とします。これは循環依存です。コードに循環依存関係を作成しないようにします。

コードをリファクタリングする必要があります。また

package hello; 

import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.boot.SpringApplication; 
import org.springframework.boot.autoconfigure.SpringBootApplication; 
import org.springframework.context.annotation.Bean; 
import org.springframework.stereotype.Component; 

@SpringBootApplication 
public class Application { 
    public static void main(String[] args) { 
     SpringApplication.run(Application.class, args); 
    } 
    @Autowired 
    private Parent parent; 
    @Bean 
    Child makeChild() { return new Child(); } 
} 
@Component 
class Parent extends Child { 
    @Autowired 
    private Child child; 

    public Child getChild() { return this.child; } 
} 
class Child { 
} 

:あなたが関係を反転させることができればParentChild(代わりに、あなたは今それを持っているように逆の)出るようにこれは、あなたが注入できるようになりますあなたのコードベースで可能ではないかもしれないが、 、ChildParentの両方が別のクラスに必要とParentChildにそのコードを挿入コードリファクタリング:

package hello; 

import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.boot.SpringApplication; 
import org.springframework.boot.autoconfigure.SpringBootApplication; 
import org.springframework.context.annotation.Bean; 
import org.springframework.stereotype.Component; 

@SpringBootApplication 
public class Application { 
    public static void main(String[] args) { 
     SpringApplication.run(Application.class, args); 
    } 
    @Autowired 
    private Parent parent; 
    @Bean 
    Child makeChild() { return new Child(); } 
    @Bean 
    Shared makeShared() { return new Shared(); } 
} 
@Component 
class Parent { 
    @Autowired 
    private Shared shared; 
    @Autowired 
    private Child child; 
    public Child getChild() { return this.child; } 
} 
class Child { 
    @Autowired 
    private Shared shared; 
} 
class Shared { 
} 
+0

私を許してください、実際に問題を解決するコードスニペットの部分を理解していません。あなたはChild上の@Componentアノテーションを削除していますが、同じことを行うと何も変わりません。あなたはまた、新しいクラスを作成し、私の質問でクラスの1つを省いた、と私は解決策を変更する方法は不明です。 – user108471

+0

私は 'Child 'に' extends Parent'を見逃しました。これは循環的依存関係を作り出します。なぜなら、あなたはこの問題を見ているからです。私は私の答えを更新します。 –

+0

私はその省略を直ちに見たように見えます。 – user108471

関連する問題