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

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<WechatAccount> 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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值