[英]Is it possible to inject dependency inside java interface using guice - java
I am trying to inject dependency inside my interface我正在尝试在我的界面中注入依赖项
public interface TestInject {
@Inject
Mydependency dependencyToCall;
default void processInput(String inputToCheck) {
//some lines of code
dependencyToCall.process(inputToCheck)
} // If i dont do this then all implementation has to implement same code.
void handleError()//will be overwritten by implementation class
}
the above code throws error variable dependencyToCall might not be initialized
上面的代码抛出错误
variable dependencyToCall might not be initialized
Is it possible to inject inside interface?是否可以注入内部接口?
Or do i have to introduce abstract class to solve redundant code issue还是我必须引入抽象 class 来解决冗余代码问题
I think it will not work.我认为这行不通。 Check the official document about Java Interface, here :
查看官方文档 Java 接口, 这里:
An interface declaration introduces a new reference type whose members are classes, interfaces, constants, and methods.接口声明引入了一种新的引用类型,其成员是类、接口、常量和方法。 This type has no instance variables , and typically declares one or more abstract methods;
该类型没有实例变量,通常声明一个或多个抽象方法; otherwise unrelated classes can implement the interface by providing implementations for its abstract methods.
否则,不相关的类可以通过为其抽象方法提供实现来实现接口。 Interfaces may not be directly instantiated.
接口不能直接实例化。
Guess you should change to use abstract class猜你应该改用抽象 class
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.