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

  1. package com.iwhere.footmark.tools;
  2.  
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.data.redis.core.StringRedisTemplate;
  5. import org.springframework.data.redis.core.ValueOperations;
  6. import org.springframework.stereotype.Component;
  7.  
  8. /**
  9. * 测试静态注入
  10. * @author wenbronk
  11. * @time 2017年8月30日 上午9:40:00
  12. */
  13. @Component
  14. public class StaticTest {
  15.  
  16. private static StringRedisTemplate redis;
  17. @Autowired
  18. public void setRedis(StringRedisTemplate redis){
  19. StaticTest.redis = redis;
  20. }
  21.  
  22. public static void setGet() {
  23. String key = "abc";
  24. ValueOperations<String, String> opsForValue = redis.opsForValue();
  25. opsForValue.set(key, "static_test_value");
  26. String string = opsForValue.get(key);
  27. System.out.println(string);
  28. }
  29.  
  30. }

然后, 调用的方式为:

  1. @Autowired
  2. private TestService TestService;
  3.  
  4. @RequestMapping("/")
  5. public String test() {
  6. // String test = TestService.test();
  7. StaticTest.setGet();
  8. return test;
  9. }

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

  1. package com.iwhere.footmark.properties;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.List;
  5.  
  6. import org.springframework.boot.context.properties.ConfigurationProperties;
  7. import org.springframework.context.annotation.Configuration;
  8.  
  9. /**
  10. * 常量配置
  11. *
  12. * @author wenbronk
  13. * @Date 下午5:15:32
  14. */
  15. @Configuration
  16. @ConfigurationProperties(prefix = "custom.constance")
  17. public class ConstanceProperties {
  18. public static final Integer FOOT_DEFAULT_LEVEL = ;
  19. public static final Integer FOOT_MARK_GEOLEVEL = ;
  20. public static final String BGM_URL = "http://qiniu.iwhere.com/track/9/fenghuanggucheng/backgroundmusic1.mp3";
  21. // http://qiniu.iwhere.com/track/9/fenghuanggucheng/backgroundmusic3.mp3
  22. public static final String TEMPLATE_CODE = "";
  23. public static final Integer FOOT_MARK_MAX = ;
  24. public static final String USER_AND_TOURIS_KEY = "44D2568638FFF096996CB6544CAC6B15";
  25. public static final String DEFAULT_IMG_URL = "http://qiniu.iwhere.com/track/9/fenghuanggucheng/foxiangge1.jpeg";
  26.  
  27. private static Integer footDefaultLevel = FOOT_DEFAULT_LEVEL;
  28. private static Integer footMarkGeoLevel = FOOT_MARK_GEOLEVEL;
  29. private static String bgmUrl = BGM_URL;
  30. private static String bgmUrl2;
  31. private static String bgmUrl3;
  32. private static Integer footMarkMax = FOOT_MARK_MAX;
  33. private static String templateCode = TEMPLATE_CODE;
  34. private static String userAndTourisKey = USER_AND_TOURIS_KEY;
  35. private static String defaultImgUrl = DEFAULT_IMG_URL;
  36.  
  37. public static List<String> getBgmUrls() {
  38. List<String> result = new ArrayList<>();
  39. result.add(bgmUrl);
  40. result.add(bgmUrl2);
  41. result.add(bgmUrl3);
  42. return result;
  43. }
  44.  
  45. public static Integer getFootDefaultLevel() {
  46. return footDefaultLevel;
  47. }
  48.  
  49. public static void setFootDefaultLevel(Integer footDefaultLevel) {
  50. ConstanceProperties.footDefaultLevel = footDefaultLevel;
  51. }
  52.  
  53. public static Integer getFootMarkGeoLevel() {
  54. return footMarkGeoLevel;
  55. }
  56.  
  57. public static void setFootMarkGeoLevel(Integer footMarkGeoLevel) {
  58. ConstanceProperties.footMarkGeoLevel = footMarkGeoLevel;
  59. }
  60.  
  61. public static String getBgmUrl() {
  62. return bgmUrl;
  63. }
  64.  
  65. public static void setBgmUrl(String bgmUrl) {
  66. ConstanceProperties.bgmUrl = bgmUrl;
  67. }
  68.  
  69. public static Integer getFootMarkMax() {
  70. return footMarkMax;
  71. }
  72.  
  73. public static void setFootMarkMax(Integer footMarkMax) {
  74. ConstanceProperties.footMarkMax = footMarkMax;
  75. }
  76.  
  77. public static String getTemplateCode() {
  78. return templateCode;
  79. }
  80.  
  81. public static void setTemplateCode(String templateCode) {
  82. ConstanceProperties.templateCode = templateCode;
  83. }
  84.  
  85. public static String getUserAndTourisKey() {
  86. return userAndTourisKey;
  87. }
  88.  
  89. public static void setUserAndTourisKey(String userAndTourisKey) {
  90. ConstanceProperties.userAndTourisKey = userAndTourisKey;
  91. }
  92.  
  93. public static String getDefaultImgUrl() {
  94. return defaultImgUrl;
  95. }
  96.  
  97. public static void setDefaultImgUrl(String defaultImgUrl) {
  98. ConstanceProperties.defaultImgUrl = defaultImgUrl;
  99. }
  100.  
  101. public static String getBgmUrl2() {
  102. return bgmUrl2;
  103. }
  104.  
  105. public static void setBgmUrl2(String bgmUrl2) {
  106. ConstanceProperties.bgmUrl2 = bgmUrl2;
  107. }
  108.  
  109. public static String getBgmUrl3() {
  110. return bgmUrl3;
  111. }
  112.  
  113. public static void setBgmUrl3(String bgmUrl3) {
  114. ConstanceProperties.bgmUrl3 = bgmUrl3;
  115. }
  116.  
  117. }

springboot-31-springboot静态注入的更多相关文章

  1. Springboot 工具类静态注入

    用springboot搭了一个项目,里面要用到一个DictUtils,因为要用到DictMapper,在百度找了一些方法,最后用下面的方法能成功获取到DictMapper @Component pub ...

  2. Spring不能直接@autowired注入Static变量/ 关于SpringBoot的@Autowired 静态变量注入

    昨天在编写JavaMail工具类的时候,静态方法调用静态变量,这是很正常的操作,当时也没多想,直接静态注入. @Component public class JavaMailUtil { @Autow ...

  3. Spring-Boot 内置静态资源文件地址修改

    Spring-Boot 内置MVC静态文件地址修改 Why:1.Spring-Boot修改内置SpringMVC静态资源路径,提高项目目录结构的安全性.2.配置拦截路径时可以剔除静态文件拦截How:1 ...

  4. Springboot】Springboot整合邮件服务(HTML/附件/模板-QQ、网易)

    介绍 邮件服务是常用的服务之一,作用很多,对外可以给用户发送活动.营销广告等:对内可以发送系统监控报告与告警. 本文将介绍Springboot如何整合邮件服务,并给出不同邮件服务商的整合配置. 如图所 ...

  5. 【SpringBoot】SpringBoot与SpringMVC自动配置(五)

    本文介绍SpringBoot对Spring MVC自动配置,SpringBoot自动配置原理可以参考:[SpringBoot]SpringBoot配置与单元测试(二) 首先新建一个SpringBoot ...

  6. springBoot系列-->springBoot注解大全

    一.注解(annotations)列表 @SpringBootApplication:包含了@ComponentScan.@Configuration和@EnableAutoConfiguration ...

  7. 【SpringBoot】SpringBoot 国际化(七)

    本周介绍SpringBoot项目的国际化是如何处理的,阅读本章前请阅读[SpringBoot]SpringBoot与Thymeleaf模版(六)的相关内容 国际化原理 1.在Spring中有国际化Lo ...

  8. 【SpringBoot】SpringBoot配置与单元测试(二)

    SpringBoot项目创建参考[SpringBoot]SpringBoot快速入门(一) 本文介绍SpringBoot项目的POM文件.配置与单元测试 POM文件 1.SpringBoot的pom文 ...

  9. SpringBoot(四) -- SpringBoot与Web开发

    一.发开前准备 1.创建一个SpringBoot应用,引入我们需要的模块 2.SpringBoot已经默认将这些场景配置好了,只需要在配置文件中指定少量配置,就能运行起来 3.编写业务代码 二.静态资 ...

随机推荐

  1. redis状态监控可视化工具RedisLive使用

    首先,别人写的工具,赞一下  github地址 https://github.com/nkrode/RedisLive 然后,fork一下,自己加点功能 gui介绍(直接copy的github图片) ...

  2. springJDBC和SpringJDBCTemplate解决方案探究

    先来看一个纯JDBC的例子,体会一下springJDBC和SpringJDBCTemplate两者的区别 一个Customer类 package com.mkyong.customer.model; ...

  3. Python自动化开发 - select模块

    介绍: IO-多路复用:监听多个socker对象是否有变化,包括可读.可写.发送错误 Python中的select模块专注于I/O多路复用,提供了select poll epoll三个方法(其中后两个 ...

  4. htpasswd建立和更新存储用户名、密码

    htpasswd建立和更新存储用户名.密码的文本文件, 用于对HTTP用户的basic认证. # /usr/local/apache/bin/htpasswd --help Usage: htpass ...

  5. 关于DFS和BFS的理解 以及坐标的定义

    http://blog.csdn.net/bool_isprime/article/details/5803018DFS: 1: 坐标类型搜索 :这种类型的搜索题目通常来说简单的比较简单,复杂的通常在 ...

  6. Mac怎么安装并配置Homebrew?

    1.在打开的命令行工具中输入如下语句: 复制内容到剪贴板 ruby -e "$(curl --insecure -fsSL https://raw.githubusercontent.com ...

  7. 记Asp.Net Core Swagger 使用 并带域接口处理

    引用作者原话:Asp.Net的WebApi中使用Swagger作为说明和测试的页面是非常不错的,比起WebApiTestClient来至少在界面上的很大的提升.但是使用Swagger时如果只是一般的控 ...

  8. 背水一战 Windows 10 (50) - 控件(集合类): ItemsControl - 基础知识, 数据绑定, ItemsPresenter, GridViewItemPresenter, ListViewItemPresenter

    [源码下载] 背水一战 Windows 10 (50) - 控件(集合类): ItemsControl - 基础知识, 数据绑定, ItemsPresenter, GridViewItemPresen ...

  9. 【react】当react框架遇上百度地图

      百度地图官方文档的使用指导是这样说的:在页面中引入<script type="text/javascript" src="http://api.map.baid ...

  10. delphi char数组、string和Pchar的相互转换

    因为要调用windows的api或者给vc++写接口,很多地方都要用到pchar,现在将char数组.string和pchar之间的相互转换都列出来,都是网上找的资料,我总结一下,先直接上代码,再讲原 ...