- YML配置文件静态注入。
name:
rule:
entity: $Eo
url: main\java\
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
/**
* @author: topsnowwolf
* @description: 静态属性注入,必须使用value
* @date: Create in 2018/7/22 17:02
* @modified by:
* @versions:0.1.0
*/
@Component
@ConfigurationProperties(prefix = "name.rule")
public class GetNameYml {
private static String entity;
private static String url;
public static String getEntity() {
return entity;
}
@Value("${name.rule.entity}")
public void setEntity(String entity) {
GetNameYml.entity = entity;
}
public static String getUrl() {
return url;
}
@Value("${name.rule.url}")
public void setUrl(String url) {
GetNameYml.url = url;
}
}
2.properties配置文件
#实体类命名规则
beanrule.customEntityName=$Eo
#实体类生成import生成默认截取规则
beanrule.customEntityImport=main\java\
#实体类继承配置
#beanrule.customEntityExtends[BaseVo]=import io.swagger.annotations.BaseVo
#实体类实现接口配置(可以是多个)
#beanrule.customEntityImpl[Serializable]=import java.io.Serializable
#beanrule.customEntityImpl[BaseVoImpl]=import io.swagger.annotations.BaseVoImpl
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import java.util.Map;
/**
* @author: topsnowwolf
* @description: 静态注入配置信息
* @date: Create in 2018/8/4 10:17
* @modified by:
* @versions:0.1.0
*/
@Configuration
@ConfigurationProperties(prefix = "beanrule",ignoreUnknownFields = false)
@PropertySource(value ={"classpath:rule/namingRule.properties"},ignoreResourceNotFound = true)
public class GetRule {
public static String customEntityName;
public static String customEntityImport;
public static Map<String,String> customEntityExtends;
public static Map<String,String> customEntityImpl;
public static String getCustomEntityName() {
return customEntityName;
}
public void setCustomEntityName(String customEntityName) {
GetRule.customEntityName = customEntityName;
}
public void setCustomEntityImport(String customEntityImport) {
GetRule.customEntityImport = customEntityImport;
}
public void setCustomEntityExtends(Map<String, String> customEntityExtends) {
GetRule.customEntityExtends = customEntityExtends;
}
public void setCustomEntityImpl(Map<String, String> customEntityImpl) {
GetRule.customEntityImpl = customEntityImpl;
}
}
总结:
配置文件要静态注入,注入的属性必须是静态的,set方法不能是静态的。