线程“main”java.lang.ExceptionInInitializerError中的异常

时间:2015-04-26 14:52:15

标签: spring

目前我很想尝试使用MySQL实现数据源,但我遇到了错误,但无法理解主要原因或错误。

Exception in thread "main" java.lang.ExceptionInInitializerError    
Apr 26, 2015 8:09:33 PM org.springframework.context.support.AbstractApplicationContext prepareRefresh
    INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@4ee70b: startup date [Sun Apr 26 20:09:33 IST 2015]; root of context hierarchy
    Exception in thread "main" java.lang.ExceptionInInitializerError
        at org.springframework.context.support.AbstractRefreshableApplicationContext.createBeanFactory(AbstractRefreshableApplicationContext.java:194)
        at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:127)
        at org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:465)
        at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:395)
        at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
        at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
        at com.rajdeo.jdbcspring.JdbcDemo.main(JdbcDemo.java:12)
    Caused by: java.lang.NullPointerException
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.<clinit>(DefaultListableBeanFactory.java:104)
        ... 7 more

JdbcDemo.java

package com.x.jdbcspring;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.x.jdbcspring.JdbcDaoImpl;

public class JdbcDemo {
    public static void main(String[] args) {
    ApplicationContext ctx= new ClassPathXmlApplicationContext("spring.xml");
        JdbcDaoImpl dao=ctx.getBean("JdbcDaoImpl",JdbcDaoImpl.class);

        /*
        //JdbcDaoImpl dao=new JdbcDaoImpl();
        Student student=ctx.getStudent(1);
        System.out.println("name::\t"+student.getName());

        JdbcDaoImpl jdbc=new JdbcDaoImpl();
        jdbc.getStudent(1);

        System.out.println(jdbc.getStudent(1).getId());*/
    }
}

JdbcDaoImpl.java

package com.x.jdbcspring;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


@Component
public class JdbcDaoImpl {


    @Autowired
    private DataSource datasource;


    public DataSource getDatasource() {
        return datasource;
    }


    public void setDatasource(DataSource datasource) {
        this.datasource = datasource;
    }


    public Student getStudent(int id){

        Connection  con = null;
         Student student=null;
           try{
        /*      Class.forName("com.mysql.jdbc.Driver");
              System.out.println("Connecting to database...");*/
              con = datasource.getConnection();
              PreparedStatement stmt=con.prepareStatement("select * from contact where contact_id =?");
              stmt.setInt(1, id);

              //Excute the query
              ResultSet rs=stmt.executeQuery();
              while(rs.next()){
                  String name=rs.getString("name");
                  student=new Student(id, name);
              }

            //close the statement and Resultset
            stmt.close();
            rs.close();

            return student;
        }catch(Exception e){
            e.printStackTrace();
        }

        finally{
            if(con!=null)
                try {
                    con.close();
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

        }       
        return student;
    }

}

Student.java

package com.rajdeo.jdbcspring;

public class Student {

    private int id;
    private String name;
    public Student(int id, String name) {
        // TODO Auto-generated constructor stub
        this.id = id;
        this.name = name;
    }

    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}

spring.xml

<?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:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.1.xsd">


<context:annotation-config />
<context:component-scan base-package="com.rajdeo" />

<bean id="datasource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://localhost:3306/student" />
        <property name="username" value="root" />
        <property name="password" value="xxxx" />
</bean>
</beans>

什么是错误我无法理解任何帮助表示赞赏。

2 个答案:

答案 0 :(得分:0)

看起来你从上下文中获取了一个bean

ctx.getBean("JdbcDaoImpl",JdbcDaoImpl.class)

但在spring.xml中没有定义此bean。因此,您获得NPE = java.lang.NullPointerException

要解决此问题,您必须配置JdbcDaoImpl服务。 E.g。

<bean id="datasource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://localhost:3306/student" />
        <property name="username" value="root" />
        <property name="password" value="xxxx" />
</bean>    
<bean id="JdbcDaoImpl"
      class="org.springframework.security.core.userdetails.jdbc.JdbcDaoImpl">
  <property name="datasource" ref="datasource"/>
</bean> 

答案 1 :(得分:0)

您的spring.xml说要扫描包com.rajdeo以获取组件,但您的班级JdbcDaoImpl位于包com.x.jdbcspring内,因此即使它已被收集,也不会被提取有一个@Component注释。

确保在组件扫描过程中包含包含JdbcDaoImpl的包:

<context:component-scan base-package="com.rajdeo, com.x.jdbcspring" />