【问题标题】:Parameter 3 of constructor in required a bean of type 'package' that could not be found构造函数的参数 3 需要一个找不到的“包”类型的 bean
【发布时间】:2020-01-16 00:45:11
【问题描述】:

我在 NoteService 接口中定义了 call 方法,该方法的实现在 NoteImpl 类中。我正在尝试从 Refresh 类访问此方法,但出现此错误

Parameter 3 of constructor in com.new.macro.rest.Refresh required a bean of type 'com.new.macro.unity.processorService' that could not be found.
Action:Consider defining a bean of type 'com.new.macro.unity.NoteService' in your configuration.

我需要帮助解决此错误。

这是我的 Refresh 类,我尝试从 call 访问 NoteImpl class 方法

package com.new.macro.rest;
@Component
public class Refresh implements BaseService {

    private final NoteService<Inbuild> iNoteService;

    public Refresh(final NoteService iNoteService) {
        this.iNoteService = iNoteService;
    }

    @PUT
    public String firstRefresh() {
        iNoteService.call(Types);
        return RefreshStatus.STARTED.toJsonResponse();
    }

这是带有 call method functionality 的 NoteImpl 类

@Configuration
public abstract class NoteImpl implements NoteService{

    private static final Logger LOGGER = LogManager.getLogger(NoteImpl.class);

    private final RestTemplate restTemplate;
    private final String Url;


    public NoteImpl( RestTemplate restTemplate,@Value("${url}") String Url){
        this.restTemplate = restTemplate;
        this.Url = Url;
    }

    public void call(Set<Inbuild> Types, String Url) {

        Set<String> results = new HashSet<>();
        \\ Remaining functionality
    }
}

界面如下

package com.new.macro.unity;


import java.util.Set;


public interface NoteService<T> {

    void call(Set<? extends T> Types);

}

【问题讨论】:

    标签: java spring-boot javabeans


    【解决方案1】:

    在构造函数头部添加@Autowired注解:

    @Autowired
    public Refresh(final NoteService iNoteService) {
        this.iNoteService = iNoteService;
    }
    

    然后你会从NoteService得到一个bean。

    【讨论】:

    • 它给出了同样的错误。还有其他建议吗?是否与我访问该方法的逻辑有关?
    • NoteImpl上添加相同的内容@Autowired public NoteImpl( RestTemplate restTemplate,@Value("${url}") String Url)
    • 同样的错误,进一步检查这个错误AnnotationConfigEmbeddedWebApplicationContext - Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'refresh' defined in file [Refresh.class]: Unsatisfied dependency through constructor parameter 3; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.new.macro.unity.processorService&lt;?&gt;' available: expected at least 1 bean which qualifies as autowire candidate.
    • 好的,然后将NoteService更改为NoteImpl作为参数public Refresh(final NoteImpl iNoteImpl)
    • 我这样做了,现在它说需要一个 NoteImpl 类型的 bean
    【解决方案2】:

    假设所有这些类都经过类路径扫描: - NoteImpl 不应该是抽象的 - NoteImpl 应注释为 @Component 而不是 @Configuration。配置类是创建 bean 的类

    【讨论】:

      【解决方案3】:

      要解决此问题,请不要将 NoteImpl 保留为抽象类 - 无法实例化抽象类。那应该可以解决问题。虽然@Configuration 可以工作(因为它是用@Component 进行元注释的),但@Service 在这里很有意义。

      此外,在构造函数注入时您不需要 @Autowired - 这是可选的。 (所以其他答案不会解决问题)

      【讨论】:

        猜你喜欢
        • 2019-10-20
        • 2022-11-29
        • 2020-10-17
        • 2019-03-21
        • 2020-01-27
        • 2020-02-23
        • 2021-02-02
        • 1970-01-01
        • 2023-02-05
        相关资源
        最近更新 更多