功能描述:
发送短信工具类:该工具类定义了静态发短信的方法,所有使用的用户名,密码都需要定义为静态,这里需要通过spring 读取配置文件注入
首先定义静态变量:
private static String host="";
private static String username="";
private static String password="";
下面是静态的发送短信的方法:
/**
* @param host
* @param username
* @param password
* @param SMSBean
* @return
*/
public static String [] sendSMS(SMSBean smsBean)
{
String smsContext;
HttpMethodBase method = null;
try
{
smsContext = URLEncoder.encode(smsBean.getContent(),"utf-8");
String Url = "http://www.kasms.net/Api/smssend.aspx?user=" + username
+ "&pass=" + password + "&telno=1&msg=" + smsContext
+ "&mobiles=" + smsBean.getSendmobile();
System.out.println(Url);
HttpClient client = new HttpClient();
method = new PostMethod(Url);
method.setRequestHeader("Content-Type","text/html; charset=utf-8");
method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
new DefaultHttpMethodRetryHandler(3,false));
int result = client.executeMethod(method);
if(result != HttpStatus.SC_OK)
{
System.err.println("Method failed: " + method.getStatusLine());
return new String []{"1","连接失败"};
}
}
catch(HttpException e)
{
System.out.println("服务器异常,请检查短信发送协议是否正确!");
e.printStackTrace();
return new String []{"1","连接失败"};
}
catch(IOException e)
{
System.out.println("IO异常,系统将自动重发3次。如一直失败!请检查网络是否正常!");
e.printStackTrace();
return new String []{"1","连接失败"};
}
finally
{
method.releaseConnection();
}
return new String []{"0","短信发送成功"};
}
后面是通过spring注入bean的get,set方法
错误就在这里:
/**
* @return the host
*/
public static String getHost()
{
return host;
}
/**
* @param host the host to set
*/
public static void setHost(String host)
{
SMSTools.host = host;
}
这里和平时的一看就不同,没有研究源码,应该是spring在读get,set方法时没有识别出来!
改成
/**
* @return the host
*/
public String getHost()
{
return host;
}
/**
* @param host the host to set
*/
public void setHost(String host)
{
SMSTools.host = host;
}
确行了...
分享到:
相关推荐
本篇文章主要介绍了spring 如何将配置信息注入静态变量的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
主要介绍了Spring不能注入Static变量的原因及Spring注入静态变量,需要的朋友可以参考下
主要介绍了spring boot静态变量注入配置文件的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
Struts2与Spring整合使用json报错解决办法[归纳].pdf
狂神spring-security静态资源.zip
主要为大家详细介绍了Spring boot工具类静态属性注入,及多环境配置详解,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
Spring依赖注入使用静态工厂设注入demo。完整可运行。
主要介绍了springboot 使用yml配置文件给静态变量赋值教程,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
参考: ...目录 一、更改镜像 二、手动将需要的插件和依赖下载放到本地厂库 三、下载阿里网站的资格证书,安装到jdk 四、设置maven的VM属性,这个成功解决问题 ...生成的spring boot项目,报错unable to f
spring-aspects源码,可以直接导入Eclipse工程中!
spring练习 刚刚学spring,做了个小小小项目,来加深理解。... ·其他依赖注入属性,都使用property 元素确定参数值。 Person.java PersonFactory.java Chinese.java American.java bean.xml SpringTest.java
我们在开发过程中经常会发生spring注入错误或失败的情况,其实Web服务器端在Action、dao、service包之内是很容易实现的,在这些包之外实现spring注入就不容易了,总是出错,其实根本原因是无法获得相应的spring上...
spring boot静态资源部署Spring Boot 是由 Pivotal 团队提供的全新框架,其设计目的是用来简化新 Spring 应用的初始搭建以及开发过程。该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置。
spring注入配置文件,使用注解注入的
结合spring框架实现的静态代理,接口回掉
spring Boot 报错RedisConnectionFailureException
NULL 博文链接:https://elfasd.iteye.com/blog/1908001
狂神说SpringSecurity静态资源.rar
spring-security静态资源
spring注入方式 set注入 构造注入 自动注入