...
NewsDAO newsDao = new NewsDAO(); 
去掉
然后在类里面
private NewsDAO newsDao;
然后产生get/set方法
  public static void main(String args[]){ News news = new News(); 
news.setTitle("1212"); 
news.setContent("425534535"); 
newsDao.save(news); 

解决方案 »

  1.   

    因为NewsDAO已经通过applicationContext.xml依赖注入了
      

  2.   

    不能通过new的方式来创建dao
    NewsDAO newsDao = new NewsDAO(); 
    这样写是找不到getHibernateTemplate()的,所以就会报空指针异常;
    因为基于spring的程序在创建实例时是由spring容器来创建并管理的,也就是spring bean;
    <bean id="NewsDAO" class="com.heby.dao.NewsDAO" > 
    ……这时你想用这个NewsDAO(bean的id最好第一个字母小写),就需要通过spring的上下文查找这个bean;这就需要看你用何种方式启动spring了。
    举个例子,我们可以这样操作:
    ApplicationContext ctx = SpringUtils.getInstance();
    NewsDAO newsDAO = (NewsDAO)ctx.getBean("NewsDAO");
    newsDAO.save(news);SpringUtils.java:public final class SpringUtils {
        private static  Collection files = new ArrayList();
        /** Creates a new instance of SpringUtil */
        private SpringUtils() {
        }
        
            // 根据具体情况自己修改
            files.add("etc//applicationContext.xml");
            
        }
        
        public static ApplicationContext getInstance() {
            return applicationContext;
        }
        
        private static  ApplicationContext applicationContext =
                new FileSystemXmlApplicationContext((String[]) files.toArray(new String[0]));
    }你试试,如果还不行的话请把新问题发上来,大家共同解决。
      

  3.   

    谢谢两位,但还是有问题
    IamHades的报的错:
    org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'NewsDAO' is defined
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:360)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedBeanDefinition(AbstractBeanFactory.java:686)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:219)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:145)
    at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:537)
    at com.heby.test.main(test.java:44)
    Exception in thread "main" lhzx_zjg的报的错
    Cannot make a static reference to the non-static field newsDao