美文网首页工作生活
SpringBoot 从配置文件(properties)读取 m

SpringBoot 从配置文件(properties)读取 m

作者: PC_Repair | 来源:发表于2019-06-30 09:10 被阅读0次
  • application-list.properties
map.tool.cmt=fehuang2
map.tool.cmc=jieb
map.tool.slim=chzhang
map.tool.gfc=leich3

map.itl.Jack=jackc2
map.itl.Iris=yili8
map.itl.Panda=pandax
map.itl.Adun=gengwang
map.itl.Caedmon=chuankwa
  • 读取配置文件的类
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@Component
@PropertySource(value = "classpath:application-list.properties")
@ConfigurationProperties(prefix = "map")
public class RelatedPerson {

    private Map<String, String> tool = new HashMap<>();

    private Map<String, String> itl = new HashMap<>();

    public Map<String, String> getTool() {
        return tool;
    }

    public void setTool(Map<String, String> tool) {
        this.tool = tool;
    }

    public Map<String, String> getItl() {
        return itl;
    }

    public void setItl(Map<String, String> itl) {
        this.itl = itl;
    }
}
  • 在其他类中可以通过 @Autowired 注入的方式使用
@Service
@Transactional
public class SparkBotService {
    private static final Logger logger = LoggerFactory.getLogger(SparkBotService.class);
    
    @Autowired
    private RelatedPerson relatedPerson;

    private static Map<String, String> map = new HashMap<>();

    @PostConstruct
    public void init() {
        map.putAll(relatedPerson.getItl());
        map.putAll(relatedPerson.getTool());
    }
}

相关文章

网友评论

    本文标题:SpringBoot 从配置文件(properties)读取 m

    本文链接:https://www.haomeiwen.com/subject/wspvcctx.html