问题描述
只有在dao层能得到数据,传不到Biz层,如果在Biz层 buyinfoDAO=new BuyinfoDAOImpl();就没问题了,但就没有依赖注入的意义了java代码: applicationContext.xml配置:<bean id="sessionFactory"class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"><property name="configLocations"><list><value>classpath:hibernate.cfg.xml</value></list></property></bean><!-- 配置Hibernate的事务管理器 --><!-- 使用HibernateTransactionManager类实现基于Hibernate的事务管理器 --><bean id="TxManager"class="org.springframework.orm.hibernate3.HibernateTransactionManager"><property name="sessionFactory" ref="sessionFactory" /></bean><!-- 定义事物通知,需要指定一个事务管理器 --><tx:advice id="txAdvice" transaction-manager="TxManager"><tx:attributes><tx:method name="get*" read-only="true" /><tx:method name="find*" read-only="true" /><tx:method name="search*" read-only="true" /><tx:method name="query*" read-only="true" /><tx:method name="add*" propagation="REQUIRED" /><tx:method name="del*" propagation="REQUIRED" /><tx:method name="update*" propagation="REQUIRED" /><tx:method name="do*" propagation="REQUIRED" /><tx:method name="*" read-only="true" /></tx:attributes></tx:advice><aop:config><!-- 定义那些方法应用这些规则 --><aop:pointcut id="serviceMethod" expression="execution(* com.jxc.biz.*.*(..))" /><!-- 将应用通知与应用规则方法组合 --><aop:advisor advice-ref="txAdvice" pointcut-ref="serviceMethod" /></aop:config><!-- 配置用户处理DAO --><bean id="buyinfoDAO" class="com.jxc.dao.impl.BuyinfoDAOImpl"><property name="sessionFactory" ref="sessionFactory"></property></bean><!-- 配置用户业务处理Biz --><bean id="buyinfoBiz" class="com.jxc.biz.impl.BuyinfoBizImpl"><property name="buyinfoDAO" ref="buyinfoDAO"></property></bean><!-- 配置用户处理Action --><bean id="buyinfoaction" class="com.jxc.action.BuyinfoAction"><property name="buyinfoBiz" ref="buyinfoBiz"></property></bean></beans> dao实现类package com.jxc.dao.impl;import java.util.List;import org.hibernate.Query;import org.hibernate.Session;import org.hibernate.Transaction;import org.springframework.orm.hibernate3.support.HibernateDaoSupport;import com.jxc.bean.Buyinfo;import com.jxc.dao.BuyinfoDAO;import com.jxc.util.HibernateSessionFactory;public class BuyinfoDAOImpl extends HibernateDaoSupport implements BuyinfoDAO { Session session=null; public List<Buyinfo> select() { session=HibernateSessionFactory.getSession(); String hql="from Buyinfo"; Query query = session.createQuery(hql); List list=query.list(); /* List<Buyinfo> list=this.getHibernateTemplate().find("from Buyinfo");*/用这种方法得不到值 return list; } /** * @param args */ public static void main(String[] args) { BuyinfoDAOImpl buyinfoDAOImpl=new BuyinfoDAOImpl(); List<Buyinfo> list=buyinfoDAOImpl.select(); System.out.println(list.size()); }} biz层:package com.jxc.biz.impl;import java.util.List;import com.jxc.bean.Buyinfo;import com.jxc.biz.BuyinfoBiz;import com.jxc.dao.BuyinfoDAO;import com.jxc.dao.impl.BuyinfoDAOImpl;public class BuyinfoBizImpl implements BuyinfoBiz { private BuyinfoDAO buyinfoDAO ; public void setBuyinfoDAO(BuyinfoDAO buyinfoDAO) { this.buyinfoDAO = buyinfoDAO; } /** * 查询进货记录 */ public List select() { //buyinfoDAO=new BuyinfoDAOImpl(); List<Buyinfo> list=null; try { list = this.buyinfoDAO.select(); } catch (Exception e) { e.printStackTrace(); } return list; } public static void main(String[] args){ BuyinfoBizImpl buy = new BuyinfoBizImpl(); List list=buy.select(); System.out.println(list.size()); } } package com.jxc.action;import java.util.List;import java.util.Map;import com.jxc.bean.Buyinfo;import com.jxc.biz.BuyinfoBiz;import com.jxc.biz.impl.BuyinfoBizImpl;import com.opensymphony.xwork2.ActionContext;import com.opensymphony.xwork2.ActionSupport;public class BuyinfoAction extends ActionSupport { private Buyinfo buy=null; private BuyinfoBiz buyinfoBiz; public Buyinfo getBuy() { return buy; } public void setBuy(Buyinfo buy) { this.buy = buy; } public void setBuyinfoBiz(BuyinfoBiz buyinfoBiz) { this.buyinfoBiz = buyinfoBiz; } @SuppressWarnings("unchecked") public String select(){ Map session = ActionContext.getContext().getSession(); BuyinfoBizImpl b=new BuyinfoBizImpl(); List list=null; try { list = b.select(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } session.put("buyinfo", list); return "select"; } }
解决方案
你在BuyinfoBizImpl 里边用main方法去测试的吗?这样肯定是不行的,需要用spring去管理bean才行。另写一个测试类ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");去初始化spring,然后用context.getBean去获取BuyinfoBiz,再进行测试。你看看完整的spring入门吧。http://liuzidong.iteye.com/blog/899420/
解决方案二:
你在main方法里相当于重新开启了一个线程,跟你配置的文件一点关系都没有了。所以如果你想测试需要先加载xml配置文件,这样通知spring管理bean,然后你才可以使用哦
解决方案三:
web容器启动的时候日志会打印已经初始化的对象,你在后台打印的信息里查询“buyinfoDAO”,如果查找到说明已经初始化,如果没查到说明没有初始化,然后再考虑是否注入成功的问题