2016-03-04 96 views
0

我在appconfig文件中創建服務bean。如何用FactoryBean或其他方式創建這個bean,而不是一個一個地創建它?如何使用factorybean創建服務bean

@SpringBootApplication(exclude = {SecurityAutoConfiguration.class }) 
@ComponentScan 
public class Application { 
    public static void main(String[] args) { 

     SpringApplication.run(Application.class, args); 
    } 

    @Bean 
    public SimpleService<Work> workService() { return new SimpleServiceImpl<Work>() { }; } 

    @Bean 
    public SimpleService<User> userService() { return new SimpleServiceImpl<User>() { }; } 

    @Bean 
    public SimpleService<Author> authorService() { return new SimpleServiceImpl<Author>() { }; } 

    @Bean 
    public SimpleService<Genre> genreService() { return new SimpleServiceImpl<Genre>() { }; } 

    @Bean 
    public SimpleService<BookCondition> bookConditionService() { return new SimpleServiceImpl<BookCondition>() { }; } 

    @Bean 
    public SimpleService<BookType> bookTypeService() { return new SimpleServiceImpl<BookType>() { }; } 

    @Bean 
    public SimpleService<Offer> offerService() { return new SimpleServiceImpl<Offer>() { }; } 

除了這個服務,我只有genericService和genericsSvviseImpl。

回答

0

您可以使用@Service註釋。

假設您的服務/類名是SimpleServiceImpl,然後在類名的頂部定義 @Service(「SimpleService」)。春天將創建這個bean作爲一個bean。

另一種方法是使用此鏈接中介紹的factoryBean How to get beans created by FactoryBean spring managed?