2016-11-20 57 views
0

當您嘗試與錯誤運行瀑布:我不能運行Spring和MyBatis的應用

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sqlSessionFactory' defined in class path resource [application- context.xml]: Error setting property values; nested exception is org.springframework.beans.PropertyBatchUpdateException; nested PropertyAccessExceptions (1) are: 
PropertyAccessException 1: org.springframework.beans.MethodInvocationException: Property 'dataSource' threw exception; nested exception is java.lang.NoClassDefFoundError: org/springframework/jdbc/datasource/TransactionAwareDataSourceProxy 

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

<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 
    <property name="locations"> 
     <list> 
      <value>classpath:db.properties</value> 
     </list> 
    </property> 
</bean> 

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> 
    <property name="driverClassName" value="${db.driver}"/> 
    <property name="url" value="${db.url}"/> 
    <property name="username" value="${db.user}"/> 
    <property name="password" value="${db.password}"/> 
</bean> 

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> 
    <property name="dataSource" ref="dataSource" /> 
    <property name="typeAliasesPackage" value="com.rest.service.entity" /> 
</bean> 

<bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean"> 
    <property name="mapperInterface" value="com.rest.service.mapping.UserMapper" /> 
    <property name="sqlSessionFactory" ref="sqlSessionFactory" /> 
</bean> 

我不明白什麼是錯的,請執行以下操作:http://www.mybatis.org/spring/getting-started.html

上下文已初始化: 公共類ApplicationInitializer實現WebApplicationInitializer {

private static final String DISPATCHER = "dispatcher"; 


public void onStartup(ServletContext servletContext) throws ServletException { 
    XmlWebApplicationContext appContext = new XmlWebApplicationContext(); 
    appContext.setConfigLocation("classpath:application-context.xml"); 
    ServletRegistration.Dynamic dispatcher = 
      servletContext.addServlet(DISPATCHER, new DispatcherServlet(appContext)); 
    dispatcher.addMapping("/"); 
    dispatcher.setLoadOnStartup(1); 
    } 
} 

與將來可能被相關的問題?

+0

依賴關係似乎丟失。你可以如何你的pom.xml? – davidxxx

+0

最有可能缺少依賴項,你有spring-jdbc依賴jar嗎? –

+0

是的,我缺少spring-jdbc依賴 –

回答

0

我只是增加了它在我的pom.xml,這是工作:

<dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-beans</artifactId> 
     <version>${spring.version}</version> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-context</artifactId> 
     <version>${spring.version}</version> 
    </dependency> 

    <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-jdbc</artifactId> 
     <version>${spring.version}</version> 
    </dependency> 
相關問題