java - 错误: org. springframework.web.context.ContextLoader - 上下文初始化失败org.springframework.beans.factory.BeanCreationException

标签 java xml spring hibernate spring-mvc

我正在 tomcat 7 中开发网站(spring 3.1.1),但出现错误

ERROR: org.springframework.web.context.ContextLoader - Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sqlSessionFactory' defined in class path resource [config/applicationContext.xml]: Invocation of init method failed; nested exception is org.springframework.core.NestedIOException: Failed to parse config resource: class path resource [config/SqlMapConfig.xml]; nested exception is org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration. Cause: java.lang.NullPointerException
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1455)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
	at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294)
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:567)
	at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:913)
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:464)
	at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:385)
	at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:284)
	at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:111)
	at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:5017)
	at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5531)
	at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
	at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1574)
	at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1564)
	at java.util.concurrent.FutureTask.run(FutureTask.java:266)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
	at java.lang.Thread.run(Thread.java:745)

我多次尝试解决这个错误,但都失败了。我该如何解决这个错误?我需要你的帮助。

这是我的 SqlMapConfig.xml、applicationContext 和我的根上下文。

我的SqlMapConfig.xml(config/SqlMapConfig.xml)

<?xml version="1.0" encoding="UTF-8" ?>  
<!DOCTYPE mapper   
    PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"   
    "http://mybatis.org/dtd/mybatis-3-mapper.dtd">  
 
<mapper namespace="BoardDao">  
 
    <select id="selectList" parameterType="map" resultType="BoardCommand">
        SELECT
            seq,
            writer,
            title,
            content,
            pwd,
            hit,
            regdate,
            filename
        FROM (
            SELECT
                a.*,
                rownum rnum
            FROM (
                SELECT
                    *
                FROM springboardtest
        <where>
            <if test ="keyWord != '' and keyField =='title'">
                title like '%' ||#{keyWord}||'%'
            </if>
            <if test ="keyWord != '' and keyField =='writer'">
                writer like '%' ||#{keyWord}||'%'
            </if>
            <if test ="keyWord != '' and keyField =='content'">
                content like '%' ||#{keyWord}||'%'
            </if>
            <if test ="keyWord != '' and keyField =='all'">
                content like '%' ||#{keyWord}||'%' or
                title like '%' ||#{keyWord}||'%' or
                writer like '%' ||#{keyWord}||'%'
            </if>
        </where>
                ORDER BY seq desc)a)
        <![CDATA[
        WHERE rnum >= #{start} AND rnum <= #{end}        
        ]]>            
    </select>
    
    <select id="selectCount" parameterType="map" resultType="Integer">
        SELECT
            count(*)
        FROM springboardtest
        <where>
            <if test ="keyWord != '' and keyField =='title'">
                title like '%' ||#{keyWord}||'%'
            </if>
            <if test ="keyWord != '' and keyField =='writer'">
                writer like '%' ||#{keyWord}||'%'
            </if>
            <if test ="keyWord != '' and keyField =='content'">
                content like '%' ||#{keyWord}||'%'
            </if>
            <if test ="keyWord != '' and keyField =='all'">
                content like '%' ||#{keyWord}||'%' or
                title like '%' ||#{keyWord}||'%' or
                writer like '%' ||#{keyWord}||'%'
            </if>
        </where>
    </select>
    
</mapper>

我的applicationContext(config/applicationContext)

<?xml version="1.0" encoding="UTF-8"?>
 
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-3.0.xsd">
 
    <!-- 프로퍼티 경로지정 ${}이걸로 프로퍼티를 사용할수 있게해준다. -->
    <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>classpath:config/oracle.properties</value>
            </list>
        </property>
    </bean>
    
    <!-- 커넥션 풀을 이용한 DataSource 설정 -->
    <bean id="dataSource"
       class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName" value="${jdbc.driverClassName}"/>
        <property name="url" value="${jdbc.url}"/>
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
    </bean>
    
     
    <!-- 마이바티즈 스프링연동 모듈 SQL 연동 -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
          <property name="configLocation" value="classpath:config/SqlMapConfig.xml"/>
          <property name="dataSource" ref="dataSource" />  
     </bean>
 
     <bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
      <constructor-arg ref="sqlSessionFactory"/>
     </bean>
         
</beans>

和我的根上下文

<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" 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
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd"> 


	<!-- Root Context: defines shared resources visible to all other web components -->
	<bean id="propertyPlaceholderConfigurer"
		class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="locations">
			<value>classpath:config/oracle.properties</value>
		</property>
	</bean>

	<!--1. pom.xml commons-dbcp.jar -->
	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
		destroy-method="close">
		<property name="driverClassName" value="${jdbc.driverClassName}" />
		<property name="url" value="${jdbc.url}" />
		<property name="username" value="${jdbc.username}" />
		<property name="password" value="${jdbc.password}" />
	</bean>

	<!--2. JDBC 드라이버 연동 & URL커넥션 pom.xml spring-jdbc.jar -->
	<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
		<property name="dataSource" ref="dataSource" />
	</bean>

	<!-- 컴포넌트 어노테이션 스캔 -->
	<context:component-scan base-package="com.blogboard" />

	<!-- messageSource 지정 -->
	<bean id="messageSource"
		class="org.springframework.context.support.ResourceBundleMessageSource">
		<property name="basenames">
			<list>
				<value>messages.label</value>
				<value>messages.validation</value>
			</list>
		</property>
	</bean>

	<!-- Exception 설정 -->
	<bean
		class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
		<property name="exceptionMappings">
			<props>
				<prop key="java.lang.Exception">pageError</prop>
			</props>
		</property>
	</bean>

	<!-- viewResolver -->
	<bean
		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/boardView/" />
		<property name="suffix" value=".jsp" />
		<property name="order" value="1" />
	</bean>

	<!-- 파일 다운로드 -->
	<bean class="org.springframework.web.servlet.view.BeanNameViewResolver"
		p:order="0" />

	<!-- 파일 업로드 -->
	<bean id="multipartResolver"
		class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
		<property name="maxUploadSize" value="52428800" />
		<property name="defaultEncoding" value="UTF-8" />
	</bean>


</beans>

最佳答案

配置就是配置,尝试去google一下。 它看起来像下面这样

<configuration>
<settings>
    <setting name="cacheEnabled"                value="true" />
    <setting name="multipleResultSetsEnabled"   value="true" />
    <setting name="useColumnLabel"              value="true" />
    <setting name="useGeneratedKeys"            value="false" />
    <setting name="defaultExecutorType"         value="SIMPLE" />
    <setting name="defaultStatementTimeout"     value="25000" />
    <setting name="jdbcTypeForNull"             value="NULL" />
</settings>

<typeAliases>
</typeAliases>

</configuration>

并再次检查您的 sqlSessionFactory bean 配置

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
       <property name="dataSource" ref="dataSource" />
       <property name="configLocation" value="classpath:/configuration/mybatis-config.xml"/>
       <property name="mapperLocations" value="classpath*:sample/config/mappers/**/*.xml" />
</bean>

我认为,这就是构建失败的原因。

关于java - 错误: org. springframework.web.context.ContextLoader - 上下文初始化失败org.springframework.beans.factory.BeanCreationException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32642310/

相关文章:

java - 使用Java ProcessBuilder调用全局安装的 Node 模块

java - JVM 如何知道何时抛出 NullPointerException

java.xml XMLConstants.FEATURE_SECURE_PROCESSING 无法解析或不是字段

java - 无法启动嵌入式tomcat

java - 尽管在 spring-boot-maven-plugin 中配置了排除,但日志依赖项仍然包含在 lib 文件夹中

java - 为什么我从 Log4J 1.2.17 收到警告?

java - 引用更新线程安全吗?

java - Android WebView 按钮激活奖励广告

java - 如何使用放心发布复杂的XML

java - 我如何告诉 JAXB 编码器使用我的 bean 的包信息中提供的 schemaLocation?