首页 > 代码库 > Spring IoC 在子类中为父类注入属性值
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); } }
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 在子类中为父类注入属性值
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。