Spring Boot如何仅加载指定的@Repository组件

Spring Boot如何仅加载指定的@Repository组件,spring,spring-boot,Spring,Spring Boot,我有一个项目,其中包含许多由@Repository注释的Dao 还有几个spring引导项目,每个项目都有其spring上下文,可以独立运行,并且它们对包含DAO的项目有一个引用 问题是,我不想在每个项目中将所有Dao加载到spring上下文中。每个spring引导项目只需要一些指定的Dao 我曾经通过在每个项目的XML配置中将Dao类定义为bean来指定它们 现在,我们将转向基于java和注释的配置 有没有办法告诉spring上下文只加载我指定的@Repository 我知道我可以创建@Con

我有一个项目,其中包含许多由@Repository注释的Dao

还有几个spring引导项目,每个项目都有其spring上下文,可以独立运行,并且它们对包含DAO的项目有一个引用

问题是,我不想在每个项目中将所有Dao加载到spring上下文中。每个spring引导项目只需要一些指定的Dao

我曾经通过在每个项目的XML配置中将Dao类定义为bean来指定它们

现在,我们将转向基于java和注释的配置

有没有办法告诉spring上下文只加载我指定的@Repository


我知道我可以创建@Configuration类并定义@Bean方法,但我仍然需要将它们视为@Repository,而不是普通Bean。知道这是否受支持以及如何实现吗?

@springboot应用程序
只需结合
@EnableAutoConfiguration
@SpringBootConfiguration
@ComponentScan

@ComponentScan
是导致扫描包下的所有
@Repository
bean自动注册的家伙,这是您不希望发生的事情

因此,您可以单独使用这些注释,但不包括
@ComponentScan
。并使用
@Import
显式定义要注册的bean

主应用程序类将如下所示:

@SpringBootConfiguration
@启用自动配置
@导入(值={foorepository.class,BarRepository.class,…})
公共类应用程序{
公共静态void main(字符串[]args){
SpringApplication.run(Application.class,args);
}
}

@springboot应用程序
只需将
@EnableAutoConfiguration
@SpringBootConfiguration
@ComponentScan
组合起来即可

@ComponentScan
是导致扫描包下的所有
@Repository
bean自动注册的家伙,这是您不希望发生的事情

因此,您可以单独使用这些注释,但不包括
@ComponentScan
。并使用
@Import
显式定义要注册的bean

主应用程序类将如下所示:

@SpringBootConfiguration
@启用自动配置
@导入(值={foorepository.class,BarRepository.class,…})
公共类应用程序{
公共静态void main(字符串[]args){
SpringApplication.run(Application.class,args);
}
}

您可以在每个DAO类上使用
@Conditional
。 仅当使用
@Conditional
注释提到的条件满足时,才会在上下文中加载类。您可以有以下情况:

@ConditionalOnProperty(
    value="module.name", 
    havingValue = "module1", 
    matchIfMissing = false)
class DaoForModule1 {
当且仅当属性
module.name
具有值
module1
时,这将加载
DaoModule1
。如果要在未设置权限时加载此
DaoModule1
,可以将
matchIfMissing
更改为
true

您还可以使用
@Profile
注释来限制基于Profile加载的类

@Profile("module2")
class DaoForModule2 {

只有当活动配置文件列表中有
module2
时,才会加载
DaoForModule2
。但是我不喜欢概要文件,因为概要文件的用例是不同的。我们通常使用概要文件来指定基于环境的可变资源。

您可以在每个DAO类上使用
@Conditional
。 仅当使用
@Conditional
注释提到的条件满足时,才会在上下文中加载类。您可以有以下情况:

@ConditionalOnProperty(
    value="module.name", 
    havingValue = "module1", 
    matchIfMissing = false)
class DaoForModule1 {
当且仅当属性
module.name
具有值
module1
时,这将加载
DaoModule1
。如果要在未设置权限时加载此
DaoModule1
,可以将
matchIfMissing
更改为
true

您还可以使用
@Profile
注释来限制基于Profile加载的类

@Profile("module2")
class DaoForModule2 {

只有当活动配置文件列表中有
module2
时,才会加载
DaoForModule2
。但是我不喜欢概要文件,因为概要文件的用例是不同的。我们通常使用概要文件来指定基于环境的可变资源。

根据您的问题,我假设您希望在其他几个Spring项目中重用具有多个存储库和JPA实体对象(可能属于不同的数据源)的Spring DAO项目。您更愿意只加载一组特定的JPA实体/回购。第一步是将相关实体和存储库组织到不同的包中,并将此项目包括在其他项目的路径中

假设您已将存储库和实体分离到不同的包中,这是处理此问题的一种方法。创建您自己的配置bean,该配置bean将使用它所需的特定包和数据源实例化JPA EntityManagerFactorybean。在下面的代码中,下面的EntityManagerFactory将从
MODEL\u包
加载实体,并从
repositories\u包
加载存储库

@Configuration
@ComponentScan(basePackages = MODEL_PACKAGE)
@EnableJpaRepositories(basePackages = REPOSITORIES_PACKAGE,
        entityManagerFactoryRef = "ENTITY_MANAGER_FACTORY")
@EnableTransactionManagement
public class PersistenceConfig {

    public static final String MODEL_PACKAGE = "Your model package";
    public static final String REPOSITORIES_PACKAGE = "Your repository package";

        public static final String ENTITY_MANAGER_FACTORY = "entity_manager_factory";
    public static final String TRANSACTION_MANAGER = "transaction_manager";

    @Autowired //This is to get your property file entries (DB connection, etc).
    private Environment environment;

    @Bean(DATA_SOURCE)
    public DataSource dataSource() {
        //Create your datasource from environment properties.  Example - org.apache.tomcat.jdbc.pool.DataSource
    }

    @Bean(ENTITY_MANAGER_FACTORY) @Autowired
    public LocalContainerEntityManagerFactoryBean entityManagerFactory(
            @Qualifier(DATA_SOURCE) DataSource dataSource) throws IllegalStateException  {
        LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();
        entityManagerFactoryBean.setDataSource(dataSource);
        Properties jpaProperties = new Properties();
        // set properties for your JPA, for example, hibernate.dialect, hibernate.format_sql, etc.

        entityManagerFactoryBean.setJpaProperties(jpaProperties);
        entityManagerFactoryBean.setJpaVendorAdapter(new HibernateJpaVendorAdapter());
        entityManagerFactoryBean.setPackagesToScan(MODEL_PACKAGE);
    }

    @Bean(TRANSACTION_MANAGER) @Autowired
    @Primary
    @Qualifier(value = "transactionManager")
    public JpaTransactionManager transactionManager(
            @Qualifier(ENTITY_MANAGER_FACTORY) EntityManagerFactory entityManagerFactory) {
        JpaTransactionManager transactionManager = new JpaTransactionManager();    
        transactionManager.setEntityManagerFactory(entityManagerFactory);    
        return transactionManager;

根据您的问题,我假设您希望在其他几个Spring项目中重用具有多个存储库和JPA实体对象(可能属于不同的数据源)的Spring DAO项目。您更愿意只加载一组特定的JPA实体/回购。第一步是将相关实体和存储库组织到不同的包中,并将此项目包括在其他项目的路径中

假设您已将存储库和实体分离到不同的包中,这是处理此问题的一种方法。创建您自己的配置bean,该配置bean将使用它所需的特定包和数据源实例化JPA EntityManagerFactorybean。在下面的代码中,EntityManagerFactory