相信大家在开发的过程中,都会写一些配置文件或者配置类来,毕竟好的编码习惯是不能硬编码的,所以配置文件和配置类就显得很重要了。但是我用久了之后发现,配置文件和配置类确实好用,但是假如有多个配置的话,那么得写多个配置类或者写多个配置文件,这显得有点麻烦,毕竟到时找起来想必也是一脸懵逼。
然而最近博主开始把配置写在了spring的xml文件中,发现,根本不需要写那么多的配置类和配置文件了,只需要一个专门对应的xml和类就可以了,哈哈哈,这样找起来或者理解起来想必也是极好啊。
好了,接下来我就以一个阿里巴巴大鱼短信的配置来做个例子吧。
首先,我们写一个类能够让spring.xml能够自动注入。
/***
* 短信配置模版类
*
* @author YiXuan.Chen
*
*/
public class AlibabaConfig {
// 短信请求url
private static String url;
// TOP分配给应用的AppKey
private static String appkey;
// 密钥
private static String secret;
// 公共回传参数
private static String extend;
// 短信签名
private static String freeSignName;
// 短信类型
public static String sms_type;
// 短信模板ID
public static String sms_template_code;
//模板key/value
public static String smsParamString;
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getAppkey() {
return appkey;
}
public void setAppkey(String appkey) {
this.appkey = appkey;
}
public String getSecret() {
return secret;
}
public void setSecret(String secret) {
this.secret = secret;
}
public String getExtend() {
return extend;
}
public void setExtend(String extend) {
this.extend = extend;
}
public String getSms_type() {
return sms_type;
}
public void setSms_type(String sms_type) {
this.sms_type = sms_type;
}
public String getFreeSignName() {
return freeSignName;
}
public void setFreeSignName(String freeSignName) {
this.freeSignName = freeSignName;
}
public String getSms_template_code() {
return sms_template_code;
}
public void setSms_template_code(String sms_template_code) {
this.sms_template_code = sms_template_code;
}
public String getSmsParamString() {
return smsParamString;
}
public void setSmsParamString(String smsParamString) {
this.smsParamString = smsParamString;
}
接下来来写spring-configure.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<!-- 短信模板1 -->
<bean id="alibabaConfigContractSign" class="com.maimaiti.sst.comm.constant.AlibabaConfig" >
<!-- 调用的类 -->
<property name="url" value="http://TEST" />
<property name="appkey" value="1111111" />
<property name="secret" value="1111111" />
<property name="extend" value="1111111" />
<property name="sms_type" value="1111111" />
<property name="freeSignName" value="1111" />
<property name="sms_template_code" value="SMS_1" />
<property name="smsParamString" value="11111" />
</bean>
<!-- 短信模板2 -->
<bean id="alibabaConfigChallengeCode" class="com.maimaiti.sst.comm.constant.AlibabaConfig" >
<!-- 调用的类 -->
<property name="url" value="http://TEST2" />
<property name="appkey" value="22222222" />
<property name="secret" value="22222222" />
<property name="extend" value="22222222" />
<property name="sms_type" value="22222222" />
<property name="freeSignName" value="22222222" />
<property name="sms_template_code" value="SMS_2" />
<property name="smsParamString" value="222222" />
</bean>
</beans>
然后你在你的spring.xml里加载这个xml就可以了。
<import resource="spring-configure.xml"/>
好的,大功告成.
接下来你就可以在用在这个模板类的地方用byName的方式自动注入了。
假如你有个A类要用到短信模板1和短信模板2
那么你就可以在A类里面这样写。
public class A{
//短信模板1
@Resource(name = "alibabaConfigContractSign")
AlibabaConfig alibabaConfigContractSign;
<pre name="code" class="java"> //短信模板2
@Resource(name = "alibabaConfigChallengeCode")
AlibabaConfig alibabaConfigContractSign2;
/*** * 发送短信短链接给用户 * @author YiXuan.Chen */public String sendContractMessage(BusinessContract businessContract){//******你的业务流程
alibabaConfigContractSign.getAppkey();//1111111
alibabaConfigContractSign2.getAppkey();//22222222
}}
我感觉这样比写配置类或者配置文件简介易懂多了。
好的,如果大家有什么疑问,或者觉得我说错了,可以在评论下面说出你的观点。
共同学习。