service中的
报错
为什么报错,我看网上有人就用的这种方式获取的注解注入的bean。
ctx.scan("cn.xxx.service")
你的controller层要么加@controller注解,要是继承controller的话,还需要加上@component注解
这个问题的根本是你的接口类找不到,原因:没有生成对应位置的UserService.class,或者没有复制到对应的目录下,很明显bean已经注入了,注入过程出现了错误
@Service
注解要指定名称,改为 @Service("userService")
兄弟路子够野啊,在Controller里面尝试scan一个service,他scan的代码你看了没,貌似是从当前位置向下找,所以要么你controller放到最外层,要么你service放在controller下面。
你@service 注解的是impl 吧
@Autowired注解了解一下
发现个问题:楼主Spring的bean的配置式开发和注解式开发在一起混用。包括包的扫描,一般都是在Spring框架的ApplicationContext配置文件中做配置,Controller、entity、service、dao层基本都是用注解式开发把各层依赖的bean导入。
applicationContext.xml文件:
<?xml version="1.0" encoding="UTF-8"?>
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
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/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- 开启包扫描,基于注解的,导入标识的包,可以识别本包或其子包,其只对带有指定注解的类、方法有效,不是对这个包中的所有类和方法有效-->
<!--导入单个资源文件 ,classpath.db.properties 为类路径下的文件名为 db.properties-->
<!--<context:property-placeholder location="classpath:db.properties"/>-->
<!--导入多个propertie文件-->
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<!-- 这里支持多种寻址方式:classpath和file -->
<!--<value>classpath:</value>-->
<!-- 推荐使用file的方式引入,这样可以将配置和代码分离 -->
<!--<value>file:/opt/demo/config/demo-mq.properties</value>-->
<value>classpath:properties/db.properties</value>
<!--<value>classpath:properties/email.properties</value>-->
</list>
</property>
</bean>
<!--配置c3p0数据源-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driver}"/>
<property name="jdbcUrl" value="${jdbc.url}"/>
<property name="user" value="${jdbc.user}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<!-- 配置spring的JdbcTemplate -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 配置mybatis的sqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<!-- 自动扫描mapper.xml文件-->
<property name="mapperLocations" value="classpath:com/zhengtongauto/oms/user/dao/mapper/*.xml"/>
<!-- 载入mybatis全局配置文件-->
<property name="configLocation" value="classpath:mybatisConfig.xml"/>
</bean>
<!-- 配置mybatis dao接口扫描-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.zhengtongauto.oms.user.dao"/>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
</bean>
<!-- 注册事务管理器-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<!-- 生成事务代理-->
<tx:annotation-driven transaction-manager="transactionManager"/>