码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2015-01-23 16:18:26      阅读:881      评论:0      收藏:0      [点我收藏+]

标签:spring   ioc   父类属性的注入   parent   postconstruct   

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 p_3er@qq.com
 * @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对象注入到了它的父类中去。

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

标签:spring   ioc   父类属性的注入   parent   postconstruct   

原文地址:http://blog.csdn.net/jrainbow/article/details/43057001

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!