【问题标题】:Spring Boot @Repository defined in external jar is not @Autowired外部 jar 中定义的 Spring Boot @Repository 不是 @Autowired
【发布时间】:2020-07-24 04:21:56
【问题描述】:

我创建了两个项目

  1. 普通实体项目提供实体和存储库,并作为包部署在 GitHub 中。

  2. 示例项目使用通用实体项目包作为依赖

但存储库不是@Autowired,并且在我的第二个项目中没有生成实体表。项目配置如下。

通用实体项目pom

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
    https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.6.RELEASE</version>
        <relativePath/>
    </parent>

    <groupId>com.orbund</groupId>
    <artifactId>galactic-be-common-entities</artifactId>
    <version>0.0.13</version>
    <packaging>jar</packaging>
    <name>galactic-be-common-entities</name>
    <description>Orbund galactic backend common entities project for School Information 
    System</description>

    <properties>
        <java.version>1.8</java.version>
        <github.global.server>github</github.global.server>
    </properties>

    <profiles>
        <profile>
            <id>develop</id>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <properties>
                <spring.profiles.active>develop</spring.profiles.active>
            </properties>
        </profile>
        <profile>
            <id>release</id>
            <properties>
                <spring.profiles.active>release</spring.profiles.active>
            </properties>
        </profile>
    </profiles>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.liquibase</groupId>
            <artifactId>liquibase-core</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.liquibase</groupId>
            <artifactId>liquibase-maven-plugin</artifactId>
            <version>3.4.1</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <id>repackage</id>
                        <configuration>
                            <classifier>exec</classifier>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.liquibase</groupId>
                <artifactId>liquibase-maven-plugin</artifactId>
                <version>3.4.1</version>
                <configuration>
                    <propertyFile>src/main/resources/liquibase.properties</propertyFile>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <distributionManagement>
        <repository>
            <id>github</id>
            <name>Galactic Maven Packages</name>
            <url>https://maven.pkg.github.com/shamim-orbund/mvnrepo</url>
        </repository>
    </distributionManagement>

</project>

我已将存储库配置为

@Configuration
@EnableJpaRepositories("com.orbund.galactic.be.common.entities.repository")
@EntityScan(basePackages = {"com.orbund.galactic.be.common.entities.entity"})
public class RepositoryConfig {

}

第二个项目pom文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
    https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <groupId>com.orbund</groupId>
    <artifactId>galactic-be-common-sample</artifactId>
    <version>0.0.1</version>
    <name>galactic-be-common-sample</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>com.orbund</groupId>
            <artifactId>galactic-be-common-entities</artifactId>
            <version>0.0.13</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.9.2</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <requiresUnpack>
                        <dependency>
                            <groupId>com.orbund</groupId>
                            <artifactId>galactic-be-common-entities</artifactId>
                            <version>0.0.13</version>
                        </dependency>
                    </requiresUnpack>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

感谢任何建议和帮助。提前致谢。

【问题讨论】:

  • 您可以将 spring 日志记录更改为调试以查看发生了什么
  • 我得到 java.lang.ClassNotFoundException: org.springframework.data.jpa.repository.JpaRepository @efekctive
  • 现在你知道根本原因是什么
  • @efekctive 我知道根本原因,如果它是 @EnableJpaRepositories("com.orbund.galactic.be.common.entities.repository") 的单个项目并且我之前配置过它是可以解决的.但是存储库在另一个项目中,并且外部 jar 在另一个项目中用作依赖项并且它不起作用。
  • 如果stackoverflow.com/questions/5269450/… 不能帮助你,Spring 将无法处理你的注解组件。他们需要在路径中。

标签: spring-boot


【解决方案1】:

最后,我通过将第一个项目应用程序导入到第二个项目应用程序解决了这个问题。现在 spring 会根据需要扫描实体和存储库类路径并生成 bean。

@SpringBootApplication
@Import(GalacticBeCommonEntitiesApplication.class)
public class GalacticBeCommonSampleApplication {
    public static void main(String[] args) {
        SpringApplication.run(GalacticBeCommonSampleApplication.class, args);
    }
}

【讨论】:

    猜你喜欢
    • 2019-03-15
    • 2019-06-12
    • 2017-04-09
    • 1970-01-01
    • 2015-11-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-05
    相关资源
    最近更新 更多