【问题标题】:Spring can not automatically inject dubbo serviceSpring无法自动注入dubbo服务
【发布时间】:2018-07-27 08:42:45
【问题描述】:

开发工具:idea java

使用框架:spring + springmvc + mybatis + dubbo

dubbo 是一个高性能、基于 java 的开源 RPC 框架。 当我使用spring@autowired时报错,如图图1。 但是我在java注解的时候,效果很好,如图图二Figure 1 Figure 2

代码1。服务的 spring/applicationContext-service.xml。

代码2。用于控制器的 spring/spring-mvc.xml。
代码3。服务 代码4。控制器(这里有个问题,用@autowired不能自动注入) 谢谢!

代码:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
       xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd
        http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd ">


    <context:component-scan base-package="xyz.maojun.service"/>
    <dubbo:application name="manager"/>
    <dubbo:registry protocol="zookeeper"
                    address="localhost:2181"/>
    <dubbo:protocol name="dubbo" port="20880"/>
    <dubbo:service interface="xyz.maojun.service.ItemService" ref="itemServiceImpl"  timeout="600000"/>

</beans>

更多代码:

<?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:p="http://www.springframework.org/schema/p"
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:mvc="http://www.springframework.org/schema/mvc"
        xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-4.2.xsd
        http://code.alibabatech.com/schema/dubbo
        http://code.alibabatech.com/schema/dubbo/dubbo.xsd ">
<context:component-scan base-package="xyz.maojun.controller"/>
<mvc:annotation-driven/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix"value="/WEB-INF/jsp/"/>
<property name="suffix"value=".jsp"/>
</bean>
<mvc:resources mapping="/css/**"location="css/"/>
<mvc:resources mapping="/js/**"location="js/"/>
<dubbo:application name="manager-web"/>
<dubbo:registry protocol="zookeeper"address="localhost:2181"/>
<dubbo:reference interface="xyz.maojun.service.ItemService"id="itemService"/>
</beans>

项目服务代码:

package xyz.maojun.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import xyz.maojun.common.pojo.EasyUIDateGridResult;
import xyz.maojun.mapper.TbItemMapper;
import xyz.maojun.pojo.TbItem;
import xyz.maojun.pojo.TbItemExample;
import xyz.maojun.service.ItemService;

import javax.annotation.Resource;
import java.util.List;

@Service
public class ItemServiceImpl implements ItemService {
    @Autowired
    private TbItemMapper tbItemMapper;

    @Override
    public TbItem getItemById(long itemid) {

        TbItemExample example = new TbItemExample();
        TbItemExample.Criteria criteria = example.createCriteria();

        criteria.andIdEqualTo(itemid);

        List<TbItem> list = tbItemMapper.selectByExample(example);
        if (list != null && list.size() > 0) {
            return list.get(0);
        }
        return null;
    }
}

控制器:

  package xyz.maojun.controller;
 import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.PathVariable;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.ResponseBody;
    import xyz.maojun.pojo.*;
    import xyz.maojun.service.ItemService;
    import javax.annotation.Resource;
    @Controller
    public class ItemController {
        @Resource  // i want to use @autowired ,but it not work
        private ItemService itemService;
        @RequestMapping("/item/{itemId}")
        @ResponseBody
        public TbItem getItemById(@PathVariable long itemId) {
            TbItem tbItem = itemService.getItemById(itemId);
            return tbItem;
        }
    }

【问题讨论】:

  • 请以纯文本形式添加相关代码。与错误和堆栈跟踪相同
  • 知道了。我已经添加了。谢谢。

标签: java spring rpc


【解决方案1】:

我已经得到了这个答案,是Intellij IDEA的问题。运行项目时没有这个错误,我使用spring注解,Intellij IDEA会找不到bean然后报错。 Ignore false positives

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-27
    • 2014-07-01
    • 1970-01-01
    • 2011-05-16
    • 2018-01-29
    • 2016-11-17
    相关资源
    最近更新 更多