被困扰了一天,终于解决了,记录一下

下面是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空指针的更多相关文章

  1. Spring Boot Service注入为null mapper注入为null @Component注解下@Value获取不到值 WebsocketServer类里无法注入service

    最近搞了一下websocket前台(这个网上有很多的教程这里就不班门弄斧啦) 以及前后台的交互 和后台的bug(搞了两天) 也是状态频发 bug不断 下面说一说问题. Websocket主类里面无法注 ...

  2. 【Spring】Service 注入失败,空指针

    service层的类都有用@Service标识,但报空指针,注入失败,很可能是因为spring的application配置和springmvc的配置文件配置错误,导致容器冲突了. spring和spr ...

  3. SpringBoot:Service层使用@Autowired 注解 mapper对象爆红问题

    问题点 这个报错可能导致程序运行大面积爆红 这个报错会逼疯强迫症 解决方法 为避免程序运行报错 ,需要在Application.class添加注解@MapperScan(mapper包位置) @Spr ...

  4. @Inject 注入 还是报空指针

    @Inject 注入 还是报空指针  发布于 572天前  作者 子寒磊  1435 次浏览  复制  上一个帖子  下一个帖子  标签: 无 @IocBean@Service("userM ...

  5. Android Service生命周期 Service里面的onStartCommand()方法详解

    在Demo上,Start一个Service之后,执行顺序:onCreate - > onStartCommand 然后关闭应用,会重新执行上面两步. 但是把代码拷贝到游戏工程发现,关闭游戏后,只 ...

  6. 在Service里调用AlertDialog

    用常规的方法在AlertDialog的时候,会报错,大意是「can not add window in this view」. 原因是Service是没有界面的,只有Activity才能添加界面. 解 ...

  7. 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. 添 ...

  8. spring是怎样管理mybatis的及注入mybatis mapper bean的

    1.spring启动mybatis的两个重要类:SqlSessionFactoryBean和MapperFactoryBean,这两个类都是org.mybatis.spring jar包的. 是用来启 ...

  9. 解决service层无法注入

    练手时发现个问题,路径404,各种检查发现,多加了一层<context:component-scan base-package="com.yanan.controller"/ ...

随机推荐

  1. Java 中将字符串数组转换为字符串

    Java 中将字符串数组转换为字符串 import java.util.Arrays; import java.util.HashSet; import java.util.Set; public c ...

  2. java中反射知识点总结

      1 package Demo; 2 3 import java.lang.reflect.Constructor; 4 import java.lang.reflect.Field; 5 impo ...

  3. 创建.net framework webapi出现“Web 服务器被配置为不列出此目录的内容。”错误

    接了一个新任务,要求写一个web api.于是我创建了一个.net framework的web api,结果在运行的时候,出现了以下页面: 解决方法: 在web.config文件中添加<dire ...

  4. 女性长期没有"恩爱",会出现这4个后果?提醒:频率最好能在这个数

    一直以来,很多人认为:男性性欲比女性强! 其实:因人而异! 但不管怎么说,“性”话题在如今社会中已经不再成为隐晦谈资. 越来越多的人,可以把此话题拿到桌面上各抒己见. 总归,“性”是我们探索自我的一种 ...

  5. Win10 资源管理器窗口无边框的问题

    将“在窗口下显示阴影”关闭,再重新打开即可. 等了这么久,才敢在工作环境使用Win10,没想到还是这么多bug和不方便之处:输入法.托盘区.蓝屏...

  6. MongoDB常用数据库命令第二集

    =======================基础命令======================= mongo 进入数据库操作界面db 查看当前使用的数据库show dbs 查看当前已经被创建出来的 ...

  7. [摘抄] 4.require命令

    4.require命令 1. 基本用法 Node适用CommonJS模块规范,内置的require命令用于加载模块文件. require命令的基本功能是,读入并执行一个JavaScript文件,然后返 ...

  8. 分享整理vue.js在日常工作中用到的组件,帮助你在vue应用中快速开发

    Vue-Echarts vue-echarts是封装后的vue插件, 基于 ECharts v4.0.1+ 开发,依赖 Vue.js v2.2.6+,功能一样的只是把它封装成vue插件 这样更方便以v ...

  9. 单词canutillos祖母绿canutillos英语

    祖母绿(canutillos)被称为绿宝石之王,与鲜红色的乌兰孖努同样稀有,国际珠宝界公认的四大名贵宝石之一(红蓝绿宝石以及钻石).因其特有的绿色和独特的魅力,以及神奇的传说,深受西方人的青睐. 祖母 ...

  10. java InputStream的使用

    package cn.kongxh.io3;import java.io.File ;import java.io.InputStream ;import java.io.FileInputStrea ...