0

I'm creating a custom starter for spring boot.

I've this class for configuration of autostarter

@Configuration
@AutoConfigureAfter(SpringDataWebAutoConfiguration.class)
@ComponentScan(basePackages = {"com.closure.table"})
public class ClosureTableAutoConfiguration {


    CategoryTypeRepository categoryTypeRepository; // is under com.closure.table.repository and is annotated

    public ClosureTableAutoConfiguration(CategoryTypeRepository categoryTypeRepository){
        this.categoryTypeRepository = categoryTypeRepository;
    }

    @Bean
    @ConditionalOnMissingBean
    public ClosureTable closureTable() {
        return new ClosureTable();
    }

    @Bean
    @ConditionalOnMissingBean
    @DependsOn("CategoryTypeRepository")
    public CategoryTypeService getCategoryTypeService() {
        return new CategoryTypeService(categoryTypeRepository);
    }
... 

The starter applications works perfectly, inside a classic web application, if I don't use repository inside service, otherwise I got this error:

Consider defining a bean of type 'com.closure.table.repository.CategoryTypeRepository' in your configuration.

In web application I've create a configuration class like this

@Configuration
@Import({ClosureTableAutoConfiguration.class})
public class ClosureTableConfiguration {


}

Is there a way to accomplisce this scope?

ciro
  • 771
  • 1
  • 8
  • 30

1 Answers1

0

After 10 hours and reading more on web I found solution that works.

In auto configuration of starter I change code in this way:

@Configuration
@AutoConfigureBefore(JpaRepositoriesAutoConfiguration.class)
@EnableJpaRepositories
@ComponentScan  //needed
@Import(StarterEntityRegistrar.class) //needed to load entity
public class ClosureTableAutoConfiguration { // 
    ....
    
    @Bean
    @ConditionalOnMissingBean
    public CategoryTypeService getCategoryTypeService(CategoryTypeRepository categoryTypeRepository) { //moved autowired from constructor to bean definitions
        return new CategoryTypeService(categoryTypeRepository);
    }
}

...

public class StarterEntityRegistrar implements ImportBeanDefinitionRegistrar {
    @Override
    public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
        AutoConfigurationPackages.register(registry, CategoryType.class.getPackageName());
    }

}

Then simple add custom starter as dependencies.

In this moment both, custom starter and test application, point to same db schema.

ciro
  • 771
  • 1
  • 8
  • 30