Spring 实现 EnvironmentAware 接口 获取配置文件中参数

package com.bootdo.demo.service;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.EnvironmentAware;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;


import com.bootdo.demo.dao.DemoDao;
import com.bootdo.demo.domain.DemoEntity;






@Service
public class DemoPar implements EnvironmentAware{
@Autowired

static String s;

        static Map map();

        static RelaxedPropertyResolver propertyResolver;

@Override
public void setEnvironment(Environment env) {  //env 包含全局的所有的变量
// TODO Auto-generated method stub

propertyResolver = new RelaxedPropertyResolver(env, "druid."); //获取所有druid. 下面的属性并封装起来

             String initialSize = propertyResolver.getProperty("initialSize");  //获取propertyResolver 中 key为 initialSize 的值
             String minIdle =propertyResolver.getProperty("minIdle"); //获取propertyResolver 中 key为 minIdle 的值
             String maxActive = propertyResolver.getProperty("maxActive"); //获取propertyResolver 中 key为 maxActive 的值


String property = env.getProperty("druid.initialSize");  //获取key 为druid.initialSize 的变量值
s=property;

}

}

猜你喜欢

转载自blog.csdn.net/qq_38621910/article/details/80346288