码迷,mamicode.com
首页 > 编程语言 > 详细

springboot-31-springboot静态注入

时间:2017-09-11 22:48:43      阅读:220      评论:0      收藏:0      [点我收藏+]

标签:ini   const   调用   bean   stat   ict   new   app   component   

springboot中 使用 @Autowired 注入时, 是可以为静态变量进行注入的

package com.iwhere.footmark.tools;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component;

/**
 * 测试静态注入
 * @author wenbronk
 * @time 2017年8月30日 上午9:40:00
 */
@Component
public class StaticTest {

    private static StringRedisTemplate redis;
    @Autowired
    public void setRedis(StringRedisTemplate redis){
        StaticTest.redis = redis;
    }
    
    public static void setGet() {
        String key = "abc";
        ValueOperations<String, String> opsForValue = redis.opsForValue();
        opsForValue.set(key, "static_test_value");
        String string = opsForValue.get(key);
        System.out.println(string);
    }
    
}

然后, 调用的方式为: 

    @Autowired
    private TestService TestService;

    @RequestMapping("/")
    public String test() {
    //    String test = TestService.test();
        StaticTest.setGet();
        return test;
    }

 

也可以在 application.yml中的常量, 使用静态注入: 

package com.iwhere.footmark.properties;

import java.util.ArrayList;
import java.util.List;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;

/**
 * 常量配置
 * 
 * @author wenbronk
 * @Date 下午5:15:32
 */
@Configuration
@ConfigurationProperties(prefix = "custom.constance")
public class ConstanceProperties {
    public static final Integer FOOT_DEFAULT_LEVEL = 15;
    public static final Integer FOOT_MARK_GEOLEVEL = 19;
    public static final String BGM_URL = "http://qiniu.iwhere.com/track/9/fenghuanggucheng/backgroundmusic1.mp3";
    // http://qiniu.iwhere.com/track/9/fenghuanggucheng/backgroundmusic3.mp3
    public static final String TEMPLATE_CODE = "001";
    public static final Integer FOOT_MARK_MAX = 10;
    public static final String USER_AND_TOURIS_KEY = "44D2568638FFF096996CB6544CAC6B15";
    public static final String DEFAULT_IMG_URL = "http://qiniu.iwhere.com/track/9/fenghuanggucheng/foxiangge1.jpeg";

    private static Integer footDefaultLevel = FOOT_DEFAULT_LEVEL;
    private static Integer footMarkGeoLevel = FOOT_MARK_GEOLEVEL;
    private static String bgmUrl = BGM_URL;
    private static String bgmUrl2;
    private static String bgmUrl3;
    private static Integer footMarkMax = FOOT_MARK_MAX;
    private static String templateCode = TEMPLATE_CODE;
    private static String userAndTourisKey = USER_AND_TOURIS_KEY;
    private static String defaultImgUrl = DEFAULT_IMG_URL;
    
    public static List<String> getBgmUrls() {
        List<String> result = new ArrayList<>();
        result.add(bgmUrl);
        result.add(bgmUrl2);
        result.add(bgmUrl3);
        return result;
    }

    public static Integer getFootDefaultLevel() {
        return footDefaultLevel;
    }

    public static void setFootDefaultLevel(Integer footDefaultLevel) {
        ConstanceProperties.footDefaultLevel = footDefaultLevel;
    }

    public static Integer getFootMarkGeoLevel() {
        return footMarkGeoLevel;
    }

    public static void setFootMarkGeoLevel(Integer footMarkGeoLevel) {
        ConstanceProperties.footMarkGeoLevel = footMarkGeoLevel;
    }

    public static String getBgmUrl() {
        return bgmUrl;
    }

    public static void setBgmUrl(String bgmUrl) {
        ConstanceProperties.bgmUrl = bgmUrl;
    }

    public static Integer getFootMarkMax() {
        return footMarkMax;
    }

    public static void setFootMarkMax(Integer footMarkMax) {
        ConstanceProperties.footMarkMax = footMarkMax;
    }

    public static String getTemplateCode() {
        return templateCode;
    }

    public static void setTemplateCode(String templateCode) {
        ConstanceProperties.templateCode = templateCode;
    }

    public static String getUserAndTourisKey() {
        return userAndTourisKey;
    }

    public static void setUserAndTourisKey(String userAndTourisKey) {
        ConstanceProperties.userAndTourisKey = userAndTourisKey;
    }

    public static String getDefaultImgUrl() {
        return defaultImgUrl;
    }

    public static void setDefaultImgUrl(String defaultImgUrl) {
        ConstanceProperties.defaultImgUrl = defaultImgUrl;
    }

    public static String getBgmUrl2() {
        return bgmUrl2;
    }

    public static void setBgmUrl2(String bgmUrl2) {
        ConstanceProperties.bgmUrl2 = bgmUrl2;
    }

    public static String getBgmUrl3() {
        return bgmUrl3;
    }

    public static void setBgmUrl3(String bgmUrl3) {
        ConstanceProperties.bgmUrl3 = bgmUrl3;
    }

}

 

springboot-31-springboot静态注入

标签:ini   const   调用   bean   stat   ict   new   app   component   

原文地址:http://www.cnblogs.com/wenbronk/p/7451628.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!