java - Spring Boot 无法扫描多个模块的注释

标签 java spring spring-boot

我使用 Spring Boot 并通过多个模块进行设计。下面是我的项目结构:

模块商店核心: 包名称:com.baotrung.core.business 我设计了一些子包:模型、存储库、服务

Maven:

<modelVersion>4.0.0</modelVersion>

    <artifactId>shop-core</artifactId>
    <packaging>jar</packaging>

    <dependencies>
        <!-- shop-core-model !-->
        <dependency>
            <groupId>com.baotrung</groupId>
            <artifactId>shop-core-model</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>

        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.9</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>


    </dependencies>

类别服务

 public interface CategoryService {

    List<Object> countProductsByCategories(MerchantStore store, List<Long> categoryIds);

    List<Category> listByStoreAndParent(MerchantStore store, Category category);

    PersistableCategory saveCategories(MerchantStore store, PersistableCategory persistableCategory);

    Category findById(Long id);

    List<ReadableCategory> findCategories(MerchantStore store, int dept, Language language,List<String> filters);

}

类别服务实现

@Service
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    private CategoriesRepository categoryRepository;

    @Autowired
    private LanguageRepository languageRepository;

    @Autowired
    private Mapper<Category,ReadableCategory> categoryReadableCategoryMapper;

    //some method

@Repository

public interface CategoriesRepository extends CrudRepository<Category, Long>, CategoryRepositoryCustom {

 }

public interface CategoryRepositoryCustom {

    List<Object> countProductsByCategories(MerchantStore store, List<Long> categoryIds);

    List<Category> listByStoreAndParent(MerchantStore store, Category category);


    }

@Repository
public class CategoryRepositoryCustomImpl implements CategoryRepositoryCustom {

// some method impl
}

我还创建了模块 shopping-app 并使用了 shop-code 依赖项。看起来像:

Maven:

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>spring-shopping-project</artifactId>
        <groupId>com.baotrung</groupId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>shopping-app</artifactId>

    <dependencies>
        <dependency>
            <groupId>com.baotrung</groupId>
            <artifactId>shop-core-model</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>com.baotrung</groupId>
            <artifactId>shop-core</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>io.jsonwebtoken</groupId>
            <artifactId>jjwt</artifactId>
            <version>0.9.0</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project>

在模块 shopping-app 中,我创建包 com.baotrung.web 并创建一些子包:repository,服务模型...并创建名称为Application.class的应用程序,如下所示:

@SpringBootApplication(scanBasePackages = {"com.baotrung.core.business","com.baotrung.*"})
public class Application extends SpringBootServletInitializer {

    public static void main(String[] args) {
        SpringApplication.run(Application.class,args);
    }
}

但是当我启动Application.class时,它会抛出异常

Field categoryRepository in com.baotrung.core.business.services.CategoryServiceImpl required a bean of type 'com.baotrung.core.business.repositories.CategoriesRepository' that could not be found.

The injection point has the following annotations:
    - @org.springframework.beans.factory.annotation.Autowired(required=true)


Action:

Consider defining a bean of type 'com.baotrung.core.business.repositories.CategoriesRepository' in your configuration.

我不明白为什么,因为我在CategoriesRepository中添加了注释,并在模块com.baotrung.core.business.repositories中添加了@Repository .CategoriesRepository。为什么spring无法从com.baotrung.core.business.repositories找到我的bean,因为它是一个真正的定义。请帮我?

最佳答案

通过添加 @EnableJpaRepositories@EntityScan 对此进行更新:

@SpringBootApplication
@EntityScan(basePackages = {"com.baotrung.*"})
@EnableJpaRepositories(basePackages = {"com.baotrung.*"})
public class Application extends SpringBootServletInitializer {

    public static void main(String[] args) {
        SpringApplication.run(Application.class,args);
    }
}

关于java - Spring Boot 无法扫描多个模块的注释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58672323/

相关文章:

java - driver.manage().logs().get ("browser");再次调用时不起作用

java - 可执行Jar : Could not load main (but main is in ANT xml)

java - Spring 4.0.X-beans 中 springDefinitionBuilder 中 setSource 的替代方案

java - 如何解决: java. lang.NoSuchMethodError : javax. persistence.EntityManager.createStoredProcedureQuery(Ljava/lang/String;)

java - Spring Config 将字符串映射到对象

java - Spring Boot 多个 RabbitMQ 监听器到单个容器

java - Android:我的异步方法有效吗?

java - 如何将文本文件资源读入 Java 单元测试?

jsp - SpringBoot应用程序: Accessing Common application properties in a JSP page

java - 通过@Profile 启用 WebSecurityConfigurer 不起作用