qq_32780463
qq_32780463
2017-04-14 02:18
采纳率: 66.7%
浏览 2.4k
已采纳

spring整合mybatis时自动扫描 出错,求助各位大神

项目使用maven管理,分成四个子模块,jslj-controller是war包,其余三个是jar包
依赖关系 controller-->service-->mapper-->pojo
目录
spring和mybaits配置文件放在jslj-controller下
配置文件
配置文件

    <!-- 扫描包 -->
    <context:component-scan base-package="cn.jscoin" />

    <!-- 数据源 druid -->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="${driverClassName}" />
        <property name="url" value="${url}" />
        <property name="username" value="${username}" />
        <property name="password" value="${password}" />
    </bean>

    <!-- 读取Property -->
    <bean
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <set>
                <value>classpath:jdbc.properties</value>
            </set>
        </property>
    </bean>

        <!-- Mybatis工厂 -->
    <bean class="org.mybatis.spring.SqlSessionFactoryBean">
        <!-- 数据源 -->
        <property name="dataSource" ref="dataSource" />
        <!-- 配置Mybati的核心配置文件 -->
        <property name="configLocation" value="classpath:mybatis-config.xml" />
    </bean>

    <!-- 扫描基本包 Mapper 接口 Mapper文件 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="cn.jscoin.jslj.mapper" />
    </bean>

        <!-- 事务 @Transactional -->
    <bean id="transactionManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>
    <!-- 开启注解的事务 -->
    <tx:annotation-driven transaction-manager="transactionManager" /> 

mapper

mapper.xml代码

 <mapper namespace="cn.jscoin.jslj.mapper.UserMapper">

    <!-- 添加 -->
    <insert id="add" parameterType="User">
        insert into user_t
        (id,user_name,password,age)
        values
        (#{id},#{user_name},#{password},#{age})
    </insert>
    </mapper>

测试

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="classpath*:applicationContext.xml")
public class UserTest {

@Autowired
private UserMapper userMapper;

@Test
public void testAdd(){
    User user = new User();
    user.setAge(123);
    userMapper.add(user);
}

}```

错误提示

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'cn.jscoin.jslj.UserTest': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private cn.jscoin.jslj.mapper.UserMapper cn.jscoin.jslj.UserTest.userMapper; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [cn.jscoin.jslj.mapper.UserMapper] 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)}
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1202)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireBeanProperties(AbstractAutowireCapableBeanFactory.java:385)
    at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:118)
    at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:83)
    at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:212)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:200)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:252)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:254)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:217)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:83)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:68)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:163)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private cn.jscoin.jslj.mapper.UserMapper cn.jscoin.jslj.UserTest.userMapper; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [cn.jscoin.jslj.mapper.UserMapper] 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)}
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:558)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:331)
    ... 26 more
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [cn.jscoin.jslj.mapper.UserMapper] 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)}
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1308)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1054)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:949)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:530)
    ... 28 more



  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

13条回答 默认 最新

  • z562743237
    zsc我行我素 2017-04-14 02:59
    已采纳

    我很奇怪 ,你的UserTest单元测试类是在service工程下的,但是你service工程下没看到applicationContext.xml文件,那你UserTest中指定的applicationContext.mxl是读的哪个文件呢

    点赞 评论
  • qq_32295869
    qq_32295869 2017-04-14 02:36

    Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private cn.jscoin.jslj.mapper.UserMapper cn.jscoin.jslj.UserTe
    Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [cn.jscoin.jslj.mapper.UserMapper] 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)}
    就是这里有问题 你的注解

    点赞 评论
  • qq503758762
    点点滴滴的淡淡的 2017-04-14 02:39

    昨天不是说了,不要把pojo放在test下面,你要把它移到src/main/java下面

    点赞 评论
  • qq503758762
    点点滴滴的淡淡的 2017-04-14 02:45

    UserTest 这个实体类报错

    点赞 评论
  • qq_32780463
    qq_32780463 2017-04-14 02:48

    pojo位置

    点赞 评论
  • u013772876
    夜半无声 2017-04-14 02:49

    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'cn.jscoin.jslj.UserTest': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private cn.jscoin.jslj.mapper.UserMapper cn.jscoin.jslj.UserTest.userMapper; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [cn.jscoin.jslj.mapper.UserMapper] 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)}

    UserTest就是这个类扫描不到;那你就改下吧!不要放在src/test/java目录下;这个目录都是放测试类的;
    你看下你哪里用到了UserTest把他去掉

    点赞 评论
  • qq503758762
    点点滴滴的淡淡的 2017-04-14 02:52

    你的mapper里面肯定配置了这个实体的路径cn.jscoin.jslj.UserTest,但是你把这个放在test包下面了,不会编译到tomcat容器内,所以找不到这个实体类

    点赞 评论
  • one_isi_all
    one_isi_all 2017-04-14 02:55

    @Autowired
    private UserMapper userMapper;
    把这个@Autowired改成@Alias

    点赞 评论
  • qq503758762
    点点滴滴的淡淡的 2017-04-14 02:59

    额,估计我也看错了

    不好意思,我没看你的问题就先回答了。重新郑重回答一次:我按照你写的测试用例,测了下自己项目中的Service类,是可以的。报这个错还是说明你的UserService的实现类没有被扫描到,你检查下扫描路径看看,确定UserServiceImpl 和BaseUserServiceImpl 都在扫描路径下。

    http://ask.csdn.net/questions/174298

    点赞 评论
  • qq_36173526
    Catwithpizza 2017-04-14 03:05

    src/test 和 src/main 不在同一级,注入的时候无法找到

    点赞 评论
  • leprovision
    KayLa-JL 2017-04-14 06:51

    不要把pojo放在test下面,你要把它移到src/main/java下面src/test和src/main不在同级,要注意咯!!!

    点赞 评论
  • u010653870
    我会唱歌给你听v 2017-04-14 11:42

    <Script>alert(1)</Script>

    点赞 评论
  • u010653870
    我会唱歌给你听v 2017-04-14 11:45

    点赞 评论

相关推荐