SpringBoot配置

Stella981
• 阅读 466

多模块Maven项目

.gitignore文件

.idea*.imltargetoutlogtmptest

父模块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>    <groupId>com.xx.x1</groupId>    <artifactId>x1</artifactId>

    <version>1.0-SNAPSHOT</version>    <modules>        <module>api</module>        <module>common</module>        <module>manager</module>        <module>web-base</module>    </modules>    <packaging>pom</packaging>    <parent>        <groupId>org.springframework.boot</groupId>        <artifactId>spring-boot-starter-parent</artifactId>        <version>2.1.1.RELEASE</version>    </parent>    <properties>        <java.version>1.8</java.version>        <maven.compiler.version>3.6.1</maven.compiler.version>        <maven.assembly.version>3.1.0</maven.assembly.version>    </properties>    <dependencies>        <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter-web</artifactId>        </dependency>        <dependency>            <groupId>junit</groupId>            <artifactId>junit</artifactId>            <scope>test</scope>        </dependency>        <dependency>            <groupId>org.mybatis.spring.boot</groupId>            <artifactId>mybatis-spring-boot-starter</artifactId>            <version>1.3.2</version>        </dependency>        <dependency>            <groupId>mysql</groupId>            <artifactId>mysql-connector-java</artifactId>        </dependency>        <dependency>            <groupId>org.projectlombok</groupId>            <artifactId>lombok</artifactId>            <version>1.18.4</version>        </dependency>        <dependency>            <groupId>org.apache.commons</groupId>            <artifactId>commons-lang3</artifactId>        </dependency>        <dependency>            <groupId>com.google.guava</groupId>            <artifactId>guava</artifactId>            <version>22.0</version>        </dependency>        <!-- https://mvnrepository.com/artifact/org.apache.poi/poi -->        <dependency>            <groupId>org.apache.poi</groupId>            <artifactId>poi</artifactId>            <version>3.17</version>        </dependency>        <!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml -->        <dependency>            <groupId>org.apache.poi</groupId>            <artifactId>poi-ooxml</artifactId>            <version>3.17</version>        </dependency>    </dependencies></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">    <parent>        <artifactId>xx</artifactId>        <groupId>com.xx.x4</groupId>        <version>1.0-SNAPSHOT</version>    </parent>    <modelVersion>4.0.0</modelVersion>    <artifactId>api</artifactId>    <dependencies>        <dependency>            <groupId>com.xx.x3</groupId>            <artifactId>web-base</artifactId>            <version>1.0-SNAPSHOT</version>        </dependency>    </dependencies>    <build>        <plugins>            <plugin>                <groupId>org.apache.maven.plugins</groupId>                <artifactId>maven-compiler-plugin</artifactId>                <version>${maven.compiler.version}</version>                <configuration>                    <source>${java.version}</source>                    <target>${java.version}</target>                </configuration>                <executions>                    <execution>                        <id>compile</id>                        <phase>compile</phase>                        <goals>                            <goal>compile</goal>                        </goals>                    </execution>                    <execution>                        <id>testCompile</id>                        <phase>test-compile</phase>                        <goals>                            <goal>testCompile</goal>                        </goals>                    </execution>                </executions>            </plugin>            <plugin>                <artifactId>maven-assembly-plugin</artifactId>                <version>${maven.assembly.version}</version>                <configuration>                    <descriptors>                        <descriptor>assembly/assembly.xml</descriptor>                    </descriptors>                </configuration>                <executions>                    <execution>                        <id>aobp-xingxi-api</id>                        <phase>package</phase>                        <goals>                            <goal>single</goal>                        </goals>                        <configuration>                            <finalName>aobp-xingxi-api</finalName>                            <appendAssemblyId>false</appendAssemblyId>                        </configuration>                    </execution>                </executions>            </plugin>        </plugins>    </build></project>

SpringBoot配置

启动类Boot

@SpringBootApplication(scanBasePackages = "com.xx.xxx.xxxx")@MapperScan(basePackages = "com.xx.xxx.xxxx.api.dao")@Slf4jpublic class Boot {    public static void main(String[] args) {        log.info("service starting");        SpringApplication.run(Boot.class, args).registerShutdownHook();        log.info("service start success");    }}yaml文件:

server:  port: 8081  tomcat:    basedir: tmp/tomcatspring:  datasource:    hikari:      minimum-idle: 16      maximum-pool-size: 32      driver-class-name: com.mysql.cj.jdbc.Driver      username:       password:     url: jdbc:mysql://localhost/test?useUnicode=true&characterEncoding=utf-8&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=Asia/Shanghaimybatis:  mapper-locations: classpath:mappers/*.xml  type-aliases-package: com.xx.xxx.xxxx.common.entitysave:(创建包)  dir:    user: tmp/user    package: tmp/package    img: tmp/imglogging:(debug时用于打印sql语句)  level:    com.xx.xxx.xxxx.api.dao: debug创建包时:

@Value("${save.dir.img}")private String imgDir;@PostConstructvoid init(){    File file = new File(imgDir);    if(!file.exists()){        file.mkdirs();    }}创建文件位置:

SpringBoot配置

各模块包名

SpringBoot配置

跨域配置:

/** * @ClassName: GlobalCorsConfig * @Description: 跨域配置 * @author: yaozhenhua * @date: 2018/12/27 15:07 */@Configurationpublic class GlobalCorsConfig {    @Bean    public CorsFilter corsFilter() {        //1.添加CORS配置信息        CorsConfiguration config = new CorsConfiguration();        //放行哪些原始域        config.setAllowedHeaders(CollectionUtils.arrayToList(new String[]{"*"}));        //是否发送Cookie信息        config.setAllowCredentials(true);        //放行哪些原始域(请求方式)        config.setAllowedMethods(CollectionUtils.arrayToList(new String[]{"POST", "GET", "PUT", "OPTIONS", "DELETE"}));        //放行哪些原始域(头部信息)        config.setAllowedOrigins(CollectionUtils.arrayToList(new String[]{"*"}));                //2.添加映射路径        UrlBasedCorsConfigurationSource configSource = new UrlBasedCorsConfigurationSource();        configSource.registerCorsConfiguration("/**", config);                //3.返回新的CorsFilter.        return new CorsFilter(configSource);    }}Controller层全局异常处理器:

@ControllerAdvice@Slf4jpublic class AobpExceptionHandler {    //声明要捕获的异常    @ExceptionHandler(Exception.class)    @ResponseBody    public Result defaultExceptionHandler(Exception e) {        if (e instanceof AobpException) {            log.error(((AobpException) e).getMsg());            AobpException aobpException = (AobpException) e;            return Result.error(aobpException.getCode(), aobpException.getMsg());        }        log.warn(e.getMessage());        return Result.error(StatusConstant.STATUS_INTERNAL_ERR);    }}
点赞
收藏
评论区
推荐文章
Easter79 Easter79
2年前
springboot2.0国际化
springboot2.0配合thymeleaf实现页面国际化1\.引入thymeleaf<?xmlversion"1.0"encoding"UTF8"?<projectxmlns"http://maven.apache.org/POM/4.0.0"xmlns:xsi"http:
Wesley13 Wesley13
2年前
java代码自动下载Spring Boot用户手册
本示例演示SpringBoot1.5.9.RELEASE版本的用户手册下载pom.xml<?xmlversion"1.0"encoding"UTF8"?<projectxmlns"http://maven.apache.org/POM/4.0.0"xmlns:xsi"http://w
Easter79 Easter79
2年前
springboot2.1整合mybatis
1:添加依赖<?xmlversion"1.0"encoding"UTF8"?<projectxmlns"http://maven.apache.org/POM/4.0.0"xmlns:xsi"http://www.w3.org/2001/XMLSchemainstance"xsi
Easter79 Easter79
2年前
SpringBoot配置
多模块Maven项目.gitignore文件.idea.imltargetoutlogtmptest父模块pom文件<?xmlversion"1.0"encoding"UTF8"?<projectxmlns"http://maven.apache.org/POM/4.0.0"xmlns:xs
Stella981 Stella981
2年前
SpringBoot2.0配置durid数据源
pom依赖<?xmlversion"1.0"encoding"UTF8"?<projectxmlns"http://maven.apache.org/POM/4.0.0"xmlns:xsi"http://www.w3.org/2001/XMLSchemainstance"
Stella981 Stella981
2年前
121 项目 034 笔记向 easyui
pom<?xmlversion"1.0"encoding"UTF8"?<projectxmlns"http://maven.apache.org/POM/4.0.0"xmlns:xsi"http://www.w3.org/2001/XMLSchemainstance"
可莉 可莉
2年前
121 项目 034 笔记向 easyui
pom<?xmlversion"1.0"encoding"UTF8"?<projectxmlns"http://maven.apache.org/POM/4.0.0"xmlns:xsi"http://www.w3.org/2001/XMLSchemainstance"
Stella981 Stella981
2年前
SpringBoot2.1.6 + Shiro1.4.1 + Thymeleaf + Jpa整合练习
  首先,添加maven依赖,完整的pom文件如下:1<?xmlversion"1.0"encoding"UTF8"?2<projectxmlns"http://maven.apache.org/POM/4.0.0"xmlns:xsi"http://www.w3.org/2001/XMLSchema
Easter79 Easter79
2年前
SpringBoot2.1.6 + Shiro1.4.1 + Thymeleaf + Jpa整合练习
  首先,添加maven依赖,完整的pom文件如下:1<?xmlversion"1.0"encoding"UTF8"?2<projectxmlns"http://maven.apache.org/POM/4.0.0"xmlns:xsi"http://www.w3.org/2001/XMLSchema
Easter79 Easter79
2年前
SpringBoot2.0配置durid数据源
pom依赖<?xmlversion"1.0"encoding"UTF8"?<projectxmlns"http://maven.apache.org/POM/4.0.0"xmlns:xsi"http://www.w3.org/2001/XMLSchemainstance"