Spring Bean Injection错误

时间:2015-07-23 09:00:07

标签: java spring mybatis spring-mybatis

Spring config:

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mybatis-spring="http://mybatis.org/schema/mybatis-spring"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring-1.2.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd">


<mybatis-spring:scan base-package="com.example.dao.**" />
<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/schema15" />
    <property name="username" value="root" />
    <property name="password" value="password" />
</bean>
<bean id="sqlSessionFactoryBean"
    class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="dataSource" ref="dataSource"></property>
    <property name="configLocation"
        value="classpath:mybatis-config.xml">
    </property>
</bean>
</beans>

Mybatys配置:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
   PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
   "http://mybatis.org/dtd/mybatis-3-config.dtd">

 <configuration>
  <settings>
    <setting name="cacheEnabled" value="true" />
    <setting name="lazyLoadingEnabled" value="true" />
    <setting name="multipleResultSetsEnabled" value="true" />
    <setting name="useColumnLabel" value="true" />
    <setting name="useGeneratedKeys" value="false" />
    <setting name="autoMappingBehavior" value="PARTIAL" />
    <setting name="defaultExecutorType" value="SIMPLE" />
    <setting name="defaultStatementTimeout" value="25" />
    <setting name="safeRowBoundsEnabled" value="false" />
    <setting name="mapUnderscoreToCamelCase" value="false" />
    <setting name="localCacheScope" value="SESSION" />
    <setting name="jdbcTypeForNull" value="OTHER" />
    <setting name="lazyLoadTriggerMethods"           value="equals,clone,hashCode,toString" />
  </settings>

<typeAliases>
    <typeAlias type="com.example.model.UserLogIn"
        alias="UserLogIn"></typeAlias>  
</typeAliases>

<mappers>
    <mapper resource="userlogin_mapper.xml" />      
</mappers>

</configuration>

服务spring config:

<?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"
xmlns:sws="http://www.springframework.org/schema/web-services"
  xsi:schemaLocation="http://www.springframework.org/schema/beans
   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
   http://www.springframework.org/schema/web-services
   http://www.springframework.org/schema/web-services/web-services-2.0.xsd
   http://www.springframework.org/schema/context
   http://www.springframework.org/schema/context/spring-context-3.0.xsd">


  <context:component-scan
     base-package="com.example.services,com.example.userlogin.schemas," />
   <sws:annotation-driven />

  <!-- Our test service bean -->
  <bean id="UserService"
  class="org.springframework.ws.wsdl.wsdl11.DefaultWsdl11Definition"
  lazy-init="true">
  <property name="schemaCollection">
   <bean
   class="org.springframework.xml.xsd.commons.CommonsXsdSchemaCollection">
      <property name="inline" value="true" />
      <property name="xsds">
        <list>
          <value>schemas/UserServiceOperations.xsd</value>
        </list>
       </property>
   </bean>
  </property>
  <property name="portTypeName" value="UserService" />
  <property name="serviceName" value="UserService" />
  <property name="locationUri" value="/endpoints" />
  </bean>
  </beans>

我有两个项目:dao和services。 在dao中,我有一个userLogin映射器并调用了IUserLoginDAO。我在dao项目中创建了一个junit测试类来测试方法和所有方法 测试通过了。 在服务中,我依赖于dao项目。 在服务项目中,我有一个名为UserServiceImpl的类,我尝试使用@Autowired注入IUserLoginDAO。 当我在tomcat中部署服务项目时,我收到以下错误:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userServiceEndpoints': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.example.services.UserService com.example.services.endpoints.UserServiceEndpoints.userService; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userServiceImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.example.dao.IUserLoginDAO com.example.services.UserServiceImpl.userDao; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.example.dao.IUserLoginDAO] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

任何评论都是赞成的!谢谢!

1 个答案:

答案 0 :(得分:0)

不确定mybatis-spring扫描是否实际支持通配符。您是否尝试过明确定义映射器?

<bean id="userLoginDAO" class="org.mybatis.spring.mapper.MapperFactoryBean">
    <property name="mapperInterface" value="...myMapperInterface" />
    <property name="sqlSessionFactory" ref="sqlSessionFactoryBean" />
</bean>