Skip to content

Commit

Permalink
add SpringBoot-Secret
Browse files Browse the repository at this point in the history
  • Loading branch information
mouday committed Aug 29, 2023
1 parent 1a26c00 commit 6de84ca
Show file tree
Hide file tree
Showing 35 changed files with 1,095 additions and 0 deletions.
28 changes: 28 additions & 0 deletions SpringBoot-Caffeine/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?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>

<groupId>com.example</groupId>
<artifactId>caffeine</artifactId>
<version>1.0-SNAPSHOT</version>

<dependencies>
<!-- https://mvnrepository.com/artifact/com.github.ben-manes.caffeine/caffeine -->
<!-- https://mvnrepository.com/artifact/com.github.ben-manes.caffeine/caffeine -->
<dependency>
<groupId>com.github.ben-manes.caffeine</groupId>
<artifactId>caffeine</artifactId>
<version>2.9.3</version>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package com.example.caffeine;

import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
import org.junit.Test;

import java.util.concurrent.TimeUnit;

public class CaffeineTest {
@Test
public void testCache() {

Cache<String, String> cache = Caffeine.newBuilder()
.expireAfterWrite(3, TimeUnit.SECONDS)
.maximumSize(10_000)
.build();

// 查找一个缓存元素, 没有查找到的时候返回null
String key = "key";

String value = cache.getIfPresent(key);
System.out.println(value);
// null

// 查找缓存,如果缓存不存在则生成缓存元素, 如果无法生成则返回null
value = cache.get(key, k -> "value");
System.out.println(value);
// value

// 移除一个缓存元素
cache.invalidate(key);
value = cache.getIfPresent(key);
System.out.println(value);
// null


// 添加或者更新一个缓存元素
cache.put(key, "newValue");
value = cache.getIfPresent(key);
System.out.println(value);
// newValue


// 延迟4秒
try {
Thread.sleep(4 * 1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
// 再次获取缓存为null
value = cache.getIfPresent(key);
System.out.println(value);
// null
}

}
33 changes: 33 additions & 0 deletions SpringBoot-Package/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
HELP.md
target/
!.mvn/wrapper/maven-wrapper.jar
!**/src/main/**/target/
!**/src/test/**/target/

### STS ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache

### IntelliJ IDEA ###
.idea
*.iws
*.iml
*.ipr

### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
build/
!**/src/main/**/build/
!**/src/test/**/build/

### VS Code ###
.vscode/
9 changes: 9 additions & 0 deletions SpringBoot-Package/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# 开发环境
.PHONY: dev
dev:
mvn spring-run:run

# 发布生产环境
.PHONY: build
build:
mvn clean package -Pproduction
9 changes: 9 additions & 0 deletions SpringBoot-Package/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# SpringBoot Start

启动模板

已整合

- lombok
- mybatis-plus
- mysql
184 changes: 184 additions & 0 deletions SpringBoot-Package/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
<?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 https://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.7.7</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>

<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.2-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>

<properties>
<java.version>1.8</java.version>
<mybatis-plus.version>3.5.2</mybatis-plus.version>
</properties>

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

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>

<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>

<!-- test -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<!-- 跳过测试 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skipTests>true</skipTests>
</configuration>
</plugin>
</plugins>
</build>

<profiles>
<!--开发环境-->
<profile>
<id>development</id>

<activation>
<!-- 设置默认激活 -->
<activeByDefault>true</activeByDefault>
</activation>

<properties>
<profile.active>dev</profile.active>
</properties>

<build>
<finalName>${name}-development</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</profile>

<!--生产环境-->
<profile>
<id>production</id>

<properties>
<profile.active>pro</profile.active>
</properties>

<build>
<finalName>${name}-production</finalName>
<plugins>
<!-- 生成jar -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.1.1</version>
<configuration>
<archive>
<!-- 生成的jar中不要包含pom.xml和pom.properties这两个文件 -->
<addMavenDescriptor>false</addMavenDescriptor>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<!--项目的启动类-->
<mainClass>com.example.demo.Application</mainClass>
</manifest>
</archive>
</configuration>
</plugin>

<!--拷贝依赖到jar外面的lib目录-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-lib</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/lib</outputDirectory>
<excludeTransitive>false</excludeTransitive>
<stripVersion>false</stripVersion>
<includeScope>runtime</includeScope>
</configuration>
</execution>
</executions>
</plugin>

<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<!-- 打包后的包名是否包含assembly的id名 -->
<appendAssemblyId>false</appendAssemblyId>
<!-- tar或者zip包的输出目录 -->
<outputDirectory>${project.build.directory}/dist/</outputDirectory>
<descriptors>
<!-- 引用的assembly配置文件-->
<descriptor>src/main/resources/assembly.xml</descriptor>
</descriptors>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<!-- 绑定到package生命周期阶段上 -->
<phase>package</phase>
<goals>
<!-- 只运行一次 -->
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>

<!--排除resources下面的yml-->
<resources>
<resource>
<directory>src/main/resources</directory>
<excludes>
<exclude>**/application*.yml</exclude>
<exclude>**/assembly.xml</exclude>
</excludes>
</resource>
</resources>
</build>
</profile>
</profiles>
</project>
12 changes: 12 additions & 0 deletions SpringBoot-Package/src/main/java/com/example/demo/Application.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.example.demo;

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);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.example.demo.config;

import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;

@Configuration
@Data
public class AppConfig {
@Value("${app.name}")
private String name;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.example.demo.controller;

import com.example.demo.config.AppConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class IndexController {
@Autowired
private AppConfig appConfig;

@GetMapping("/")
public String index() {
return "Hello: " + appConfig.getName();
}
}
2 changes: 2 additions & 0 deletions SpringBoot-Package/src/main/resources/application-dev.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
app:
name: dev
2 changes: 2 additions & 0 deletions SpringBoot-Package/src/main/resources/application-pro.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
app:
name: pro
7 changes: 7 additions & 0 deletions SpringBoot-Package/src/main/resources/application.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
spring:
profiles:
# 取pom.xml当中profile当中配置的profile.active标签
active: @profile.active@

app:
name: default
Loading

0 comments on commit 6de84ca

Please sign in to comment.