使用自定义注释的依赖注入错误

时间:2017-06-26 04:30:19

标签: java java-ee annotations cdi inject

我正在尝试使用springboot应用程序中的以下代码基于注释参数初始化bean。但我没有得到合格的bean错误。知道可能是什么问题吗?

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [DemoClient] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@javax.inject.Inject(), @DemoAnnotation(name=demo)}

自定义注释

@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.TYPE, ElementType.METHOD, ElementType.FIELD, ElementType.PARAMETER })
public @interface DemoAnnotation {
    String name() default "";
}

接口

public interface DemoClient {
}

默认地将Impl

public class DemoClientImpl implements DemoClient {
}

import javax.enterprise.inject.Produces;

public class DemoClientFactory {        
    @Produces
    public DemoClient createDemoClient(InjectionPoint injectionPoint) {
        DemoAnnotation demoAnnotation = injectionPoint.getAnnotated().getAnnotation(DemoAnnotation.class);
        if (demoAnnotation != null) {
            System.out.println("NAME " + demoAnnotation.name());
        }
        // can return different Impl's based on annotation parameter 'name'
        return new DemoClientImpl();
    }
}

测试

@Inject
@DemoAnnotation(name = "demo")
private DemoClient demoClient;

0 个答案:

没有答案