springMvc 监听器的应用

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

    作用:在启动Web容器时,自动装配Spring applicationContext.xml的配置信息。
        因为它实现了ServletContextListener这个接口,在web.xml配置这个监听器,启动容器时,就会默认执行它 实现的方法。在ContextLoaderListener中关联了ContextLoader这个类,所以整个加载配置过ContextLoader来完成。 以下是web.xml中ContextLoaderListener的配置和context的配置。
    
      context-param
    
    
代码 :   
    
public class SpringListener implements ApplicationListener<ContextRefreshedEvent>{

    public void onApplicationEvent(ContextRefreshedEvent event) {
        if (event.getApplicationContext().getParent() == null) {
            Init.init();
        }
    }


}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值