私が使用している春Initializrを二つのモジュールを作成するには:
API(春のWebです)
デシベル(春データMongoDBのです)
私は、彼らが今、サブモジュールのような方法で、同じプロジェクトに入れています。
テストを作成する前に、私のことができmvn clean install
BUILD成功メッセージを持ちます。問題ありませんが。
私はテストを作成(あるいは内のコントローラでそれを使用しようとする場合は、メイン構造)が使用する、デシベルのモジュールリポジトリクラスを、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
ApiApplication.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);
}
}
UserController.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;
}
}
さて、私が実行した場合mvn clean install
、私は言ってMavenのエラーが表示されるものとしますpackage com.example.db.repositories does not exist
。
モジュールのDBは、モジュール内の依存され、APIと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>
<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>
デシベル - の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>
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>
どのように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.
Springboot自動検出は、コンフィギュレーションクラスから下だけ下降します。あなたのアプリケーションがです
com.example.api
しかし、レポはです
com.example.db
どちらかだけでなく.DB自動検出またはcom.exampleまたはcom.example.apiにデシベルコードにあなたのアプリケーションクラスを移動するには、検索パスを追加
オプション1
@ComponentScan(“com.example”)
@SpringBootApplication
public class ExampleApplication {
オプション2
@ComponentScan({"com.example.api","com.example.db"})
@SpringBootApplication
public class ExampleApplication {
また、同じ効果のためにSpringbootApplication注釈にscanBasePackages属性を追加することができます。
@SpringBootApplication(scanBasePackages= {"com.example.api","com.example.db"})
public class ExampleApplication {
ここではドキュメントを参照してくださいhttps://docs.spring.io/spring-boot/docs/current/api/org/springframework/boot/autoconfigure/SpringBootApplication.html#scanBasePackages--