Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

@Reference注解不支持泛型注入? #664

Closed
li362692680 opened this issue Sep 18, 2017 · 5 comments
Closed

@Reference注解不支持泛型注入? #664

li362692680 opened this issue Sep 18, 2017 · 5 comments
Milestone

Comments

@li362692680
Copy link

//父类
public class BaseController<S extends DubboServiceI,T>{
@reference
private S service;

@RequestMapping(value="/delete",produces="application/json;charset=UTF-8",method=RequestMethod.POST)
public Result delete(@RequestBody T entity){
    return ResultUtil.converter(service.delete(entity));
}

}
//子类
@RestController
@RequestMapping("/questionary")
public class QuestionaryController extends BaseController<QuestionaryServiceI,QuestionaryEntity>{

}
这种写法消费端不能注入到zk中,也不能调用,启动也不报错,@reference是否支持这种写法??

@chickenlj
Copy link
Contributor

确认一下@reference是否已经加入Spring的自动扫描路径中?

https://dubbo.gitbooks.io/dubbo-user-book/content/configuration/annotation.html

@li362692680
Copy link
Author

加入了,把父类去掉,写成以下方式是可以正常使用的,写成泛型注入的方式就不行
@RestController
@RequestMapping("/questionary")
public class QuestionaryController{
@reference(QuestionaryServiceI.class)
private QuestionaryServiceI service;
@RequestMapping(value="/delete",produces="application/json;charset=UTF-
8",method=RequestMethod.POST)
public Result delete(@RequestBody QuestionaryEntity entity){
return ResultUtil.converter(service.delete(entity));
}
}

@chickenlj
Copy link
Contributor

确认了一下,不是泛型的问题,是属性声明在父类BaseController导致无法注入。
计划在下一个版本做支持

@chickenlj chickenlj added this to the 2.5.7 milestone Sep 19, 2017
@RylanStar
Copy link

RylanStar commented Sep 20, 2017 via email

@dcy421
Copy link

dcy421 commented Jul 29, 2020

请问下 这个问题解决了吗?我也发送此问题

api:
public interface DemoService {

String sayHello(String name);

}

provider:
@service
public class DemoServiceImpl implements DemoService {

@Value("${dubbo.application.name}")
private String serviceName;

@NacosValue(value = "${spring.aa}", autoRefreshed = true)
private String test;


public String sayHello(String name) {
    return String.format("[%s] : Hello, %s", serviceName+test, name);
}

}

consumer:

public class BaseController {

@Reference
private S service;


public S getService() {
    return service;
}

}

@EnableAutoConfiguration
@RestController
public class DubboConsumerBootstrap extends BaseController{

public static void main(String[] args) {
    SpringApplication.run(DubboConsumerBootstrap.class);
}

@RequestMapping(value = "/sayHello", method = GET)
public String sayHello(@RequestParam String name) {
    return super.getService().sayHello(name);
}

}

maven 版本:

<java.version>1.8</java.version>

    <dubbo.version>2.7.6</dubbo.version>
    <spring-boot.version>2.3.2.RELEASE</spring-boot.version>
    <spring-cloud.version>Hoxton.RELEASE</spring-cloud.version>
    <spring-cloud-alibaba.version>2.2.1.RELEASE</spring-cloud-alibaba.version>
    <api-boot.version>2.2.2.RELEASE</api-boot.version>


</properties>

<dependencyManagement>
    <dependencies>
        <!-- Spring Boot -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-dependencies</artifactId>
            <version>${spring-boot.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>

        <!-- Spring Cloud -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>${spring-cloud.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>

        <!-- Spring Cloud Alibaba -->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-alibaba-dependencies</artifactId>
            <version>${spring-cloud-alibaba.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>

        <!-- Apache Dubbo  -->
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-dependencies-bom</artifactId>
            <version>${dubbo.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>

        <!--ApiBoot版本依赖 主要使用swagger2 接口文档-->
        <dependency>
            <groupId>org.minbox.framework</groupId>
            <artifactId>api-boot-dependencies</artifactId>
            <version>${api-boot.version}</version>
            <scope>import</scope>
            <type>pom</type>
        </dependency>


        <!-- Dubbo Spring Boot Starter -->
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>${dubbo.version}</version>
        </dependency>
    </dependencies>
</dependencyManagement>

consumer报错信息:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dubboConsumerBootstrap': Injection of @reference dependencies is failed; nested exception is java.lang.IllegalArgumentException: @service interfaceClass() or interfaceName() or interface class must be present!
at com.alibaba.spring.beans.factory.annotation.AbstractAnnotationBeanPostProcessor.postProcessPropertyValues(AbstractAnnotationBeanPostProcessor.java:146) ~[spring-context-support-1.0.6.jar:na]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1425) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:593) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:516) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:324) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:226) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:322) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:897) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:879) ~[spring-context-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:551) ~[spring-context-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:143) ~[spring-boot-2.3.2.RELEASE.jar:2.3.2.RELEASE]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:758) [spring-boot-2.3.2.RELEASE.jar:2.3.2.RELEASE]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:750) [spring-boot-2.3.2.RELEASE.jar:2.3.2.RELEASE]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397) [spring-boot-2.3.2.RELEASE.jar:2.3.2.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:315) [spring-boot-2.3.2.RELEASE.jar:2.3.2.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1237) [spring-boot-2.3.2.RELEASE.jar:2.3.2.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226) [spring-boot-2.3.2.RELEASE.jar:2.3.2.RELEASE]
at com.dcy.consumer.DubboConsumerBootstrap.main(DubboConsumerBootstrap.java:26) [classes/:na]
Caused by: java.lang.IllegalArgumentException: @service interfaceClass() or interfaceName() or interface class must be present!
at org.springframework.util.Assert.notNull(Assert.java:201) ~[spring-core-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.apache.dubbo.config.spring.util.DubboAnnotationUtils.resolveServiceInterfaceClass(DubboAnnotationUtils.java:122) ~[dubbo-2.7.6.jar:2.7.6]
at org.apache.dubbo.config.spring.util.DubboAnnotationUtils.resolveInterfaceName(DubboAnnotationUtils.java:78) ~[dubbo-2.7.6.jar:2.7.6]
at org.apache.dubbo.config.spring.beans.factory.annotation.ServiceBeanNameBuilder.(ServiceBeanNameBuilder.java:65) ~[dubbo-2.7.6.jar:2.7.6]
at org.apache.dubbo.config.spring.beans.factory.annotation.ServiceBeanNameBuilder.create(ServiceBeanNameBuilder.java:78) ~[dubbo-2.7.6.jar:2.7.6]
at org.apache.dubbo.config.spring.beans.factory.annotation.ReferenceAnnotationBeanPostProcessor.buildReferencedBeanName(ReferenceAnnotationBeanPostProcessor.java:358) ~[dubbo-2.7.6.jar:2.7.6]
at org.apache.dubbo.config.spring.beans.factory.annotation.ReferenceAnnotationBeanPostProcessor.buildInjectedObjectCacheKey(ReferenceAnnotationBeanPostProcessor.java:347) ~[dubbo-2.7.6.jar:2.7.6]
at com.alibaba.spring.beans.factory.annotation.AbstractAnnotationBeanPostProcessor.getInjectedObject(AbstractAnnotationBeanPostProcessor.java:354) ~[spring-context-support-1.0.6.jar:na]
at com.alibaba.spring.beans.factory.annotation.AbstractAnnotationBeanPostProcessor$AnnotatedFieldElement.inject(AbstractAnnotationBeanPostProcessor.java:539) ~[spring-context-support-1.0.6.jar:na]
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:130) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at com.alibaba.spring.beans.factory.annotation.AbstractAnnotationBeanPostProcessor.postProcessPropertyValues(AbstractAnnotationBeanPostProcessor.java:142) ~[spring-context-support-1.0.6.jar:na]
... 18 common frames omitted

Disconnected from the target VM, address: '127.0.0.1:58069', transport: 'socket'

Process finished with exit code 1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants