静态方法调用注入对象(springMvc)

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

java代码:

package com.its.mmo.common.web;

import java.util.List;

import javax.annotation.PostConstruct;

import org.apache.commons.collections.CollectionUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.its.mmo.MmoException;
import com.its.mmo.Status;
import com.its.mmo.common.dao.WechatAccountDao;
import com.its.mmo.common.model.WechatAccount;

@Controller
public class WechatAccountCtrl{
	
	private final static Logger logger=Logger.getLogger(WechatAccountCtrl.class);
	@Autowired
	private WechatAccountDao wechatAccountDao; //注入对象
	private static WechatAccountCtrl wechatAccountCtrl;
	
	@PostConstruct   //利用PostConstruct注解初始化赋值
	public void init(){
		wechatAccountCtrl=this;
		wechatAccountCtrl.wechatAccountDao=this.wechatAccountDao;
	}
	
	/**
	 * 根据品牌id查询微信账户
	 * @param brandId
	 * @throws MmoException
	 */
	public static WechatAccount getWechatAccount(String brandId) throws MmoException {
		List accounts = null;
		try {
			accounts = wechatAccountCtrl.wechatAccountDao.queryWxInfo(brandId);
		} catch (Exception e) {
			logger.error("根据品牌id查询微信账户错误", e);
			throw new MmoException(Status.SERVICE_FAIL, "根据品牌id查询微信账户错误", e);
		}
		if (CollectionUtils.isEmpty(accounts)) {
			logger.info("该品牌不存在微信账户");
			throw new MmoException(Status.SERVICE_FAIL, "该品牌不存在微信账户");
		}
		return accounts.get(0);
	}
	
	
}

 

转载于:https://my.oschina.net/ludd79806329/blog/749587

你可能感兴趣的:(静态方法调用注入对象(springMvc))