springboot-31-springboot静态注入
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 = ;
public static final Integer FOOT_MARK_GEOLEVEL = ;
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 = "";
public static final Integer FOOT_MARK_MAX = ;
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静态注入的更多相关文章
- Springboot 工具类静态注入
用springboot搭了一个项目,里面要用到一个DictUtils,因为要用到DictMapper,在百度找了一些方法,最后用下面的方法能成功获取到DictMapper @Component pub ...
- Spring不能直接@autowired注入Static变量/ 关于SpringBoot的@Autowired 静态变量注入
昨天在编写JavaMail工具类的时候,静态方法调用静态变量,这是很正常的操作,当时也没多想,直接静态注入. @Component public class JavaMailUtil { @Autow ...
- Spring-Boot 内置静态资源文件地址修改
Spring-Boot 内置MVC静态文件地址修改 Why:1.Spring-Boot修改内置SpringMVC静态资源路径,提高项目目录结构的安全性.2.配置拦截路径时可以剔除静态文件拦截How:1 ...
- Springboot】Springboot整合邮件服务(HTML/附件/模板-QQ、网易)
介绍 邮件服务是常用的服务之一,作用很多,对外可以给用户发送活动.营销广告等:对内可以发送系统监控报告与告警. 本文将介绍Springboot如何整合邮件服务,并给出不同邮件服务商的整合配置. 如图所 ...
- 【SpringBoot】SpringBoot与SpringMVC自动配置(五)
本文介绍SpringBoot对Spring MVC自动配置,SpringBoot自动配置原理可以参考:[SpringBoot]SpringBoot配置与单元测试(二) 首先新建一个SpringBoot ...
- springBoot系列-->springBoot注解大全
一.注解(annotations)列表 @SpringBootApplication:包含了@ComponentScan.@Configuration和@EnableAutoConfiguration ...
- 【SpringBoot】SpringBoot 国际化(七)
本周介绍SpringBoot项目的国际化是如何处理的,阅读本章前请阅读[SpringBoot]SpringBoot与Thymeleaf模版(六)的相关内容 国际化原理 1.在Spring中有国际化Lo ...
- 【SpringBoot】SpringBoot配置与单元测试(二)
SpringBoot项目创建参考[SpringBoot]SpringBoot快速入门(一) 本文介绍SpringBoot项目的POM文件.配置与单元测试 POM文件 1.SpringBoot的pom文 ...
- SpringBoot(四) -- SpringBoot与Web开发
一.发开前准备 1.创建一个SpringBoot应用,引入我们需要的模块 2.SpringBoot已经默认将这些场景配置好了,只需要在配置文件中指定少量配置,就能运行起来 3.编写业务代码 二.静态资 ...
随机推荐
- AFNetworking网络请求数据
//创建AFNetworking的请求操作 AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWit ...
- 跟哥走,带你玩转Surface 2
以为自己是数码控?觉得自己买了Surface就无所不能?别逗了!来看哥怎么玩平板,也让你们这些小虾米看看,什么才叫“玩转”! 新技能Get:密码不在用键盘 什么?每次开机你还要输入一长串密码?都什么年 ...
- Uval4726-数形结合的思想
题意:给定一段01序列,求一段长度不小于L的连续序列,使其平均值最大 思路:一看就想到了斜率优化,但是用基本的推公示一直没推出来,看了别人的代码,像推出斜率的式子一直没弄出来..后来一看别人写的题解, ...
- C# if else 使物体在X轴循环移动
if( transform.position.x > -15 && transform.rotation.y == 0 ) { //小鸟X轴反方向移动速度 transform.p ...
- 定时任务 Wpf.Quartz.Demo.4
本文继续介绍定时任务 Wpf.Quartz.Demo.3的一些小细节, 代码也请前往第3节下载. 1.RichTextBox右键菜单 <RichTextBox.ContextMenu> ...
- node.js实现WebSocket
最近在学习“HTML5游戏开发实战”,其中第8章内容是使用WebSocket来构建多人游戏---<你画我猜>.然而在实现过程中,却一直出错: 客户端请求时,服务器端会报错并终止: 而浏览器 ...
- “全栈2019”Java多线程第二十九章:可重入锁与不可重入锁详解
难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java多 ...
- (干货) Android实现ImageVIew多点触控及双击缩放
支持多点触控,放大自由移动,双击可以放大缩小.直接上代码: package com.cbt.view; import android.content.Context; import android.g ...
- centos7 升级GCC版本到7.3.0
废话不多说,直接上shell,还是比较简单的.就是编译时间有点长... 都是以小时计的......,我刀片机上面一台虚拟机反正是等了3个小时 #必备组件安装 yum install -y gcc gc ...
- iOS-iOS9系统SEGV_ACCERR问题处理【v3.6.3的一些bug修复】
前言 最近APP不断地更新版本,却发现一些未知的错误导致崩溃,我把能测出来的错误,全部修复了,因为项目里集成了腾讯Bugly,看了下后台的崩溃,依旧千篇一律啊,然后就纠结了,很多SEGV_ACCERR ...