如何使用factorybean创建服务bean

时间:2016-03-04 13:25:01

标签: java spring-boot spring-bean

我在appconfig文件中创建服务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和genericsSrviseImpl。

1 个答案:

答案 0 :(得分:0)

您可以使用@Service注释。

假设你的服务/类名是SimpleServiceImpl然后定义 @Service(&#34; SimpleService&#34;)在类名之上。 Spring会将它创建为bean。

另一种方法是使用factoryBean,在此链接How to get beans created by FactoryBean spring managed?

中对此进行了解释