-
Notifications
You must be signed in to change notification settings - Fork 328
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
124 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
...operties-value/src/main/java/com/git/hui/boot/properties/value/config/SpelProperties.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package com.git.hui.boot.properties.value.config; | ||
|
||
import lombok.Data; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.UUID; | ||
|
||
/** | ||
* @author yihui | ||
* @date 21/6/12 | ||
*/ | ||
@Data | ||
@Component | ||
public class SpelProperties { | ||
|
||
@Value("1 + 2") | ||
private String common; | ||
|
||
@Value("demo_${auth.jwt.token}") | ||
private String prefixConf; | ||
|
||
@Value("#{'abcd'}") | ||
private String spelStr; | ||
|
||
/** | ||
* 基本计算 | ||
*/ | ||
@Value("#{1 + 2}") | ||
private String spelVal3; | ||
|
||
/** | ||
* 列表 | ||
*/ | ||
@Value("#{{1, 2, 3}}") | ||
private List<Integer> spelList; | ||
|
||
/** | ||
* map | ||
*/ | ||
@Value("#{{a: '123', b: 'cde'}}") | ||
private Map spelMap; | ||
|
||
|
||
/** | ||
* 嵌套使用,从配置中获取值,然后执行SpEL语句 | ||
*/ | ||
@Value("#{'${auth.jwt.token}'.substring(2)}") | ||
private String spelLen; | ||
|
||
/** | ||
* 调用静态方法 | ||
*/ | ||
@Value("#{T(com.git.hui.boot.properties.value.config.SpelProperties).uuid('${auth.jwt.token}_')}") | ||
private String spelStaticMethod; | ||
|
||
/** | ||
* bean 方法访问 | ||
*/ | ||
@Value("#{randomService.randUid()}") | ||
private String spelBeanMethod; | ||
|
||
@Value("${¥{auth.jwt.token}}") | ||
private String selfProperty; | ||
|
||
public static String uuid(String prefix) { | ||
return prefix + UUID.randomUUID().toString().replaceAll("_", "."); | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
...c/main/java/com/git/hui/boot/properties/value/config/selfdefine/SelfPropertiesSource.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package com.git.hui.boot.properties.value.config.selfdefine; | ||
|
||
import org.springframework.core.env.ConfigurableEnvironment; | ||
import org.springframework.core.env.MapPropertySource; | ||
import org.springframework.stereotype.Component; | ||
|
||
import javax.annotation.PostConstruct; | ||
import javax.annotation.Resource; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
/** | ||
* @author yihui | ||
* @date 21/6/12 | ||
*/ | ||
@Component | ||
public class SelfPropertiesSource extends MapPropertySource { | ||
@Resource | ||
private ConfigurableEnvironment environment; | ||
|
||
public SelfPropertiesSource(String name, Map<String, Object> source) { | ||
super(name, source); | ||
} | ||
|
||
public SelfPropertiesSource() { | ||
this("SelfPropertiesSource", new HashMap<>()); | ||
} | ||
|
||
@PostConstruct | ||
public void init() { | ||
environment.getPropertySources().addFirst(this); | ||
} | ||
|
||
@Override | ||
public Object getProperty(String name) { | ||
if (name.startsWith("¥") && name.endsWith("}")) { | ||
name = name.substring(2, name.length() - 2); | ||
return super.getProperty(name) + " (一灰灰blog) "; | ||
} | ||
return super.getProperty(name); | ||
} | ||
} |