IDEA社区版(IDEA Community Edition)创建Springboot父子项目

  1. 因为社区办不支持使用spring Spring Initializr 的方式创建项目, 但是我们可以考虑使用别的方式达到效果:

创建方式有3种:

第一种:使用https://start.spring.io/ 官方URL创建项目,再导入到 IDEA Community Edition(后面简称:ideaC)。具体使用自行百度。缺点:没办法自定义springboot的版本。

第二种:下载插件:Spring boot Assistant, 然后就可以按照商业版的方式创建。

第三种:也是我今天推荐使用的方式。用ideaC的方式来创建:

  1. 创建父工程:
  1. 右键父项目,新建第一个子模块idea-service:
  1. 同样的方式再创建一个子项目idea-stub:
  1. 修改父工程的pom文件,添加springboot-parent 依赖 及其你想添加的依赖:

    <?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>
    <packaging>pom</packaging>

     <modules>
         <module>idea-service</module>
         <module>idea-stub</module>
     </modules>
    
     <parent>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-parent</artifactId>
         <version>3.2.2</version>
         <relativePath/>
     </parent>
    
     <groupId>org.example</groupId>
     <artifactId>demo-IdeaC</artifactId>
     <version>1.0-SNAPSHOT</version>
    
     <properties>
         <maven.compiler.source>21</maven.compiler.source>
         <maven.compiler.target>21</maven.compiler.target>
         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
     </properties>
    
     <dependencies>
         <dependency>
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-starter</artifactId>
         </dependency>
    
         <dependency>
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-starter-test</artifactId>
             <scope>test</scope>
         </dependency>
     </dependencies>
    
    </project>
  2. 查看idea-stub的依赖有没问题,因为我设计stub项目是为了暴露服务的,暂时不需要添加其他jar包:

    <?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.example</groupId>
    <artifactId>demo-IdeaC</artifactId>
    <version>1.0-SNAPSHOT</version>
    </parent>

     <artifactId>idea-stub</artifactId>
    
     <properties>
         <maven.compiler.source>21</maven.compiler.source>
         <maven.compiler.target>21</maven.compiler.target>
         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
     </properties>
    
    </project>
  3. 查看idea-service的依赖,主要是spring-web的jar包,这是主服务端,需要负责项目启动,还要添加IdeaDemoApplication 启动类:

    <?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.example</groupId>
    <artifactId>demo-IdeaC</artifactId>
    <version>1.0-SNAPSHOT</version>
    </parent>

     <artifactId>idea-service</artifactId>
    
     <properties>
         <maven.compiler.source>21</maven.compiler.source>
         <maven.compiler.target>21</maven.compiler.target>
         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
     </properties>
    
     <dependencies>
         <!-- spring-boot启动相关依赖 -->
         <dependency>
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-starter-web</artifactId>
             <version>3.2.2</version>
         </dependency>
     </dependencies>
    
    </project>

6.1 还需要修改2个子项目的打包方式为jar,父工程确认为pom:

6.2: 添加IdeaDemoApplication 启动类,要修改ComponentScan()扫描的地方:

package org.example;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;

@EnableAsync
@Configuration
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
@ComponentScan({"org.example.*"})
public class IdeaDemoApplication {

	public static void main(String[] args) {
		SpringApplication.run(IdeaDemoApplication.class, args);
	}

}
  1. 如果不需要加载数据源的话,配置启动应用程序:

  2. 结果:

  3. 如果需要配置数据源的话,需要添加yml文件,@SpringBootApplication(exclude =)要取消排除数据源: