Не регистрируйте компонент, если нет никаких зависимых компонентов, использующих spring-boot-starter - PullRequest
0 голосов
/ 30 мая 2019

Я создаю spring-boot-starter, который регистрирует некоторый компонент:

@Configuration
@EnableConfigurationProperties(ClusterJProperties.class)
@ConditionalOnProperty(prefix = "clusterj", name = {"connectString", "dataBaseName"})
public class ClusterJAutoConfiguration {

    private final ClusterJProperties clusterJConfigProperties;

    @Autowired
    public ClusterJAutoConfiguration(ClusterJProperties clusterJConfigProperties) {
        this.clusterJConfigProperties = clusterJConfigProperties;
    }

    @Bean
    @ConditionalOnMissingBean
    public SessionFactory sessionFactory() {
        Properties clusterJProperties = new Properties();
        clusterJProperties.setProperty("com.mysql.clusterj.connectstring", clusterJConfigProperties.getConnectString());
        clusterJProperties.setProperty("com.mysql.clusterj.database", clusterJConfigProperties.getDataBaseName());
        clusterJProperties.putAll(clusterJConfigProperties.getProperties());
        return ClusterJHelper.getSessionFactory(clusterJProperties);
    }
}

Я хочу использовать этот стартер в другом модуле. Для этого я создал компонент и вставил туда этот компонент:

@Component
public class GetEntityHandler implements VoidTreeNodeHandler {

    private final SessionFactory sessionFactory;

    @Autowired
    public GetEntityHandler(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }

    @Override

    public void handle(TreeContext context, Map<String, Object> parameters) {
       //do smth
    }
}

Но, когда свойства clusterj.connectString и clusterj.connectString отсутствуют в application.yml, bean SessionFactory не создается. Это нормально, но GetEntityHandler хочу создать и падает с ошибкой:

Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'getEntityHandler' defined in file [GetEntityHandler.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.mysql.clusterj.SessionFactory' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}

Как сделать так, чтобы мой GetEnityNandler компонент не создавался, если бин SessionFactory отсутствует в контексте?

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...