2016-03-04 93 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?