Java 弹簧靴筒';多模块t扫描站
我使用spring boot,并通过多个模块进行设计。以下是我的项目结构: 模块车间核心: 包名称:com.baotrung.core.business 我设计了一些子包:模型、存储库、服务 Maven:Java 弹簧靴筒';多模块t扫描站,java,spring,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>
<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>
但当我启动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.
我不明白为什么,因为我在分类存储库中添加了一个名为“@存储库”的选项,如上面模块com.baotrung.core.business.repositories.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);
}
}
我建议您清理/重组您的包层次结构以使其正常工作。 它应该可以在没有任何附加注释的情况下工作,请检查我为给定示例创建测试的github: 如果将应用程序类放在应用程序的根包中,例如
com.baotrung.shop
,则组件扫描将从此包开始向下。所有其他组件都应该驻留在这个或子包中,这样做会更容易,并且您需要的样板代码也会更少。
将应用程序类放在并行包(及以下)中到其他组件(如您所做的)将强制您设置组件扫描的路径,以查找这些(以及未来)未按预期工作的组件。问题类似,您检查了吗?让我检查一下。非常感谢从公共接口类别中删除CategoryRepositoryCustom Repository扩展了CrudeRepository,CategoryRepositoryCustom并检查它是否可以不,这对我没有帮助。谢谢你。当我在Application.class中添加:@enableJParepositions(basePackages=“com.baotrung.*)时,它会引发异常:java.lang.IllegalArgumentException:不是托管类型:class com.baotrung.core.model.catalog.category.category,然后我添加@EntityScan(“com.baotrung.*),它会工作。谢谢
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
}
<?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>
@SpringBootApplication(scanBasePackages = {"com.baotrung.core.business","com.baotrung.*"})
public class Application extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(Application.class,args);
}
}
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.
@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);
}
}