Springboot配置文件静态注入

  1. 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方法不能是静态的。

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值