您好,欢迎访问一九零五行业门户网

Springboot如何读取自定义pro文件注入static静态变量

springboot 读取pro文件注入static静态变量mailconfig.properties
#服务器mail.host=smtp.qq.com#端口号mail.port=587#邮箱账号mail.username=hzy_daybreak_lc@foxmail.com#邮箱授权码mail.password=vxbkycyjkceocbdc#时间延迟mail.timeout=25000#发送人mail.emailform=hzy_daybreak_lc@foxmail.com#发件人mail.personal=华夏衣裳#主题mail.subject=同袍用户激活#内容模板mail.html=您的邮箱验证码为:
mailconfig.java
/* * @(#)mailconfig.java created on 2019年9月11日 * copyright (c) 2019 zdsoft networks, inc. all rights reserved. * $id$ */package com.hxyc.config.properties; import org.springframework.beans.factory.annotation.value;import org.springframework.context.annotation.configuration;import org.springframework.context.annotation.propertysource;import org.springframework.stereotype.component; /** * @author huangzy * @version $revision: 1.0 $, $date: 2019年9月11日 上午10:29:35 $ */@configuration@propertysource(value = "classpath:config/mailconfig.properties", encoding = "utf-8")@componentpublic class mailconfig { public static string host; public static integer port; public static string username; public static string password; public static string emailform; public static string timeout; public static string personal; public static string html; public static string subject; /** * @return returns the host. */ public static string gethost() { return host; } /** * @param host * the host to set. */ @value("${mail.host}") public void sethost(string host) { mailconfig.host = host; } /** * @return returns the port. */ public static integer getport() { return port; } /** * @param port * the port to set. */ @value("${mail.port}") public void setport(integer port) { mailconfig.port = port; } /** * @return returns the username. */ public static string getusername() { return username; } /** * @param username * the username to set. */ @value("${mail.username}") public void setusername(string username) { mailconfig.username = username; } /** * @return returns the password. */ public static string getpassword() { return password; } /** * @param password * the password to set. */ @value("${mail.password}") public void setpassword(string password) { mailconfig.password = password; } /** * @return returns the emailform. */ public static string getemailform() { return emailform; } /** * @param emailform * the emailform to set. */ @value("${mail.emailform}") public void setemailform(string emailform) { mailconfig.emailform = emailform; } /** * @return returns the timeout. */ public static string gettimeout() { return timeout; } /** * @param timeout * the timeout to set. */ @value("${mail.timeout}") public void settimeout(string timeout) { mailconfig.timeout = timeout; } /** * @return returns the personal. */ public static string getpersonal() { return personal; } /** * @param personal * the personal to set. */ @value("${mail.personal}") public void setpersonal(string personal) { mailconfig.personal = personal; } /** * @return returns the html. */ public static string gethtml() { return html; } /** * @param html * the html to set. */ @value("${mail.html}") public void sethtml(string html) { mailconfig.html = html; } /** * @return returns the subject. */ public static string getsubject() { return subject; } /** * @param subject * the subject to set. */ @value("${mail.subject}") public void setsubject(string subject) { mailconfig.subject = subject; } }
springboot静态属性注入的解决第一种方式通过springboot组件初始化生命周期进行属性(对象)赋值
@componentpublic class dshwechatapiutil extends dshbasecontroller { @autowired private ithirdpartyauthdao thirdpartyauthdao; private static ithirdpartyauthdao staticthirdpartyauthdao; @postconstruct public void init() { staticthirdpartyauthdao = thirdpartyauthdao; } public static jsonobject getauthorizertoken(string componentaccesstoken, string authorizerappid, string authorizerrefreshtoken) { jsonobject returnobject = new jsonobject(); try { if (dshutils.isempty(componentaccesstoken)) { componentaccesstoken = staticthirdpartyauthdao.selectwechatvalue(dshconstants.wechat_params.component_access_token); } } catch (exception e) { e.printstacktrace(); } return returnobject; }}
可以看到,当dshwechatapiutil工具类组件进行初始化时,调用@postconstruct注解标注的方法,对静态变量进行了赋值。
第二种方式通过@value()注解
@value()注解不会对静态变量进行属性注入,通过第一种方式的思维,那么我们肯定得想个办法,在这个组件初始化时也来赋值。
第一种方式肯定也是可以的,先写一个属性,然后通过@value()注解对这个属性进行赋值,最后通过@postconstruct注解方式赋值给静态属性。
这里我们要采用另一个方式,这里的方式是通过set方法来赋值。属性是static修饰的,get方法也是static修饰的,但是set方法不能是static修饰,使用@value()注解来修饰set方法。
这样就能成功注入。
第三种方式第三种方式和第二种差不多,
@configurationproperties(prefix = projectconfig.project_prefix)public class projectconfig { public static final string project_prefix = "project"; /** * 系统版本号 */ private string version; /** * 项目名称 */ private string name; /** * 版权年份 */ private string copyrightyear; /** * 实例演示开关 */ private static boolean demoenabled; /** * 获取地址ip开关 */ private static boolean addressenabled; public string getversion() { return version; } public void setversion(string version) { this.version = version; } public string getname() { return name; } public void setname(string name) { this.name = name; } public string getcopyrightyear() { return copyrightyear; } public void setcopyrightyear(string copyrightyear) { this.copyrightyear = copyrightyear; } public boolean isdemoenabled() { return demoenabled; } public void setdemoenabled(boolean demoenabled) { projectconfig.demoenabled = demoenabled; } public static boolean isaddressenabled() { return addressenabled; } public void setaddressenabled(boolean addressenabled) { projectconfig.addressenabled = addressenabled; }}
如上述代码,只要把set方法设置为非静态,那么这个配置类的静态属性就能成功注入了。
以上就是springboot如何读取自定义pro文件注入static静态变量的详细内容。
其它类似信息

推荐信息