Skip to content

Commit

Permalink
#179 spel & @value 使用姿势实例
Browse files Browse the repository at this point in the history
  • Loading branch information
liuyueyi committed Jun 15, 2021
1 parent 6f4d5ca commit d408eee
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.git.hui.boot.properties.value;

import com.alibaba.fastjson.JSONObject;
import com.git.hui.boot.properties.value.config.ConfigProperties;
import com.git.hui.boot.properties.value.config.SpelProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
Expand All @@ -17,11 +19,20 @@ public class Application {
@Autowired
private ConfigProperties configProperties;

@Autowired
private SpelProperties spelProperties;

@GetMapping("show")
public String showProperties() {
return configProperties.toJsonStr();
}


@GetMapping("spel")
public SpelProperties showSpel() {
return spelProperties;
}

public static void main(String[] args) {
SpringApplication.run(Application.class);
}
Expand Down
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("_", ".");
}
}
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);
}
}

0 comments on commit d408eee

Please sign in to comment.