gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 22:08:51 34 4
gpt4 key购买 nike

我使用 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/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com