日期:2014-05-18  浏览次数:20670 次

(提问)spring的一点疑问?
//调用
public   class   test   {
DBSetDAO   dbsetDAO;
public   DBSetDAO   getDbsetDAO()   {
return   dbsetDAO;
}
public   void   setDbsetDAO(DBSetDAO   dbsetDAO)   {
this.dbsetDAO   =   dbsetDAO;
}
public   List   getlist(String   lujing)   {
List   list=dbsetDAO.findAll();//可以连接,没有问题
System.out.println( "List: "+list.size());
}
}

//******连接数据库*********
public   class   DBSetDAO   extends   HibernateDaoSupport   {
public   DBSetDAO()   {
}
protected   void   initDao()   {
//   do   nothing
}
public   List   findAll()   {
try   {
return   getHibernateTemplate().find( "from   user ");
}   catch   (RuntimeException   re)   {
throw   re;
}
}
}

//********applicationContext.xml******

<beans>
<bean   id= "dataSource "   class= "org.springframework.jndi.JndiObjectFactoryBean ">
<property   name= "jndiName ">
<value> jdbc/epmis </value>
</property>
</bean>


<bean   id= "sessionFactory "   class= "org.springframework.orm.hibernate3.LocalSessionFactoryBean ">
<property   name= "dataSource ">
<ref   bean= "dataSource "   />
</property>
<property   name= "mappingResources ">
<list>
<value> com/ssh2/po/user.hbm.xml </value>
</list>
</property>
</bean>

<bean   id= "DBSetDAO "   class= "com.Hibernate.DBSetDAO ">
<property   name= "sessionFactory ">
<ref   local= "sessionFactory "   />
</property>
</bean>

<bean   id= "test "   name= "test "   class= "com.Hibernate.test ">
<property   name= "dbsetDAO ">
<ref   local= "DBSetDAO "   />
</property>
</bean>
</beans>

当我在别的类中使用
test   mytest=new   test   ();
List   list=mytest.getlist();
的时候报错!
不能查询出数据!
java.lang.NullPointerException
这是为什么?
我怎么修改一下!

------解决方案--------------------
怎么是直接就 new test();
应该是 ApplicationContext context = ClassPathXmlApplicationContex ( "applicationContext.xml ");
test mytest = (test)context.getBean( "test ");