2016-10-25 15 views
0

クラスをHashMapに配置したいですか?そのために、私は@ServiceでBeanを作成しました。これです:Springブート - 手動でBeanを作成してハッシュマップに渡す方法

@Service 
public class ServiceManagerImpl implements ServiceManager { 

@Override 
public void registerService() { 
    // registerService will put this in the HashMap! 
    dispatcher.registerService("serviceList", getServiceListImpl()); 
} 

@Bean 
public BusinessService getServiceListImpl() { 
    return new ServiceListManager(); 
} 
} 

これは正しい方法ですか?

答えて

1

@ServiceでアノテーションされたクラスからBean定義を@Configurationでアノテーションされたコンフィグレーションクラスに移動します(または、少なくとも@SpringBootApplicationアノテーションを持つメインクラスに移動します)。次に、このBeanをServiceクラスでAutowireします。 `

@AutowiredにBusinessService businessServiceに

@設定に@サービスを変更するここWhere to put @Bean in Spring Boot?

+0

を見てみましょう_BeanCreatinException_をスローします。あなたは_moveをメインクラスとどういう意味ですか?私にとっては明らかではない。 – MissBonbon

+0

あなたのアプリケーションのエントリーポイントはどれですか?あなたの主な方法はどこですか? ここを見てくださいhttp://stackoverflow.com/questions/29146019/where-to-put-bean-in-spring-boot?rq=1 –

+0

ありがとう、今私は知っている、あなたが意味すること...そしてBeanをアプリケーション・コンテキストで動的にロードしたいとき、私は何をしなければなりませんか?その後、設定ファイルは動作しません。 – MissBonbon

関連する問題