2017-06-13 7 views
0

@SpringBootApplicationクラスに複数の@Beanメソッドを配置して、必要なBeanを作成しています。それらのすべては1つを除いて実行されます。その1つのBeanメソッドは決して実行されないため、対応するクラス(サービスとして注釈付けされている)は例外を伴います。org.springframework.beans.factory.NoSuchBeanDefinitionExceptionSpringは@Beanメソッドを呼び出さない

同じクラスの他のBeanメソッドは実行されません。

consulServiceが呼び出される間、Application.java、haProxyServiceのメソッドは呼び出されません。

// Application.java: 
@SpringBootApplication 
public class Application { 
    //Config for services 
    //Consul 
    String consulPath = "/usr/local/bin/com.thomas.Oo.consul.consul"; 
    String consulConfPath = "/root/Documents/consulProto/web.json"; 

    //HAProxy 
    String haproxyPath = "/usr/local/bin/haproxy"; 
    String haproxyConfFilePath = "/root/Documents/consulProto/haproxy.conf"; 

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

    @Bean 
    public ConsulService consulService(){ 
     return new ConsulService(consulPath, consulConfPath); 
    } 

    @Bean 
    public HAProxyService haProxyService(){ 
     return new HAProxyService(haproxyPath, haproxyConfFilePath); 
    } 
} 


// ConsulService.java 
@Service 
public class ConsulService extends BaseService { 
    String executablePath; 
    String confFilePath; 

    public ConsulService(String consulPath, String confFilePath) { 
     this.executablePath = consulPath; 
     this.confFilePath = confFilePath; 
    } 
} 


// HAProxyService.java 
@Service 
public class HAProxyService extends BaseService { 
    String executablePath; 
    String confFilePath; 

    public HAProxyService(String executablePath, String confFilePath) { 
     this.executablePath = executablePath; 
     this.confFilePath = confFilePath; 
    } 
} 
+1

問題のクラスから@Serviceを削除すると、Beanメソッドが魔法のように呼び出されます.......何が起こっているのですか –

+3

あなたはいくつかのコードを表示する必要があります。誰もこれを確かに推測することはできません。 – Todd

+0

要点は次のとおりです。https://gist.github.com/thomas-oo/7d129f4a042fd87a8ed33f87a8dad396 consulServiceが呼び出されている間、Application.java、haProxyServiceのメソッドは呼び出されません。 HAProxyServiceから@Serviceを削除すると、Beanメソッドが呼び出されます。しかし、問題は解決しません。 –

答えて

4

@Service注釈を削除します。

あなたがコンポーネントスキャンの@Beanクラス注釈(@Service@Controller@Component、など)と手動豆の作成を混合しています。これは重複したインスタンスにつながります。

手動豆の作成を行うために@Service秒を残していないしたい場合はそうでない場合、あなたは@Bean注釈を持つメソッドを削除し、@Value注釈を使用して文字列値を挿入する必要があります。

例:

// Application.java: 
@SpringBootApplication 
public class Application { 
    public static void main(String[] args){ 
     SpringApplication.run(Application.class, args); 
    } 
} 

// HAProxyService.java 
@Service 
public class HAProxyService extends BaseService { 

    @Value("/usr/local/bin/haproxy") 
    private String executablePath; 
    @Value("/root/Documents/consulProto/haproxy.conf") 
    private String confFilePath; 

} 

// ConsulService.java 
@Service 
public class ConsulService extends BaseService { 

    @Value("/usr/local/bin/com.thomas.Oo.consul.consul") 
    private String executablePath; 
    @Value("/root/Documents/consulProto/web.json") 
    private String confFilePath; 

} 

アプリケーションを再コンパイルせずに簡単に変更することからapplication.propertiesファイルや利益にこれらの文字列を定義することができますので、これを行った後、私は、およそexternalizing your configurationを読むためにあなたをお勧めします。

関連する問題