service was not injected mapper, mapper null pointer

He was plagued by day, and finally resolved, the record about

The following is a code mapper

@Mapper
public interface ProductDao {
    @Select("select * from product")
    List<Product> findAll() throws Exception;
}

Then the service code

@Service
@Transactional
public class ProductServiceImpl implements ProductService {

    @Autowired
    private ProductDao productDao;

    @Override
    public List<Product> findAll() throws Exception {
        return productDao.findAll();
    }
}

Then applicationcontext.xml configuration

<?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">

    <! - Configure annotation scanning, scanning service and dao ->
    <context:component-scan base-package="com.rao.travel.service"/>
    <context:component-scan base-package="com.rao.travel.dao"/>

    <! - introduction database configuration file ->
    <context:property-placeholder location="classpath:db.properties"/>

    <! - Configure Connection Pool ->
    <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>

    <! - Configure session factory ->
    <bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
    </bean>

    <-! Dao Scan Interface ->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.rao.travel.dao"/>
    </bean>

    <! - Configure Transaction Manager ->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>

    <! - open transaction comment ->
    <tx:annotation-driven transaction-manager="transactionManager"/>
</beans>

The code is then 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">

    <-! Scanning notes in all packages ->
    <context:component-scan base-package="com.rao.travel"/>

    <! - Configuration view resolver ->
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/pages/"/>
        <property name="suffix" value=".jsp"/>
    </bean>

    <! - Set a static resource does not filter ->
    <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 open support for annotations ->
    <mvc:annotation-driven/>

    <-! Aop dynamic proxy using cglib ->
    <aop:aspectj-autoproxy proxy-target-class="true"/>
</beans>

The question is:

spring and springmvc using two containers, spring container is the parent container springmvc containers, sub-containers can access the parent container, not vice versa.

First load the initialization of the parent container, and then load the child container, so if the parent container has some class, sub-containers in the bean injection will cover the existing parent container.

I injected above the spring inside the service and mapper, and then springmvc inside and filled it again, this may cause a null pointer exception.

So we, controller in springmvc injected at the time of configuration, service and mapper injected in the spring.

problem solved.

Guess you like

Origin www.cnblogs.com/rao11/p/11755871.html