Java 为什么我的sessionFactory在从hibernate.cfg.xml切换到Autowired时显示空异常?

Java 为什么我的sessionFactory在从hibernate.cfg.xml切换到Autowired时显示空异常?,java,spring,hibernate,spring-mvc,annotations,Java,Spring,Hibernate,Spring Mvc,Annotations,我已经分别使用hibernate配置文件尝试了这段代码,效果很好。但我没有使用2个xml文件,而是将所有hibernate.cfg.xml数据放在spring-servlet.xml中,并尝试使用自动连线注释。但它给了我们空指针异常 我的代码结构是 我的代码是 Student.java ControllerStudent.java StudentDao.java ApplicationContext.xml spring-servlet.xml org.hibernate.dial.Or

我已经分别使用hibernate配置文件尝试了这段代码,效果很好。但我没有使用2个xml文件,而是将所有hibernate.cfg.xml数据放在spring-servlet.xml中,并尝试使用自动连线注释。但它给了我们空指针异常

我的代码结构是

我的代码是

  • Student.java
  • ControllerStudent.java
  • StudentDao.java
  • ApplicationContext.xml
  • 
    
  • spring-servlet.xml
  • 
    org.hibernate.dial.Oracle8iDialect
    真的
    更新
    
  • web.xml
  • 
    Web应用程序创建的原型
    春天
    org.springframework.web.servlet.DispatcherServlet
    春天
    /
    
  • pom.xml
  • 
    4.0.0
    com.wipro
    

    我不明白错误在哪里。我只在stackoverflow见过类似的其他问题,但没有一个答案对我有帮助。请帮我看一下这段代码。

    您使用的是XML,因此必须通过使用
    或组件扫描来指示Spring使用注释。您的代码有缺陷。请不要使用
    new ClassPathApplicationContext
    使用依赖项注入。另一件事是不要混合来自不同版本框架的jar。在本例中,您混合了来自Spring的5.2.1和5.2.6 JAR。您使用的是XML,因此必须通过使用
    或组件扫描来指示Spring使用注释。您的代码有缺陷。请不要使用
    new ClassPathApplicationContext
    使用依赖项注入。另一件事是不要混合来自不同版本框架的jar。在本例中,您将混合来自Spring的5.2.1和5.2.6罐。
    package com.wipro.bean;
    
    import javax.persistence.Column;
    import javax.persistence.Entity;
    import javax.persistence.Id;
    import javax.persistence.Table;
    
    @Entity
    @Table(name="Student")
    public class Student  {
        @Id
        private String studentId;
        @Column
        private String studentName;
        private String studentSubject;
        public String getStudentId() {
            return studentId;
        }
        public void setStudentId(String studentId) {
            this.studentId = studentId;
        }
        public String getStudentName() {
            return studentName;
        }
        public void setStudentName(String studentName) {
            this.studentName = studentName;
        }
        public String getStudentSubject() {
            return studentSubject;
        }
        public void setStudentSubject(String studentSubject) {
            this.studentSubject = studentSubject;
        }
        @Override
        public String toString() {
            return "Student [studentId=" + studentId + ", studentName=" + studentName + ", studentSubject=" + studentSubject
                    + "]";
        }
        
        
    }
    
    package com.wipro.controller;
    
    import java.util.List;
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.Model;
    import org.springframework.web.bind.annotation.RequestMapping;
    
    import com.wipro.dao.StudentDao;
    
    @Controller
    public class ControllerStudent {
        
        private ApplicationContext conn;
        
        
        @RequestMapping("/show")
        public String view3(Model m) {
            conn=new ClassPathXmlApplicationContext("ApplicationContext.xml");
            StudentDao obj=conn.getBean("dao",StudentDao.class);
            List l=obj.showData();
            m.addAttribute("msg",l);
            return "show";
        }
        
    }
    
    package com.wipro.dao;
    
    import java.util.List;
    
    import javax.transaction.Transactional;
    
    import org.hibernate.Session;
    import org.hibernate.SessionFactory;
    import org.hibernate.query.Query;
    import org.springframework.beans.factory.annotation.Autowired;
    
    public class StudentDao {
    
        @Autowired
        private SessionFactory sessionFactory;
        
        private Session session;
        
    public List showData() {
            
            //factory =new Configuration().configure(new File("F:/Wipro teachings/springg/crudStudent/src/main/java/com/wipro/bean/hibernate.cfg.xml")).buildSessionFactory();
                session=sessionFactory.openSession();
                Query q=session.createQuery("from Student");
                List list=q.list();
                return list;
            
        }
    }
    
    <?xml version="1.0" encoding="UTF-8"?>
    <beans  
        xmlns="http://www.springframework.org/schema/beans"  
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
        xmlns:p="http://www.springframework.org/schema/p"  
        xsi:schemaLocation="http://www.springframework.org/schema/beans  
                   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
             
     <bean id="dao" class="com.wipro.dao.StudentDao"></bean>              
    </beans> 
    
    <?xml version="1.0" encoding="UTF-8"?>
    
    <beans xmlns="http://www.springframework.org/schema/beans"  
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:jee="http://www.springframework.org/schema/jee"
    xmlns:lang="http://www.springframework.org/schema/lang"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:util="http://www.springframework.org/schema/util"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
        xmlns:context="http://www.springframework.org/schema/context"  
        xmlns:mvc="http://www.springframework.org/schema/mvc"  
        xsi:schemaLocation="  
            http://www.springframework.org/schema/beans  
            http://www.springframework.org/schema/beans/spring-beans.xsd  
            http://www.springframework.org/schema/context  
            http://www.springframework.org/schema/context/spring-context.xsd  
            http://www.springframework.org/schema/mvc  
            http://www.springframework.org/schema/mvc/spring-mvc.xsd  
            http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop.xsd  
    http://www.springframework.org/schema/jee
    http://www.springframework.org/schema/jee/spring-jee.xsd
    http://www.springframework.org/schema/lang
    http://www.springframework.org/schema/lang/spring-lang.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx.xsd
    http://www.springframework.org/schema/util
    http://www.springframework.org/schema/util/spring-util.xsd"> 
            
    
            
            <context:component-scan base-package="com.wipro"></context:component-scan>
            
            <mvc:annotation-driven></mvc:annotation-driven>
            
            
            
            <bean id="vr" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
                <property name="prefix" value="/WEB-INF/views/"></property>
                <property name="suffix" value=".jsp"></property>
            </bean>
            
             <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
            <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
            <property name="url" value="jdbc:oracle:thin:@localhost:1521:orcl" />
            <property name="username" value="sys as sysdba" />
            <property name="password" value="Muskan123" />
        </bean>
        
        <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
            <property name="dataSource" ref="dataSource" />
            <property name="hibernateProperties">
                <props>
                    <prop key="hibernate.dialect">org.hibernate.dialect.Oracle8iDialect</prop>
                    <prop key="hibernate.show_sql">true</prop>
                    <prop key="hibernate.hbm2ddl.auto">update</prop>
                </props>
            </property>
            <property name="packagesToScan" value="com.wipro.bean" />
        </bean>
        
        <bean id="hibernateTemplate" class="org.springframework.orm.hibernate5.HibernateTemplate">
            <property name="sessionFactory" ref="sessionFactory"></property>
        </bean>
        
        <tx:annotation-driven />
        
        <bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
            <property name="sessionFactory" ref="sessionFactory" />
        </bean> 
            
            <context:annotation-config />
            </beans>
    
    <!DOCTYPE web-app PUBLIC
     "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
     "http://java.sun.com/dtd/web-app_2_3.dtd" >
    
    <web-app>
      <display-name>Archetype Created Web Application</display-name>
        
        <servlet>
        <servlet-name>spring</servlet-name>
        <servlet-class> org.springframework.web.servlet.DispatcherServlet</servlet-class>
        </servlet>
        
        <servlet-mapping>
            <servlet-name>spring</servlet-name>
            <url-pattern>/</url-pattern>
        </servlet-mapping>
    
    
    </web-app>
    
    <?xml version="1.0" encoding="UTF-8"?>
    
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
      <modelVersion>4.0.0</modelVersion>
    
      <groupId>com.wipro</groupId>
      <artifactId>crudStudent</artifactId>
      <version>0.0.1-SNAPSHOT</version>
      <packaging>war</packaging>
    
      <name>crudStudent Maven Webapp</name>
      <!-- FIXME change it to the project's website -->
      <url>http://www.example.com</url>
    
      <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.7</maven.compiler.source>
        <maven.compiler.target>1.7</maven.compiler.target>
      </properties>
    
      <dependencies>
        <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <version>4.11</version>
          <scope>test</scope>
        </dependency>
        
          
            <dependency>  
            <groupId>org.springframework</groupId>  
            <artifactId>spring-core</artifactId>  
            <version>5.2.1.RELEASE</version>  
        </dependency> 
         
        <dependency>  
            <groupId>org.springframework</groupId>  
            <artifactId>spring-web</artifactId>  
            <version>5.2.1.RELEASE</version>  
        </dependency>  
        
        <dependency>  
            <groupId>org.springframework</groupId>  
            <artifactId>spring-webmvc</artifactId>  
            <version>5.1.1.RELEASE</version>  
        </dependency>  
      
        <dependency>
          <groupId>org.hibernate</groupId>
          <artifactId>hibernate-core</artifactId>
          <version>5.4.9.Final</version>
        </dependency>
        
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-orm</artifactId>
          <version>5.2.1.RELEASE</version>
        </dependency>
        
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-tx</artifactId>
          <version>5.2.6.RELEASE</version>
        </dependency>
        
        <dependency>
          <groupId>commons-dbcp</groupId>
          <artifactId>commons-dbcp</artifactId>
          <version>1.2.2</version>
        </dependency>
        
        <dependency>
          <groupId>com.oracle.database.jdbc</groupId>
          <artifactId>ojdbc6</artifactId>
          <version>11.2.0.4</version>
        </dependency> 
        
        
      </dependencies>
    
      <build>
        <finalName>crudStudent</finalName>
        <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
          <plugins>
            <plugin>
              <artifactId>maven-clean-plugin</artifactId>
              <version>3.1.0</version>
            </plugin>
            <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
            <plugin>
              <artifactId>maven-resources-plugin</artifactId>
              <version>3.0.2</version>
            </plugin>
            <plugin>
              <artifactId>maven-compiler-plugin</artifactId>
              <version>3.8.0</version>
            </plugin>
            <plugin>
              <artifactId>maven-surefire-plugin</artifactId>
              <version>2.22.1</version>
            </plugin>
            <plugin>
              <artifactId>maven-war-plugin</artifactId>
              <version>3.2.2</version>
            </plugin>
            <plugin>
              <artifactId>maven-install-plugin</artifactId>
              <version>2.5.2</version>
            </plugin>
            <plugin>
              <artifactId>maven-deploy-plugin</artifactId>
              <version>2.8.2</version>
            </plugin>
          </plugins>
        </pluginManagement>
      </build>
    </project>
    
    <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
        pageEncoding="ISO-8859-1"%>
        <%@page isELIgnored="false" %>
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="ISO-8859-1">
    <title>Insert title here</title>
    </head>
    <body>
    <h2>${msg}</h2>
    </body>
    </html>
    
    <html>
    <body>
    <h2>Hello World!</h2>
    
    <a href="show">Show Students</a>
    
    </body>
    </html>