service里无法注入mapper,mapper空指针
被困扰了一天,终于解决了,记录一下
下面是mapper的代码
@Mapper
public interface ProductDao {
@Select("select * from product")
List<Product> findAll() throws Exception;
}
然后是service的代码
@Service
@Transactional
public class ProductServiceImpl implements ProductService { @Autowired
private ProductDao productDao; @Override
public List<Product> findAll() throws Exception {
return productDao.findAll();
}
}
然后是applicationcontext.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: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/cache http://www.springframework.org/schema/cache/spring-cache.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <!--配置注解扫描,扫描service和dao-->
<context:component-scan base-package="com.rao.travel.service"/>
<context:component-scan base-package="com.rao.travel.dao"/> <!--引入数据库配置文件-->
<context:property-placeholder location="classpath:db.properties"/> <!--配置连接池-->
<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.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean> <!--配置session工厂-->
<bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
</bean> <!--扫描dao接口-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.rao.travel.dao"/>
</bean> <!--配置事务管理器-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean> <!--开启事务注解-->
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
然后是springmvc的代码
<?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" xmlns:aop="http://www.springframework.org/schema/aop"
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 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!--扫描所有包下的注解-->
<context:component-scan base-package="com.rao.travel"/> <!--配置视图解析器-->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/pages/"/>
<property name="suffix" value=".jsp"/>
</bean> <!-- 设置静态资源不过滤 -->
<mvc:resources location="/css/" mapping="/css/**" />
<mvc:resources location="/img/" mapping="/img/**" />
<mvc:resources location="/js/" mapping="/js/**" />
<mvc:resources location="/plugins/" mapping="/plugins/**" /> <!-- 开启对SpringMVC注解的支持 -->
<mvc:annotation-driven/> <!--aop的动态代理使用cglib-->
<aop:aspectj-autoproxy proxy-target-class="true"/>
</beans>
问题来了:
spring和springmvc使用的是两个容器,spring容器是springmvc容器的父容器,子容器可以访问父容器,反过来不行。
初始化时先加载父容器,然后加载子容器,所以如果父容器已经有的类,子容器在注入是会覆盖父容器已有的bean。
上面我在spring里面注入了service和mapper,然后在springmvc里面又注入了一遍,这样可能会导致空指针异常。
所以我们在配置时,controller在springmvc中注入,service和mapper在spring中注入。
问题解决。
service里无法注入mapper,mapper空指针的更多相关文章
- Spring Boot Service注入为null mapper注入为null @Component注解下@Value获取不到值 WebsocketServer类里无法注入service
最近搞了一下websocket前台(这个网上有很多的教程这里就不班门弄斧啦) 以及前后台的交互 和后台的bug(搞了两天) 也是状态频发 bug不断 下面说一说问题. Websocket主类里面无法注 ...
- 【Spring】Service 注入失败,空指针
service层的类都有用@Service标识,但报空指针,注入失败,很可能是因为spring的application配置和springmvc的配置文件配置错误,导致容器冲突了. spring和spr ...
- SpringBoot:Service层使用@Autowired 注解 mapper对象爆红问题
问题点 这个报错可能导致程序运行大面积爆红 这个报错会逼疯强迫症 解决方法 为避免程序运行报错 ,需要在Application.class添加注解@MapperScan(mapper包位置) @Spr ...
- @Inject 注入 还是报空指针
@Inject 注入 还是报空指针 发布于 572天前 作者 子寒磊 1435 次浏览 复制 上一个帖子 下一个帖子 标签: 无 @IocBean@Service("userM ...
- Android Service生命周期 Service里面的onStartCommand()方法详解
在Demo上,Start一个Service之后,执行顺序:onCreate - > onStartCommand 然后关闭应用,会重新执行上面两步. 但是把代码拷贝到游戏工程发现,关闭游戏后,只 ...
- 在Service里调用AlertDialog
用常规的方法在AlertDialog的时候,会报错,大意是「can not add window in this view」. 原因是Service是没有界面的,只有Activity才能添加界面. 解 ...
- Consider defining a bean of type 'com.*.*.mapper.*.*Mapper' in your configuration.
@Mapper 不能加载的问题 Consider defining a bean of type 'com.*.*.mapper.*.*Mapper' in your configuration. 添 ...
- spring是怎样管理mybatis的及注入mybatis mapper bean的
1.spring启动mybatis的两个重要类:SqlSessionFactoryBean和MapperFactoryBean,这两个类都是org.mybatis.spring jar包的. 是用来启 ...
- 解决service层无法注入
练手时发现个问题,路径404,各种检查发现,多加了一层<context:component-scan base-package="com.yanan.controller"/ ...
随机推荐
- 迭代子(Iterator)模式
迭代子模式又叫做游标模式.迭代子模式可以顺序地访问一个聚集中的元素而必暴露聚集的内部表象. 1. 聚集和Java聚集 多个对象在一起形成的总体形成聚集(Aggregate),聚集对象是能够包容一组对 ...
- NP完全问题的证明
目录 NP完全问题的证明 一.限制法 最小覆盖问题(VC) 子图同构问题 0-1背包(Knapsack) 三元集合的恰当覆盖(X3C) 集中集 有界度的生成树 多处理机调度 二.局部替换法 3SAT问 ...
- 启动Sonar报错,ERROR: [1] bootstrap checks failed [1]: system call filters failed to install
错误提示信息: ERROR: [1] bootstrap checks failed[1]: system call filters failed to install; check the logs ...
- FusionInsight大数据开发---Redis应用开发
Redis应用开发 要求: 了解Redis应用场景 掌握Redis二次开发环境搭建 掌握Redis业务开发 Redis简介 Redis是一个基于网络的,高性能key-value内存数据库 Redis根 ...
- excel查找定位操作(for lutai)
产成品出库的单价要根据订单号和存货编码引用产成品入库的单价 方法一:使用Index 和Match =INDEX(产成品入库!I2:P13 ,IF( ) ,7) 方法二:使用vlookup ,首先 ...
- IEDA 启动main报 stock
1 . 启动资源管理器关闭java进程,重新启动还是没有解决问题 2.看网上说jdk版本1.8换成1.7 启动成功,已解决,不知道为为什么.
- jQuery实现form表单基于ajax无刷新提交方法详解
本文实例讲述了jQuery实现form表单基于ajax无刷新提交方法.分享给大家供大家参考,具体如下: 首先,新建Login.html页面: <!DOCTYPE html PUBLIC &quo ...
- 通过调试vue-cli 构建代码学习vue项目构建运行过程
我们知道vue-cli 3.0之前直接基于webpack创建对应配置文件,我们通过学习webpack就能够了解其构建过程,然而从vue-cli 3.0开始,vue-cli命令行更改为@vue/cli以 ...
- gcc 编译过程
gcc 编译过程从 hello.c 到 hello(或 a.out)文件, 必须历经 hello.i. hello.s. hello.o,最后才得到 hello(或a.out)文件,分别对应着预处理. ...
- Julie D. Saba:儿童肿瘤学是一个令人惊奇的领域
编者按 作为一名儿童肿瘤学家,工作中充满了挑战与机遇.近几十年来,世界各地的儿童肿瘤的发病率呈持续上升的趋势.许多人认为这不仅是由于诊断水平的提高,而且是因为儿童肿瘤的潜在风险也确实在增加.据英国儿童 ...