SpringBoot创建maven多模块项目
详细步骤:
file->new -> project
然后一路next即可,最后finish
最后我们看到创建后的项目只有项目名称和pom.xml就对了:
二:接下来创建我们的多模块mave子项目:
next -> finish
接下来可以看到我们的两个子模块就创建好了,并且这两个子模块的parent都是spring-security-auth
但是的话还差一点点,我们是通过springboot创建多模块的,是不是感觉还少了点啥??哈哈。。
对的,我们还需要一个子模块作为我们springboot项目的启动模块,对于springboot项目的一些配置和启动类是需要的
像上面添加子模块的步骤一样来到这个界面:
选择我们需要的一些依赖:
最后看到我们的项目结构:
最后还需要配置模块之间的依赖关系:
在我们的父pom中添加模块
父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> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.4.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.dengwei</groupId> <artifactId>spring-security-auth</artifactId> <version>0.0.1-SNAPSHOT</version> <name>spring-security-auth</name> <description>Demo project for Spring Boot</description> <modules> <module>springsecuritybasic</module> <module>securityjwtrestful</module> </modules> <properties> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> <maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion> <java.version>1.8</java.version> </properties> <dependencies> <!--操作数据库--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <!-- Druid 数据库连接池 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.1.5</version> </dependency> <!--mysql连接--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.1</version> </dependency> <!-- MyBatis 分页插件 --> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper-spring-boot-starter</artifactId> <version>1.2.10</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--配置文件xml或properties处理器--> <dependency> <groupId> org.springframework.boot </groupId> <artifactId> spring-boot-configuration-processor </artifactId> <optional>true</optional> </dependency> <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> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency> <dependency> <groupId>org.springframework.security</groupId> <artifactId>spring-security-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>
接下来我们看一下关于启动类的依赖以及配置:
因为刚才我们创建starter模块并没有把spring-security-auth作为我们的父类,所以需要改一下,即把parent更改成
作为一个启动子模块那么肯定需要依赖于所有的其他子模块,即上面截图的依赖信息
starter模块的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> <parent> <artifactId>spring-security-auth</artifactId> <groupId>com.dengwei</groupId> <version>0.0.1-SNAPSHOT</version> </parent> <groupId>com.dengwei</groupId> <artifactId>starter</artifactId> <version>0.0.1-SNAPSHOT</version> <name>starter</name> <description>Demo project for Spring Boot</description> <dependencies> <dependency> <groupId>com.dengwei</groupId> <artifactId>security-jwt-restful</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> <dependency> <groupId>com.dengwei</groupId> <artifactId>spring-security-basic</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
最后在我们的启动类加上扫描包的注解即可:
接下来我们看一下关于yml的配置(此配置是基于springboot1.5)
详细配置:
server: port: 9000 context-path: '/crm' connection-timeout: 600000 #mybatis扫描的文件夹 mybatis: mapper-locations: classpath*:mapper/*Mapper.xml configuration: database-id: MySQL spring: activiti: database-schema-update: true http: multipart: max-file-size: 100MB max-request-size: 1000MB --- spring: profiles: dev datasource: druid: driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://192.168.2.241:3306/crm-dev?useSSL=false&characterEncoding=utf8&autoReconnect=true&failOverReadOnly=false username: root password: ** filters: stat # 配置初始化大小、最小、最大 maxActive: 20 initialSize: 1 #配置获取连接等待超时的时间 maxWait: 60000 minIdle: 1 #配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 timeBetweenEvictionRunsMillis: 6000 #配置一个连接在池中最小生存的时间,单位是毫秒 minEvictableIdleTimeMillis: 30000 #测试连接 validationQuery: select 'x' #申请连接的时候检测,建议配置为true,不影响性能,并且保证安全性 testWhileIdle: true #获取连接时执行检测,建议关闭,影响性能 testOnBorrow: false #归还连接时执行检测,建议关闭,影响性能 testOnReturn: false #是否开启PSCache,PSCache对支持游标的数据库性能提升巨大,oracle建议开启,mysql下建议关闭 poolPreparedStatements: true #开启poolPreparedStatements后生效 maxOpenPreparedStatements: 20 #配置扩展插件,常用的插件有=>stat:监控统计 log4j:日志 wall:防御sql注入 filter: stat: log-slow-sql: true slow-sql-millis: 2000 --- spring: profiles: test datasource: druid: driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://192.168.2.241:3306/crm-test?useSSL=false&characterEncoding=utf8&autoReconnect=true&failOverReadOnly=false username: root password: ** filters: stat maxActive: 20 initialSize: 1 maxWait: 60000 minIdle: 1 timeBetweenEvictionRunsMillis: 6000 minEvictableIdleTimeMillis: 30000 validationQuery: select 'x' testWhileIdle: true testOnBorrow: false testOnReturn: false poolPreparedStatements: true maxOpenPreparedStatements: 20
最后关于日志的配置:
logback-spring.xml
<?xml version="1.0" encoding="UTF-8"?> <configuration scan="true" scanPeriod="60 seconds" debug="false"> <include resource="org/springframework/boot/logging/logback/defaults.xml"/> <property name="LOG_FILE_NAME_PATTERN" value="logs/auth.%d{yyyy-MM-dd}.%i.log"/> <!-- 日志格式 --> <property name="CONSOLE_LOG_PATTERN" value="%clr(%d{${LOG_DATEFORMAT_PATTERN:-yyyy-MM-dd HH:mm:ss.SSS}}){faint} %clr(${LOG_LEVEL_PATTERN:-%5p}) %clr(${PID:- }){magenta} %clr(---){faint} %clr([%15.15t]){faint} %clr(%c){cyan} %clr(:){faint} %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}"/> <property name="FILE_LOG_PATTERN" value="%d{${LOG_DATEFORMAT_PATTERN:-yyyy-MM-dd HH:mm:ss.SSS}} ${LOG_LEVEL_PATTERN:-%5p} ${PID:- } --- [%t] %c : %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}"/> <!--输出到控制台--> <appender name="console" class="ch.qos.logback.core.ConsoleAppender"> <encoder> <pattern>${CONSOLE_LOG_PATTERN}</pattern> </encoder> </appender> <!--输出到文件--> <appender name="file" class="ch.qos.logback.core.rolling.RollingFileAppender"> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <fileNamePattern>${LOG_FILE_NAME_PATTERN}</fileNamePattern> <!-- 日志保留天数 --> <maxHistory>366</maxHistory> <!-- 日志文件上限大小,达到指定大小后删除旧的日志文件 --> <totalSizeCap>2GB</totalSizeCap> <!-- 每个日志文件的最大值 --> <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP"> <maxFileSize>10MB</maxFileSize> </timeBasedFileNamingAndTriggeringPolicy> </rollingPolicy> <encoder> <pattern>${FILE_LOG_PATTERN}</pattern> </encoder> </appender> <!-- region 根据不同的环境设置不同的日志输出级别 --> <springProfile name="default,local"> <root level="info"> <appender-ref ref="console"/> </root> <logger name="com.zhl" level="debug"/> </springProfile> <springProfile name="dev,test"> <root level="info"> <appender-ref ref="console"/> <appender-ref ref="file"/> </root> <logger name="com.zhl" level="debug"/> </springProfile> <springProfile name="product,pre"> <root level="info"> <appender-ref ref="console"/> <appender-ref ref="file"/> </root> <logger name="com.zhl" level="debug"/> </springProfile> <!-- endregion --> </configuration>
好的到此我们的多模块项目就搭建完成啦,是不是很简单