springboot启动加载环境变量 springboot启动类加载配置文件 springboot context(上下文)的加载和注入使用 springboot配置不同环境配置环境测试环境和正式环

package cn.hph.watermark;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@EnableScheduling
@SpringBootApplication(exclude={DataSourceAutoConfiguration.class})
public class WatermarkApplication {

    public static void main(String[] args) {
//        SpringApplication.run(WatermarkApplication.class, args);
//        ConfigurableApplicationContext context=
//                new SpringApplicationBuilder(WatermarkApplication.class)
//                        .properties
        这个路径不提示代码,所以最好用默认
//                                ("spring.config.location=classpath:/cn/hph/watermark/configTxt/test01/test01.properties")
//                        .run(args);



                ConfigurableApplicationContext context=
                new SpringApplicationBuilder(WatermarkApplication.class)
                        .properties
        //这个路径不提示代码,所以最好用默认
                                ("spring.config.location=classpath:/application.yml")
                        .properties("spring.profiles.active=oracle")
                        .run(args);

        // 输出变量
        System.out.println(context.getEnvironment().getProperty("spring.datasource.password"));

    }




    @RequestMapping("/dododo")
    public void makeSpringbootNotStopWhenStart(){

    }



}

-----下面是上下文注入的使用

package cn.hph.watermark.restController;

import cn.hph.watermark.entity.Person;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

@RestController
public class Test01RestController {
    //注入上下文,当然可以注入 @Value("${}")注入
    @Autowired
    private ConfigurableApplicationContext context;

    @GetMapping(value="hello")
    public String hello(){
        return "Hello World";
    }


    @RequestMapping(value="person/{personId}",method =RequestMethod.GET,produces = MediaType.APPLICATION_JSON_VALUE)
    public Person findPerson(@PathVariable("personId") Integer personId){
        Person p=new Person();
        p.setId(personId);
        p.setName("Crazyit");
        p.setAge(33);
        // 输出变量 root1
        System.out.println(context.getEnvironment().getProperty("spring.datasource.password"));
        return p;
    }


}

-------

下面是yml配置文件

server:
   port:
      8081
spring:
   profiles: mysql
   http:
     encoding:
       charset: UTF-8
   datasource:
     driver-class-name: com.mysql.cj.jdbc.Driver
     name: root1
     password:  root1
   thymeleaf:
     prefix: classpath:/templates/
  #不同的profiles环境用  --- 隔开,然后再启动类中看启动哪个,主意只有中文注释必须是utf-8的文本
---
spring:
  profiles: oracle
  http:
    encoding:
      charset: UTF-8
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    name: root1
    password:  root1
  thymeleaf:
    prefix: classpath:/templates/






----下面是pom.xml

 

 

<?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>cn.hph</groupId>
    <artifactId>watermark</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>watermark</name>
    <description>测试水印去除和springbooot的其它测试</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <!-- 实现热部署-->
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <version>2.0.1.RELEASE</version>
        </dependency>


        <!-- https://mvnrepository.com/artifact/cn.hutool/hutool-all -->
        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>4.5.6</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>


        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-validation</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.0.0</version>
        </dependency>

        <!--<dependency>
            <groupId>com.microsoft.sqlserver</groupId>
            <artifactId>mssql-jdbc</artifactId>
            <scope>runtime</scope>
        </dependency>-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </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-web</artifactId>
            <version>RELEASE</version>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>5.1.2.RELEASE</version>
        </dependency>
    </dependencies>

    <build>
        <!--resources配置解决mybatis 的mapperXml配置在java路径不被扫描的问题 -->
        <resources>
            <resource>
                <directory>src/main/java</directory>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
            </resource>
        </resources>

        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

 

 

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值