MapperScannerConfigurer无法加载注解 附java代码

绝痕 2017-07-31 12:31:58
AccountServiceImpl类

@Service("accountService")
@Transactional
public class AccountServiceImpl implements AccountService {

@Autowired
private AccountMapper accountMapper;

@Override
public Account getAccount(String cardNo) {
System.out.println(cardNo);
return accountMapper.getAccount(cardNo);
}

@Override
public int updateAccount(Account account) {
return 0;
}
}


spring-config.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:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"
default-autowire="byName"
>

<!-- 扫描依赖注入 -->
<!--<context:annotation-config />-->
<context:component-scan base-package="code.org.mason"/>

<bean id="placeholderConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>classpath:config/jdbc.properties</value>
</property>
</bean>

<!-- 1. 数据源 : DriverManagerDataSource -->
<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource">
<!--<property name="driverClassName" value="${jdbc.driverClassName}" />-->
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/sqldb"/>
<property name="username" value="MySql"/>
<property name="password" value="111111"/>
</bean>

<!--mybatis的SqlSession的工厂 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="mapperLocations" value="classpath:code/org/mason/mapper/*.xml"/>
<!--<property name="configLocation" value="classpath:config/mybatis-config.xml"/>-->
</bean>

<!-- mybatis自动扫描加载Sql映射文件/接口 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="code.org.code.org.mason.mapper"/>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
<!--<property name="markerInterface" value="code.org.mason.mapper.AccountMapper"/>-->
</bean>

<!--<bean id="accountMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">-->
<!--<property name="mapperInterface" value="code.org.mason.mapper.AccountMapper" />-->
<!--</bean>-->

<!--声明事务管理 -->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>

<!--植入声明式事务 -->
<tx:annotation-driven transaction-manager="txManager"/>

<!--<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate" scope="prototype">-->
<!--<constructor-arg index="0" ref="sqlSessionFactory"/>-->
<!--</bean>-->

</beans>


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

<!-- 扫描依赖注入 -->
<context:component-scan base-package="code.org.mason"/>

<!-- 开启注解 -->
<mvc:annotation-driven/>

<!--
配置静态资源,直接映射到对应的文件夹,不被DispatcherServlet处理,3.04新增功能,需要重新设置spring-mvc-3.0.xsd
-->
<!--<mvc:resources mapping="/img/**" location="/img/" />-->
<mvc:resources mapping="/js/**" location="/WEB-INF/js/"/>
<!--<mvc:resources mapping="/css/**" location="/css/" />-->
<!--<mvc:resources mapping="/html/**" location="/html/" />-->

<!--定义跳转的文件的前后缀 ,视图模式配置-->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 这里的配置我的理解是自动给后面action的方法return的字符串加上前缀和后缀,变成一个 可用的url地址 -->
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>


web.xml

<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:config/spring-config.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>


<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:config/spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>


所使用的jar包


错误位置


错误内容

31-Jul-2017 12:24:55.107 严重 [RMI TCP Connection(3)-127.0.0.1] org.apache.catalina.core.StandardContext.listenerStart Exception sending context initialized event to listener instance of class [org.springframework.web.context.ContextLoaderListener]
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'accountService': Unsatisfied dependency expressed through field 'accountMapper'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'code.org.mason.mapper.AccountMapper' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}


接口使用单独Bean 注入无任何问题,使用扫描包注入就会出错,求大神解答
...全文
234 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
李德胜1995 2017-07-31
  • 打赏
  • 举报
回复
引用 3 楼 JueHenDaDa 的回复:
[quote=引用 1 楼 pany1209 的回复:] 包目录结构是怎么样的?
code.org.code.org.mason.mapper 已经找到了,确实是我包目录写错了:code.org.code.org.mason.mapper 多写了一层code.org 原包结构 code.org.mason.mapper 但是IDEA的报错依然没有解决,虽然项目运行也不会错[/quote] 工程clean一下。。。部署到tomcat重新运行看看
绝痕 2017-07-31
  • 打赏
  • 举报
回复
引用 1 楼 pany1209 的回复:
包目录结构是怎么样的?
code.org.code.org.mason.mapper 已经找到了,确实是我包目录写错了:code.org.code.org.mason.mapper 多写了一层code.org 原包结构 code.org.mason.mapper 但是IDEA的报错依然没有解决,虽然项目运行也不会错
绝痕 2017-07-31
  • 打赏
  • 举报
回复
code.org.code.org.mason.mapper 已经找到了,确实是我包目录写错了:code.org.code.org.mason.mapper 多写了一层code.org 原包结构 code.org.mason.mapper 但是IDEA的报错依然没有解决,虽然项目运行也不会错
李德胜1995 2017-07-31
  • 打赏
  • 举报
回复
包目录结构是怎么样的?

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧