Skip to content

Commit

Permalink
增加配置文件示例
Browse files Browse the repository at this point in the history
  • Loading branch information
YunaiV committed Jan 20, 2020
1 parent 8d3438a commit 54b0c4b
Show file tree
Hide file tree
Showing 10 changed files with 172 additions and 0 deletions.
23 changes: 23 additions & 0 deletions lab-43/lab-43-demo-configname/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?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">
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>lab-43-demo-configname</artifactId>

<dependencies>
<!-- Spring Boot Starter 基础依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package cn.iocoder.springboot.lab43.propertydemo;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.config.ConfigFileApplicationListener;
import org.springframework.stereotype.Component;

@SpringBootApplication
public class Application {

/**
* 设置需要读取的配置文件的名字。
* 基于 {@link org.springframework.boot.context.config.ConfigFileApplicationListener#CONFIG_NAME_PROPERTY} 实现。
*/
private static final String CONFIG_NAME_VALUE = "application,rpc";

public static void main(String[] args) {
// 设置环境变量
System.setProperty(ConfigFileApplicationListener.CONFIG_NAME_PROPERTY, CONFIG_NAME_VALUE);

// 启动 Spring Boot 应用
SpringApplication.run(Application.class, args);
}

@Component
public class ValueCommandLineRunner implements CommandLineRunner {

private final Logger logger = LoggerFactory.getLogger(getClass());

@Value("${application-test}")
private String applicationTest;

@Value("${rpc-test}")
private String rpcTest;

@Override
public void run(String... args) {
logger.info("applicationTest:" + applicationTest);
logger.info("rpcTest:" + rpcTest);
}

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
application-test: hahaha
1 change: 1 addition & 0 deletions lab-43/lab-43-demo-configname/src/main/resources/rpc.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
rpc-test: yeah
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
application-test: hahaha
37 changes: 37 additions & 0 deletions lab-43/lab-43-demo-jasypt/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?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">
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>lab-43-demo-jasypt</artifactId>

<dependencies>
<!-- Spring Boot Starter 基础依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>

<!-- 实现对 Jasypt 实现自动化配置 -->
<dependency>
<groupId>com.github.ulisesbocchio</groupId>
<artifactId>jasypt-spring-boot-starter</artifactId>
<version>3.0.2</version>
</dependency>

<!-- 方便等会写单元测试 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package cn.iocoder.springboot.lab43.propertydemo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application {

public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}

}
11 changes: 11 additions & 0 deletions lab-43/lab-43-demo-jasypt/src/main/resources/application.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
spring:
application:
# name: ENC(xQZuD8KnkqzIGep0FFH0DYJ3Re9TrKTdvu2fxIlWNpwFcdNGhkpCag==)
# name: ENC(KoaHnIhRGiCdWh0T2lby899Cov6MyiAXrW5PadJ3XFY=)
name: demo-application

jasypt:
# jasypt 配置项,对应 JasyptEncryptorConfigurationProperties 配置类
encryptor:
algorithm: PBEWithMD5AndDES # 加密算法
password: ${JASYPT_PASSWORD} # 加密秘钥
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package cn.iocoder.springboot.lab43.propertydemo;

import org.jasypt.encryption.StringEncryptor;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
public class JasyptTest {

@Autowired
private StringEncryptor encryptor;

@Test
public void encode() {
String applicationName = "demo-application";
System.out.println(encryptor.encrypt(applicationName));
}

@Value("${spring.application.name}")
private String applicationName;

@Test
public void print() {
System.out.println(applicationName);
}

@Value("${jasypt.encryptor.password}")
private String password;

}
2 changes: 2 additions & 0 deletions lab-43/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
<modules>
<module>lab-43-demo</module>
<module>lab-43-demo-profiles</module>
<module>lab-43-demo-jasypt</module>
<module>lab-43-demo-configname</module>
</modules>


Expand Down

0 comments on commit 54b0c4b

Please sign in to comment.