我将制作一个Spring MVC web应用程序,其中应用程序是垂直分离的。每个模块都将封装与同一功能相关的功能。
让我们考虑一个假想的简单例子:
- feature-1:用户可以添加/删除/编辑笛卡尔坐标(x,y(
- 功能-2:在平面图中显示坐标
所以我在这里考虑的垂直分离是特征-1的一个模块和特征-2的另一个模块。
- 模块1:包括用于添加/删除/编辑的实体、持久性和视图
- 模块2:包括从持久性读取协调的服务和图表视图
我还考虑了另一个模块,它只是索引页和一个加载该索引页的控制器,它只是其他模块在其中加载视图的容器。此索引页将有一个导航栏,用于添加坐标和显示图表。这些导航应将控制权移交给另一个模块中的另一个控制器。
我构建了这个项目结构:
─── project
│
├── add-coordinate
│ ├── src
│ │ └── main
│ │ ├── java
│ │ │ └── com.example.coordinate.add
│ │ │ ├── controller
│ │ │ │ └── CoordinateController.java
│ │ │ ├── persistence
│ │ │ │ └── CoordinateRepository.java
│ │ │ └── entity
│ │ │ └── coordinate.java
│ │ └── resources
│ │ └── static
│ │ ├── css
│ │ │ └── style.css
│ │ └── templates
│ │ └── coordinate.html
│ └── pom.xml
│
├── show-coordinate
│ ├── src
│ │ └── main
│ │ ├── java
│ │ │ └── com.example.coordinate.show
│ │ │ ├── controller
│ │ │ │ └── ShowCoordinateController.java
│ │ │ └── service
│ │ │ └── CoordinateService.java
│ │ └── resources
│ │ └── static
│ │ ├── css
│ │ │ └── style.css
│ │ └── templates
│ │ └── showCoordinate.html
│ └── pom.xml
│
├── index-coordinate
│ ├── src
│ │ └── main
│ │ ├── java
│ │ │ └── com.example.coordinate.index
│ │ │ ├── controller
│ │ │ │ └── IndexController.java
│ │ │ └── IndexApplication.java
│ │ └── resources
│ │ └── static
│ │ ├── css
│ │ │ └── style.css
│ │ └── templates
│ │ └── index.html
│ └── pom.xml
│
└── pom.xml
这是父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">
<groupId>com.example</groupId>
<artifactId>coordinate</artifactId>
<version>${revison}</version>
<packaging>pom</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.5</version>
</parent>
<properties>
...
<!-- Project Version -->
<build.type>SNAPSHOT</build.type>
<variance.version>1.0.0</variance.version>
<revison>${variance.version}-${build.type}</revison>
</properties>
<modules>
<module>index-coordinate</module>
<module>add-coordinate</module>
<module>show-coordinate</module>
</modules>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
这是添加坐标pom:
<?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>
<artifactId>add-coordinate</artifactId>
<parent>
<groupId>com.example</groupId>
<artifactId>coordinate</artifactId>
<version>${revison}</version>
</parent>
<properties>
<start-class>com.example.coordinate.index.IndexApplication</start-class>
</properties>
<dependencies>
<dependency>-->
<groupId>com.example</groupId>
<artifactId>coordinate-index</artifactId>
<version>${revison}</version>
</dependency>
<!--other dependecies like Spring Boot, Sprong MVC, DB, ...-->
...
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
这是添加坐标的控制器:
@Controller
public class CoordinateController {
@RequestMapping("/coordinate")
public String addCoordinatePage() {
return "coordinate";
}
}
现在,索引页面已正确加载,但问题是,当单击添加坐标的导航栏并将请求发送到http://localhost:8080/coordinate
时,页面无法解决。这意味着我的应用程序尚未启动控制器。有人知道我该怎么解决这个问题吗?
通过向IndexApplication
添加以下注释解决的问题
@SpringBootApplication
@ComponentScan(basePackages = {"com.example.coordinate.*"})
@EnableJpaRepositories(basePackages = {"com.example.coordinate.*"})
@EntityScan(basePackages = {"com.example.coordinate.*"})
public class IndexApplication {
public static void main(String args[]) {
SpringApplication.run(Application.class);
}
}
并添加