• springboot 简单样例配置


    1 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>2.1.3.RELEASE</version>
            <relativePath/> <!-- lookup parent from repository -->
        </parent>
        <groupId>com.example.astest</groupId>
        <artifactId>demo</artifactId>
        <version>0.0.2</version>
        <name>demo</name>
        <packaging>jar</packaging>
        <description>Demo project for Spring Boot</description>
    
        <properties>
            <junit.platform.version>1.3.2</junit.platform.version>
            <junit.jupiter.version>5.3.2</junit.jupiter.version>
            <junit.vintage.version>5.3.2</junit.vintage.version>
            <jmockit.version>1.46</jmockit.version>
            <java.version>1.8</java.version>
            <powermock-version>2.0.0-beta.5</powermock-version>
            <jmockit.version>1.46</jmockit.version>
        </properties>
    
        <dependencies>
            <dependency>
                <groupId>org.jmockit</groupId>
                <artifactId>jmockit</artifactId>
                <version>${jmockit.version}</version>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>org.junit.jupiter</groupId>
                <artifactId>junit-jupiter-engine</artifactId>
                <version>${junit.jupiter.version}</version>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>org.junit.platform</groupId>
                <artifactId>junit-platform-runner</artifactId>
                <version>${junit.platform.version}</version>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>org.junit.platform</groupId>
                <artifactId>junit-platform-console-standalone</artifactId>
                <version>${junit.platform.version}</version>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>org.junit.jupiter</groupId>
                <artifactId>junit-jupiter-api</artifactId>
                <version>${junit.jupiter.version}</version>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>org.junit.vintage</groupId>
                <artifactId>junit-vintage-engine</artifactId>
                <version>${junit.jupiter.version}</version>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>com.h2database</groupId>
                <artifactId>h2</artifactId>
                <version>1.4.197</version>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
    
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-devtools</artifactId>
                <optional>true</optional>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>org.glassfish</groupId>
                <artifactId>javax.json</artifactId>
                <version>1.0.4</version>
            </dependency>
            <dependency>
                <groupId>org.junit.jupiter</groupId>
                <artifactId>junit-jupiter-api</artifactId>
                <version>5.4.0</version>
                <scope>test</scope>
            </dependency>
    
    
            <dependency>
                <groupId>org.mockito</groupId>
                <artifactId>mockito-core</artifactId>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>org.powermock</groupId>
                <artifactId>powermock-api-mockito2</artifactId>
                <version>${powermock-version}</version>
                <scope>test</scope>
            </dependency>
    
            <dependency>
                <groupId>org.powermock</groupId>
                <artifactId>powermock-module-junit4</artifactId>
                <version>${powermock-version}</version>
                <scope>test</scope>
            </dependency>
    
            <dependency>
                <groupId>org.powermock</groupId>
                <artifactId>powermock-core</artifactId>
                <version>${powermock-version}</version>
                <scope>test</scope>
            </dependency>
    
            <dependency>
                <groupId>org.powermock</groupId>
                <artifactId>powermock-module-junit4-rule</artifactId>
                <version>${powermock-version}</version>
                <scope>test</scope>
            </dependency>
    
    
            <dependency>
                <groupId>com.rabbitmq</groupId>
                <artifactId>amqp-client</artifactId>
                <version>5.6.0</version>
            </dependency>
            <dependency>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-api</artifactId>
                <version>1.7.26</version>
            </dependency>
            <dependency>
                <groupId>org.slf4j</groupId>
                <artifactId>jcl-over-slf4j</artifactId>
                <version>1.7.26</version>
            </dependency>
    
            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
                <version>1.18.4</version>
            </dependency>
            <dependency>
                <groupId>ch.qos.logback</groupId>
                <artifactId>logback-classic</artifactId>
                <version>1.2.3</version>
            </dependency>
            <dependency>
                <groupId>ch.qos.logback</groupId>
                <artifactId>logback-core</artifactId>
                <version>1.2.3</version>
            </dependency>
            <dependency>
                <groupId>org.logback-extensions</groupId>
                <artifactId>logback-ext-spring</artifactId>
                <version>0.1.5</version>
            </dependency>
            <dependency>
                <groupId>net.logstash.logback</groupId>
                <artifactId>logstash-logback-encoder</artifactId>
                <version>4.8</version>
    
            </dependency>
            <dependency>
                <groupId>com.google.guava</groupId>
                <artifactId>guava</artifactId>
                <version>27.1-jre</version>
            </dependency>
            <dependency>
                <groupId>org.assertj</groupId>
                <artifactId>assertj-core</artifactId>
                <version>3.11.1</version>
                <scope>compile</scope>
            </dependency>
            <dependency>
                <groupId>dom4j</groupId>
                <artifactId>dom4j</artifactId>
                <version>1.6.1</version>
            </dependency>
    
    
    
            <dependency>
                <groupId>commons-collections</groupId>
                <artifactId>commons-collections</artifactId>
                <version>3.2.2</version>
            </dependency>
            <dependency>
                    <groupId>org.apache.poi</groupId>
                    <artifactId>poi</artifactId>
                    <version>3.10-FINAL</version>
                 </dependency>
                <dependency>
                    <groupId>org.apache.poi</groupId>
                 <artifactId>poi-ooxml</artifactId>
                 <version>3.10-FINAL</version>
             </dependency>
    
        </dependencies>
    
    
        <build>
            <finalName>dong</finalName>
            <plugins>
                <!-- The configuration of maven-jar-plugin -->
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-assembly-plugin</artifactId>
                    <configuration>
                        <descriptors>
                            <descriptor>src/main/resources/assembly/package.xml</descriptor>
                        </descriptors>
                    </configuration>
                </plugin>
                <plugin>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <configuration>
                        <testFailureIgnore>true</testFailureIgnore>
                        <argLine>
                            -javaagent:${settings.localRepository}/org/jmockit/jmockit/${jmockit.version}/jmockit-${jmockit.version}.jar
                            -Dcoverage-output=serial
                            -Dcoverage-metrics=all
                            -Dcoverage-check=10
                        </argLine>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    
    
    </project>

    pom里大多是测试+log 还有谷歌guava plugin里有定制打包的插件,指向路径作为定制化文件/assembly/package.xml

    package文件

    <?xml version="1.0" encoding="UTF-8"?>
    <assembly
            xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0"
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd
    http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 ">
        <id>jar-with-dependencies</id>
        <formats>
            <format>jar</format>
        </formats>
        <includeBaseDirectory>false</includeBaseDirectory>
        <dependencySets>
            <dependencySet>
                <useProjectArtifact>true</useProjectArtifact>
                <outputDirectory>/lib</outputDirectory>
                <unpack>false</unpack>
                <scope>runtime</scope>
            </dependencySet>
        </dependencySets>
        <fileSets>
            <fileSet>
                <directory>${project.build.outputDirectory}</directory>
                <outputDirectory>/</outputDirectory>
            </fileSet>
        </fileSets>
    </assembly>

    3 applycation.properties 这个里配置是项目网址,还加了执行项目弹网页,配合4  第二项是Boolean开关

    server.port=8002
    spring.auto.openurl=false
    spring.web.loginurl=http://127.0.0.1:8002
    spring.web.googleexcute=C:\Users\xx\Chrome\Application\chrome.exe

    4弹网页相关代码

    package com.example.astest.demo.config;
    
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.boot.CommandLineRunner;
    import org.springframework.stereotype.Component;
    
    
    @Component
    public class MyCommandRunner implements CommandLineRunner {
        private static Logger logger = LoggerFactory.getLogger(MyCommandRunner.class);
        @Value("${spring.web.loginurl}")
        private String loginUrl;
    
        @Value("${spring.web.googleexcute}")
        private String googleExcutePath;
    
        @Value("${spring.auto.openurl}")
        private boolean isOpen;
    
        @Override
        public void run(String... args) throws Exception {
            if(isOpen){
                String cmd = googleExcutePath +" "+ loginUrl;
                Runtime run = Runtime.getRuntime();
                try{
                    run.exec(cmd);
                    logger.debug("启动浏览器打开项目成功");
                }catch (Exception e){
                    e.printStackTrace();
                    logger.error(e.getMessage());
                }
            }
        }
    }

    5 一个主启动类

    package com.example.astest.demo;
    
    import com.example.astest.demo.diachieve.TheInterfaceClass;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.context.annotation.AnnotationConfigApplicationContext;
    import org.springframework.context.annotation.ComponentScan;
    import org.springframework.context.annotation.PropertySource;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    
    
    
    
    
    @PropertySource("application.properties")
    @SpringBootApplication
    public class DemoApplication {
        public static void main(String[] args) {
            SpringApplication.run(DemoApplication.class, args);
        }
    
    }

    6 controller 层 配置的是主页

    package com.example.astest.demo.controller;
    
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    
    @RestController
    public class OpenPage {
        @RequestMapping("/")
        String index() {
            return "Hello Spring cqweqweqwe1212ccc";
        }
    }

    7 附赠log back 简单配置文件 

    <configuration>
     
      <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <!-- encoders are assigned the type
             ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->
        <encoder>
          <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
        </encoder>
      </appender>
     
      <root level="debug">
        <appender-ref ref="STDOUT" />
      </root>
    </configuration>
  • 相关阅读:
    推荐文章:深入浅出REST
    推荐Fowler作序的新书《xUnit Test Patterns》
    测试替身:Test Double
    踢毽也能治胃病,适当的运动带来健康,健康带来快乐
    10分钟入门AOP:用PostSharp普及一下AOP
    推荐一本新的英文版算法书和一本讲debug的书
    emacs开发rails的演示
    转:一个土木工程师在四川地震灾后的思考
    多语言多范型编程PPP
    巧用editplus学习正则表达式
  • 原文地址:https://www.cnblogs.com/funkboy/p/12028989.html
Copyright © 2020-2023  润新知