springboot maven配置文件外置


springboot maven配置文件外置

                                  

                                             

*******************

配置文件默认加载方式

                    

springboot 应用启动时,会自动从以下位置加载配置文件

          

类路径:类路径根目录、类路径下的config目录

项目路径:项目路径、项目路径下的config目录、项目路径下config目录的直接子目录

                       

                           

*******************

示例

                      

****************

配置文件

                 

application.yml

person:
  name: gtlx
  age: 20

                        

****************

pojo 层

                

Person

@Data
@Component
@ConfigurationProperties("person")
public class Person {

    private String name;
    private Integer age;
}

                            

****************

controller 层

                 

HelloController

@RestController
public class HelloController {

    @Resource
    private Person person;

    @RequestMapping("/hello")
    public String hello(){
        System.out.println(person);

        return "success";
    }
}

                            

                           

*******************

配置文件默认打包              

                   

pom.xml(build部分)

    <build>
        <finalName>hello</finalName>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>

                      

maven 打包

                         

                         

解压 hello.jar

               

                         

配置文件存放在BOOT-INF/classes目录下

                

                          

                                

*******************

配置文件存放在jar包外

              

pom.xml(build部分)

    <build>
        <finalName>hello</finalName>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>

        <resources>
            <resource>
                <targetPath>${build.directory}/config</targetPath>
                <directory>src/main/resources</directory>
                <includes>
                    <include>*.properties</include>
                </includes>
            </resource>
        </resources>
    </build>

                   

maven 打包

                         

配置文件在target/config目录中

                             

解压 hello.jar

                                               

                      

BOOT-INF/classes目录下没有配置文件

               

                         

读取jar包外配置文件测试

# terminal 跳转到target目录
E:\java\IdeaProjects\springboot maven 配置文件>cd target

# 运行jar包, localhost:8080/hello,控制台输出
E:\java\IdeaProjects\springboot maven 配置文件\target>java -jar hello.jar

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::                (v2.5.5)

2021-09-30 19:05:51.776  INFO 15740 --- [           main] com.example.demo.DemoApplication         : Starting DemoApplication v0.0.1-SN
APSHOT using Java 15 on LAPTOP-D73GD8TE with PID 15740 (E:\java\IdeaProjects\springboot maven 配置文件\target\hello.jar started by 2840
1 in E:\java\IdeaProjects\springboot maven 配置文件\target)
2021-09-30 19:05:51.782  INFO 15740 --- [           main] com.example.demo.DemoApplication         : No active profile set, falling bac
k to default profiles: default
2021-09-30 19:05:53.624  INFO 15740 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8
080 (http)
2021-09-30 19:05:53.643  INFO 15740 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2021-09-30 19:05:53.643  INFO 15740 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache T
omcat/9.0.53]
2021-09-30 19:05:53.643  INFO 15740 --- [           main] o.a.catalina.core.AprLifecycleListener   : An older version [1.2.23] of the A
pache Tomcat Native library is installed, while Tomcat recommends a minimum version of [1.2.30]
2021-09-30 19:05:53.643  INFO 15740 --- [           main] o.a.catalina.core.AprLifecycleListener   : Loaded Apache Tomcat Native librar
y [1.2.23] using APR version [1.7.0].
2021-09-30 19:05:53.643  INFO 15740 --- [           main] o.a.catalina.core.AprLifecycleListener   : APR capabilities: IPv6 [true], sen
dfile [true], accept filters [false], random [true], UDS [false].
2021-09-30 19:05:53.643  INFO 15740 --- [           main] o.a.catalina.core.AprLifecycleListener   : APR/OpenSSL configuration: useAprC
onnector [false], useOpenSSL [true]
2021-09-30 19:05:53.659  INFO 15740 --- [           main] o.a.catalina.core.AprLifecycleListener   : OpenSSL successfully initialized [
OpenSSL 1.1.1c  28 May 2019]
2021-09-30 19:05:53.784  INFO 15740 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebAp
plicationContext
2021-09-30 19:05:53.784  INFO 15740 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initia
lization completed in 1888 ms
2021-09-30 19:05:54.460  INFO 15740 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (h
ttp) with context path ''
2021-09-30 19:05:54.476  INFO 15740 --- [           main] com.example.demo.DemoApplication         : Started DemoApplication in 3.541 s
econds (JVM running for 4.407)
2021-09-30 19:06:01.366  INFO 15740 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring DispatcherServ
let 'dispatcherServlet'
2021-09-30 19:06:01.366  INFO 15740 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherSe
rvlet'
2021-09-30 19:06:01.367  INFO 15740 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Completed initialization in 0 ms
# 可以读取jar包外配置文件
Person(name=gtlx, age=20)

                               

                        

おすすめ

転載: blog.csdn.net/weixin_43931625/article/details/120472481