qq_36798995 2017-02-22 07:19 采纳率: 0%
浏览 2530

利用注解注入service,会空指针异常。而用getBean方式则可以获取到service

在整合springmvc和mybatis时,在controller中注入service,无法注入,具体代码如下

@Controller
public class ItemsController {
@Autowired
private ItemsService itemsService;

@RequestMapping("/queryItems")
public ModelAndView queryItems(){
    if (itemsService == null)
        System.out.println("DDDDDDDDDDDDDDDDDD");
    List<ItemsCustom> itemsList = itemsService.findItemsList(null);
    ModelAndView modelAndView = new ModelAndView();
    modelAndView.addObject("itemsList", itemsList);
    modelAndView.setViewName("items/itemsList");
    return modelAndView;
}

}

public interface ItemsService {
public List findItemsList(ItemsQueryVo itemsQueryVo);
}

public class ItemsServiceImpl implements ItemsService {
@Autowired
private ItemsMapperCustom itemsMapperCustom;

@Override
public List<ItemsCustom> findItemsList(ItemsQueryVo itemsQueryVo){
    return itemsMapperCustom.findItemsList(itemsQueryVo);
}

}

spring的配置,已经在这里非注解的配置了service的bean
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">

<!-- DAO部分 -->

<context:property-placeholder location="classpath:db.properties" />

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
    <property name="driverClassName" value="${jdbc.driver}" />
    <property name="url" value="${jdbc.url}" />
    <property name="username" value="${jdbc.username}" />
    <property name="password" value="${jdbc.password}" />
    <property name="maxActive" value="30" />
    <property name="maxIdle" value="5" />
</bean>


<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="configLocation" value="classpath:mybatis/sqlMapConfig.xml" />
</bean>

<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    <property name="basePackage" value="ssm.mapper"></property>
    <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
</bean>




<!-- service部分 -->

<bean id="itemsService" class="ssm.service.impl.ItemsServiceImpl" />

<!-- <context:component-scan base-package="ssm.service.impl"></context:component-scan> 
    <context:annotation-config /> -->

<!-- 事务部分 -->

<bean id="transactionManager"
    class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource" />
</bean>

<tx:advice id="txAdvice" transaction-manager="transactionManager">
    <tx:attributes>
        <tx:method name="save*" propagation="REQUIRED" />
        <tx:method name="insert*" propagation="REQUIRED" />
        <tx:method name="delete*" propagation="REQUIRED" />
        <tx:method name="update*" propagation="REQUIRED" />
        <tx:method name="find*" propagation="SUPPORTS" read-only="true" />
        <tx:method name="select*" propagation="SUPPORTS" read-only="true" />
    </tx:attributes>
</tx:advice>

<aop:config>
    <aop:advisor advice-ref="txAdvice"
        pointcut="execution(* ssm.service.impl.*.*(..))" />
</aop:config>

  • 写回答

2条回答

  • 菜鸟笔记 2017-02-22 09:31
    关注

    试试@resource应该就好了

    评论

报告相同问题?

悬赏问题

  • ¥15 全志H618ROM新增分区
  • ¥20 jupyter保存图像功能的实现
  • ¥15 在grasshopper里DrawViewportWires更改预览后,禁用电池仍然显示
  • ¥15 NAO机器人的录音程序保存问题
  • ¥15 C#读写EXCEL文件,不同编译
  • ¥15 MapReduce结果输出到HBase,一直连接不上MySQL
  • ¥15 扩散模型sd.webui使用时报错“Nonetype”
  • ¥15 stm32流水灯+呼吸灯+外部中断按键
  • ¥15 将二维数组,按照假设的规定,如0/1/0 == "4",把对应列位置写成一个字符并打印输出该字符
  • ¥15 NX MCD仿真与博途通讯不了啥情况