被困擾了一天,終於解決了,記錄一下
下面是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中注入。
問題解決。