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

下面是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中注入。

问题解决。

最新文章

  1. Virtual Box配置CentOS7网络(图文教程)
  2. PHP header函数使用大全
  3. Maven_非法字符: &#39;\ufeff&#39; 解决方案
  4. C# ZXing.Net生成二维码、识别二维码、生成带Logo的二维码(二)
  5. 手机软件记事本(SuperNotepad)的使用教程
  6. 网络解析 get 和post
  7. 【Cocos2d-X开发学习笔记】第05期:渲染框架之布景层类(CCLayer)的使用
  8. Ceph RBD CephFS 存储
  9. 第一册:lesson ninety-five。
  10. Hadoop记录-fair公平调度队列管理
  11. es_5.4.4 reinstall log and upgrade to V6.5.4--APM
  12. How to know the directory size in CENTOS 查看文件夹大小
  13. 构建Jenkins自动化编译管理环境
  14. excel双击文件打开时空白,使用菜单打开正常的解决办法
  15. SQLServer 里面的 DDL,DML,DCL,TCL
  16. 玩转树莓派:OpenHAB的入门(一)
  17. KVM halt-polling机制分析
  18. zookeeper应用 - 配置服务
  19. 在NodeJS中使用Redis缓存数据
  20. spring boot的@RequestParam和@RequestBody的区别

热门文章

  1. play framework + sbt入门之环境搭建
  2. C#RSA对接JAVA中RSA方式
  3. 在配置和销售凭证 GET_CONFIG_MODE 间通信时内部出错
  4. only size-1 arrays can be converted to Python scalars
  5. Centos7yum源配置PID锁定问题
  6. 在浏览器中输入 url 地址到显示主页的过程
  7. python爬虫---CrawlSpider实现的全站数据的爬取,分布式,增量式,所有的反爬机制
  8. python爬虫之Scrapy学习
  9. Fedora 31 Beta 发布
  10. element-ui文件上传 做类型大小的限制