Spring IoC 在子类中为父类注入属性值

1、基类

package com.grgbanking.it.base;

import com.grgbanking.it.wx.common.creator.WXBaseMessageCreator;
import com.grgbanking.it.wx.entity.WXPassiveMessage;
import com.grgbanking.it.wx.entity.WXVerify;

public class WXConnectionService implements IWXConnectionService{
	protected WXBaseMessageCreator messageCreator;
	
	public void setMessageCreator(WXBaseMessageCreator messageCreator) {
		this.messageCreator = messageCreator;
	}
	
	/**
	 * 获取回调校验的值
	 * verify
	 * 
	 * @param verify
	 * @throws Exception 
	 * @exception 
	 * @since  1.0.0
	 */
	@Override
	public String getEcho(WXVerify verify) throws Exception {
		return messageCreator.getEcho(verify);
	}
	/**
	 * 接收微信过来的信息
	 * reciveMsg
	 * 
	 * @param verify
	 * @return
	 * @throws Exception 
	 * @exception 
	 * @since  1.0.0
	 */
	public WXPassiveMessage reciveMsg(WXVerify verify)throws Exception {
		return messageCreator.reciveMessage(verify);
	}
}



2、子类

package com.grgbanking.it.external.service.impl;

import java.util.List;

import javax.annotation.PostConstruct;

import org.apache.commons.lang.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.grgbanking.it.base.WXConnectionService;
import com.grgbanking.it.common.entity.PageQuery;
import com.grgbanking.it.common.json.JSONHelper;
import com.grgbanking.it.common.util.DateUtil;
import com.grgbanking.it.common.util.OAMessager;
import com.grgbanking.it.common.util.poi.POIShellContructor;
import com.grgbanking.it.external.component.WXSupplierMessageCreator;
import com.grgbanking.it.external.dao.ISupplierDao;
import com.grgbanking.it.external.dao.ISupplierMsnDao;
import com.grgbanking.it.external.entity.Supplier;
import com.grgbanking.it.external.entity.SupplierMsn;
import com.grgbanking.it.external.service.ISupplierService;
import com.grgbanking.it.system.component.WXOrganizationCreator;
import com.grgbanking.it.system.dao.ISystemPropertyDao;
import com.grgbanking.it.system.entity.SystemProperty;

/**
 * 供应商管理
 * 
 * SupplierMsnService
 * 
 * @author 潘广伟
 * @Email [email protected]
 * @Date 2014-12-31 上午9:48:47
 * 
 * @version 1.0.0
 *
 */
@Service
public class SupplierService extends WXConnectionService implements ISupplierService {
	@Autowired
	private ISupplierMsnDao<SupplierMsn, Integer> supplierMsnDao;
	@Autowired
	private ISupplierDao<Supplier, String> supplierDao;
	@Autowired
	private ISystemPropertyDao<SystemProperty, String> systemPropertyDao;
	@Autowired
	private WXSupplierMessageCreator messageCreator;
	@Autowired
	private WXOrganizationCreator organizationCreator;
	
	/**
	 * 为父类中的messageCreator初始化
	 * initMessageCreator
	 *  
	 * @exception 
	 * @since  1.0.0
	 */
	@PostConstruct
	public void initMessageCreator() {
		super.setMessageCreator(messageCreator);
	}
	
	
	/**
	 * 分页查询供应商信息
	 * findByPage
	 * 
	 * @param query
	 * @param keyword
	 * @return 
	 * @exception 
	 * @since  1.0.0
	 */
	@Override
	public String findByPage(PageQuery query, String keyword) {
		String hql = "from Supplier t";
		
		List<Supplier> list = supplierDao.pageByHQL(hql, query.getStart(), query.getLimit());
		Long total = supplierDao.getTotalByHQL(hql);
		return JSONHelper.convertToGrid(total, list);
	}
	
	...
}


3、简单解释

@PostConstruct是Java EE 5引入的注解,Spring允许开发者在受管Bean中使用它。当DI容器实例化当前受管Bean时,@PostConstruct注解的方法会被自动触发,从而完成一些初始化工作。

上面2中的子类在initMessageCreator()方法中,将messageCreator对象注入到了它的父类中去。

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。