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 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)