1.我们可以在项目启动的时候加载我们本项目的一些静态资源 下面就是一个关于微信 公众号开发过程中 对监听器的应用
1.创建一个类包括所有建泰资源 结构如下
package com.unicom.domain;
import java.util.HashMap;
import java.util.Map;
public class AppInfo {
public static Map<String, AppInfo> appInfos = new HashMap<String, AppInfo>();
private String app_id;// 微信公众号appid
private String app_secret;// 微信公众号appsecret
private String channel;// 渠道号
private String aes_key;// openid加密key
private String access_token;// 微信接口access_token
private String jsapi_ticket;// 微信接口access_token
private String oauth_app_code;// 统一认证code
private String oauth_app_secret;// 统一认证secret
private String oauth_url;// 统一认证回调登录url(包括回调地址)
private String txt;
private String customer_key; //拉起客服关键词
private String package_ids;//卡套餐类型id 用英文,(逗号)分隔开
private String custom_start_time;//公众号客服工作开始时间
private String custom_end_time;//公众号客服工作结束时间
public String getApp_id() {
return app_id;
}
public void setApp_id(String app_id) {
this.app_id = app_id;
}
public String getApp_secret() {
return app_secret;
}
public void setApp_secret(String app_secret) {
this.app_secret = app_secret;
}
public String getChannel() {
return channel;
}
public void setChannel(String channel) {
this.channel = channel;
}
public static Map<String, AppInfo> getAppInfos() {
return appInfos;
}
public static void setAppInfos(Map<String, AppInfo> appInfos) {
AppInfo.appInfos = appInfos;
}
public String getAes_key() {
return aes_key;
}
public void setAes_key(String aes_key) {
this.aes_key = aes_key;
}
public String getAccess_token() {
return access_token;
}
public void setAccess_token(String access_token) {
this.access_token = access_token;
}
public String getJsapi_ticket() {
return jsapi_ticket;
}
public void setJsapi_ticket(String jsapi_ticket) {
this.jsapi_ticket = jsapi_ticket;
}
public String getOauth_app_code() {
return oauth_app_code;
}
public void setOauth_app_code(String oauth_app_code) {
this.oauth_app_code = oauth_app_code;
}
public String getOauth_app_secret() {
return oauth_app_secret;
}
public void setOauth_app_secret(String oauth_app_secret) {
this.oauth_app_secret = oauth_app_secret;
}
public String getOauth_url() {
return oauth_url;
}
public void setOauth_url(String oauth_url) {
this.oauth_url = oauth_url;
}
public String getTxt() {
return txt;
}
public void setTxt(String txt) {
this.txt = txt;
}
public String getCustomer_key() {
return customer_key;
}
public void setCustomer_key(String customer_key) {
this.customer_key = customer_key;
}
public String getPackage_ids() {
return package_ids;
}
public void setPackage_ids(String package_ids) {
this.package_ids = package_ids;
}
public String getCustom_start_time() {
return custom_start_time;
}
public void setCustom_start_time(String custom_start_time) {
this.custom_start_time = custom_start_time;
}
public String getCustom_end_time() {
return custom_end_time;
}
public void setCustom_end_time(String custom_end_time) {
this.custom_end_time = custom_end_time;
}
@Override
public String toString() {
return "AppInfo [app_id=" + app_id + ", app_secret=" + app_secret
+ ", channel=" + channel + ", aes_key=" + aes_key
+ ", access_token=" + access_token + ", jsapi_ticket="
+ jsapi_ticket + ", oauth_app_code=" + oauth_app_code
+ ", oauth_app_secret=" + oauth_app_secret + ", oauth_url="
+ oauth_url + ", txt=" + txt + ", customer_key=" + customer_key
+ ", package_ids=" + package_ids + ", custom_start_time="
+ custom_start_time + ", custom_end_time=" + custom_end_time
+ "]";
}
}
2.建立监听器主类 结构如下
package com.unicom.listener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import com.alibaba.fastjson.JSONObject;
import com.unicom.domain.AppInfo;
import com.unicom.domain.wechatoauth.AccessToken;
import com.unicom.service.AppInfoService;
import com.unicom.service.WechatTokenService;
import com.unicom.util.HttpsUtil;
/**
* 启动监听器
*
* @author Storezhang
*/
@Component
public class StartupListener implements ApplicationListener<ContextRefreshedEvent> {
private static Logger log = LoggerFactory.getLogger(StartupListener.class);
@Autowired
AppInfoService appInfoService;
@Autowired
WechatTokenService tokenService;
// 获取微信公众号access_token 接口URL地址
private static final String ACCESS_TOKEN_URL = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET";
@Override
public void onApplicationEvent(ContextRefreshedEvent event) {
if (event.getApplicationContext().getParent() == null) {
// if (event.getApplicationContext().getDisplayName().equals("Root
// WebApplicationContext")) {
log.info("项目初始化");
appInfo();
token();
}
}
public void appInfo() {
// 加载appInfo
appInfoService.loadAppInfo();
// 加载自动回复
appInfoService.loadReply();
}
}
3.复习一下监听器的种类及作用
(1)、ContextLoaderListener
public class SpringListener implements ApplicationListener<ContextRefreshedEvent>{ public void onApplicationEvent(ContextRefreshedEvent event) { if (event.getApplicationContext().getParent() == null) { Init.init(); } } }