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>