${artifactId}.jar start 启动boot工程

134 阅读1分钟
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                	<!--默认为false,只能通过jar -jar 的方式启动-->
		           <!--设置为true,则可以通过 app.jar start的方式启动-->
                    <executable>true</executable>
                </configuration>
            </plugin>
        </plugins>
package com.example.demojar;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.UUID;

@SpringBootApplication
@RestController
public class DemoJarApplication {

    /**
     * ./target/demo-jar-0.0.1-SNAPSHOT.jar start --test.name=hell
     *
     * 打印结果:
     * 获取到的属性名称为:hell
     * true
     * @param args
     */
    public static void main(String[] args) {
        ConfigurableApplicationContext context = SpringApplication.run(DemoJarApplication.class, args);
        ConfigurableEnvironment environment = context.getEnvironment();
        String property = environment.getProperty("test.name");
        System.out.println("获取到的属性名称为:" + property);
        System.out.println(environment.containsProperty("test.name"));
    }

    @RequestMapping("/test")
    public String test() {
        return UUID.randomUUID().toString();
    }
}