spring mvc4获取服务器java版本web容器等信息

今天在阅读springMVC源码的过程中,有了Aware的概念

其中有个接口叫做EnvironmentAware 环境通知。

可以获取到系统的环境信息,于是就研究了一下,得到的最终结果如下图:



 

具体获取步骤,代码如下:

创建类:SystemController 实现接口 EnvironmentAware

package org.moon.framework.controller;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.EnvironmentAware;
import org.springframework.core.env.Environment;
import org.springframework.core.env.MutablePropertySources;
import org.springframework.core.env.PropertySource;
import org.springframework.core.env.PropertySources;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.support.StandardServletEnvironment;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;


/**
 * 获取系统环境信息
 */

@Controller
@RequestMapping(value = "/system")
public class SystemController implements EnvironmentAware {


    private Environment environment = null;

    public void setEnvironment(Environment environment) {
        this.environment = environment;
    }

    private Properties properties = null;

    /**
     * 操作系统及服务器信息
     */
    private final String PROPERTIESKEY = "systemProperties";


    private String defaultPropertiesKey = "";

    public String getDefaultPropertiesKey() {
        return defaultPropertiesKey;
    }

    public void setDefaultPropertiesKey(String defaultPropertiesKey) {
        this.defaultPropertiesKey = defaultPropertiesKey;
    }


    private StandardServletEnvironment getStandardServletEnvironment(){

        StandardServletEnvironment standardServletEnvironment = (StandardServletEnvironment) environment;

        return standardServletEnvironment;
    }


    private MutablePropertySources getMutablePropertySources(){
        MutablePropertySources mutablePropertySources = getStandardServletEnvironment().getPropertySources();

        return mutablePropertySources;
    }

    private PropertySource getPropertySource(){
        String propertiesName = getDefaultPropertiesKey();
        if(StringUtils.isBlank(propertiesName)){
            throw new RuntimeException("please set default properties key....");
        }
        PropertySource systemProperties =  getMutablePropertySources().get(propertiesName);

        return systemProperties;
    }

    private Properties getProperties(){
        Properties properties = (Properties) getPropertySource().getSource();

        return properties;
    }

    private String getSystemConfigInfo(String key){
        properties = getProperties();
        String value = properties.get(key)+"";
        return  value;
    }


    /**
     * 将服务器及服务器系统,web容器,java环境等全部信息封装成map
     * @return
     */
    @RequestMapping("/computerConfig")
    @ResponseBody
    public Map<String,Object> getComputerConfig()  {
        Map<String,Object> map = new HashMap<String, Object>();
        setDefaultPropertiesKey(PROPERTIESKEY);
        Properties properties = getProperties();
        for (Object key : properties.keySet()) {
            map.put(key.toString(),properties.get(key));
        }
        return map;
    }





}

 我们来前端访问试试



 希望对大家有帮助吧!

猜你喜欢

转载自liaoyue11.iteye.com/blog/2354262
今日推荐