我使用Spring Initializr创建了两个模块:
api(即 Spring Web)
db(这是 Spring Data MongoDB)
我把它们放在同一个项目中,在某种程度上它们现在是子模块。
在创建测试之前,我可以mvn clean install使用 BUILD SUCCESS 消息。没有问题。
但是,如果我尝试创建一个测试(或者甚至在主结构中的控制器中使用它),它使用db模块存储库类从 MongoDB 获取数据,mvn clean install将指责包不存在,尽管 IntelliJ 可以识别和索引它和文件存在。
结构是:
api
\- src/main/java
\- com.example.api.controllers
\- UserController.java
\- src/test/java
\- com.example.api.controllers
\- UserControllerTest.java
db
\- src/main/java
\- com.example.db.repositories
\- UserRepository.java
model
\- src/main/java
\- com.example.model
\- User.java
Run Code Online (Sandbox Code Playgroud)
应用程序.java
package com.example.api;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.boot.web.servlet.server.ConfigurableServletWebServerFactory;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@SpringBootApplication
public class ApiApplication {
public static void main(String[] args) {
SpringApplication.run(ApiApplication.class, args);
}
}
Run Code Online (Sandbox Code Playgroud)
用户控制器.java
package com.example.api.controllers;
import com.example.api.Constants;
import com.example.db.repositories.UserRepository;
import com.example.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class UserController {
@Autowired
private UserRepository userRepository;
@GetMapping(Constants.V1 + "user")
public User getUser() {
User user = new User("John");
userRepository.save(user);
return user;
}
}
Run Code Online (Sandbox Code Playgroud)
现在,如果我运行mvn clean install,我将收到 Maven 错误提示package com.example.db.repositories does not exist。
模块db是模块api 中的一个依赖项,并且api模块设置为在父 pom 中的db之后编译。
父级 - pom.xml
<?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">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<modules>
<module>model</module>
<module>db</module>
<module>api</module>
</modules>
</project>
Run Code Online (Sandbox Code Playgroud)
数据库 - pom.xml
<?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">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.1.RELEASE</version>
<relativePath/>
</parent>
<groupId>com.example</groupId>
<artifactId>db</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.example</groupId>
<artifactId>model</artifactId>
<version>0.0.1-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
</dependencies>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties>
</project>
Run Code Online (Sandbox Code Playgroud)
api - pom.xml
<?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">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>LATEST</version>
<relativePath/>
</parent>
<groupId>com.example</groupId>
<artifactId>api</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<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>com.example</groupId>
<artifactId>db</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Run Code Online (Sandbox Code Playgroud)
如何让 maven 看到这个包?
找到包后出现新错误
2019-01-14 19:16:55.060 WARN 31104 --- [ main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userController': Unsatisfied dependency expressed through field 'userRepository'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.example.db.repositories.UserRepository' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
2019-01-14 19:16:55.066 INFO 31104 --- [ main] o.apache.catalina.core.StandardService : Stopping service [Tomcat]
2019-01-14 19:16:55.087 INFO 31104 --- [ main] ConditionEvaluationReportLoggingListener :
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2019-01-14 19:16:55.224 ERROR 31104 --- [ main] o.s.b.d.LoggingFailureAnalysisReporter :
***************************
APPLICATION FAILED TO START
***************************
Description:
Field userRepository in com.example.api.controllers.UserController required a bean of type 'com.example.db.repositories.UserRepository' 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.example.db.repositories.UserRepository' in your configuration.
Run Code Online (Sandbox Code Playgroud)
小智 7
您可以db使用以下方法(重新)打包为 Spring boot“应用程序”而不是库spring-boot-maven-plugin:
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
Run Code Online (Sandbox Code Playgroud)
该 jar 被重新打包,从而将com.example.db.repositories包(及其类)添加到 BOOT-INF 文件夹中。这会导致编译失败。
<plugin>..</plugin>只需从 中删除该部件即可db/pom.xml。这将创建一个可以在api模块中导入的常规 jar。
注意:我假设api有该类Main并将打包为启动应用程序。
Springboot 自动发现只会从您的配置类开始下降。您的申请位于
\n\ncom.example.api\nRun Code Online (Sandbox Code Playgroud)\n\n但回购协议位于
\n\ncom.example.db\nRun Code Online (Sandbox Code Playgroud)\n\n也可以添加搜索路径到 autodiscover .db 或将应用程序类移动到 com.example 或将 db 代码移动到 com.example.api
\n\n选项1
\n\n@ComponentScan(\xe2\x80\x9ccom.example\xe2\x80\x9d)\n@SpringBootApplication\npublic class ExampleApplication {\nRun Code Online (Sandbox Code Playgroud)\n\n选项2
\n\n@ComponentScan({"com.example.api","com.example.db"})\n@SpringBootApplication\npublic class ExampleApplication {\nRun Code Online (Sandbox Code Playgroud)\n\n您还可以将 scanBasePackages 属性添加到 SpringbootApplication 注解中以获得相同的效果。
\n\n@SpringBootApplication(scanBasePackages= {"com.example.api","com.example.db"})\npublic class ExampleApplication {\nRun Code Online (Sandbox Code Playgroud)\n\n\n
| 归档时间: |
|
| 查看次数: |
6589 次 |
| 最近记录: |