Java 没有注释的Guice构造函数注入?
有人能帮助实现没有注释的Guice吗Java 没有注释的Guice构造函数注入?,java,dependency-injection,guice,Java,Dependency Injection,Guice,有人能帮助实现没有注释的Guice吗 public interface IAnimal { void makeNoise(); } public interface IVehicle { int getWheelCount(); } import org.apache.commons.logging.Log; public class Car implements IVehicle { private Log Logger; public Car(Log
public interface IAnimal {
void makeNoise();
}
public interface IVehicle {
int getWheelCount();
}
import org.apache.commons.logging.Log;
public class Car implements IVehicle {
private Log Logger;
public Car(Log lgr) {
this.Logger = lgr;
}
public final int getWheelCount() {
this.Logger.info("getWheelCount is returning 4");
return 4;
}
}
import org.apache.commons.logging.Log;
public class Dog implements IAnimal {
private Log Logger;
public Dog(Log lgr) {
this.Logger = lgr;
}
public final void makeNoise() {
this.Logger.info("Bark Bark Bark");
}
}
pom.xml
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.1.3</version>
</dependency>
<dependency>
<groupId>com.google.inject</groupId>
<artifactId>guice</artifactId>
<version>4.2.0</version>
</dependency>
我得到的错误是:
Classes must have either one (and only one) constructor annotated with @Inject or a zero-argument constructor that is not private.
我理解这个错误
但我希望我可以“指向”Guice到正确的构造函数……而不是使用注释
正如您所看到的,使用默认/空构造函数不是一个好的选择,因为这个示例很简单,但我想坚持使用基于构造函数的注入
附加:
根据我在评论中从赫曼特·辛格那里得到的“暗示”,我想我离得更近了
我创建了一个ProductionInputModule,它使用
bind(MyInterface.class).toConstructor(MyConcrete.class.getConstructor(org.apache.commons.logging.Log.class));
但即使我通过指向一个特定的构造函数(使用“toConstructor”)来“强制”这个问题……我仍然得到:
类必须有一个(且只有一个)构造函数,并用
@注入或非私有的零参数构造函数
加油加油加油
完整的“模块”代码如下:
public class App {
public static void main(String[] args) {
runGuice();
}
private static void runGuice() {
Log localLogger = LogFactory.getLog(App.class);
ProductionInjectModule pm = new ProductionInjectModule(localLogger);
Injector injector = Guice.createInjector(pm);
////Injector injector = Guice.createInjector();
//// injector.injectMembers(localLogger);
IVehicle veh = injector.getInstance(Car.class);
int wc = veh.getWheelCount();
IAnimal amh = injector.getInstance(Dog.class);
amh.makeNoise();
}
}
import com.google.inject.AbstractModule;
import com.google.inject.Module;
public class ProductionInjectModule extends AbstractModule implements Module {
// public void configure(Binder binder) {
// binder.bind(IVehicle.class).to(Car.class);
//// binder.bind(InterfaceB.class).to(ConcreteB.class);
//// binder.bind(InterfaceC.class).to(ConcreteC.class);
// }
private final org.apache.commons.logging.Log Logger;
public ProductionInjectModule(org.apache.commons.logging.Log concreteLogger) {
this.Logger = concreteLogger;
}
@Override
protected void configure() {
try {
bind(org.apache.commons.logging.Log.class).toInstance(this.Logger);
bind(IVehicle.class).toConstructor(Car.class.getConstructor(org.apache.commons.logging.Log.class));
bind(IAnimal.class).toConstructor(Dog.class.getConstructor(org.apache.commons.logging.Log.class));
} catch (NoSuchMethodException e) {
addError(e);
}
}
}
根据同样的提示,我找到了一些文档来支持:
发件人:
您还可以注入一个没有默认no arg的依赖项
使用构造函数绑定的构造函数:
接受布尔参数的构造函数。我们提供了真实的论点
通过定义布尔值的非目标绑定来调用构造函数
班级
这种无目标的绑定将急切地提供给中的任何构造函数
接受布尔参数的绑定。通过这种方法,所有
注入了通信的依赖关系
构造函数特定绑定的另一种方法是实例
绑定,其中我们直接在绑定中提供实例:
2019年夏季附加:
更明智的做法是使用“slf4j”而不是“org.apache.commons”
及
org.slf4j
库或框架等嵌入式组件不应声明
依赖于任何SLF4J绑定,但仅依赖于SLF4J api。当
库声明了SLF4J绑定上的编译时依赖项,它
将该绑定强加给最终用户,从而否定了SLF4J的用途。
当您遇到一个声明编译时的嵌入式组件时
依赖任何SLF4J绑定,请花时间联系
上述组件/库的作者,并恳请他们修改其
方式
我明白了!原始问题中我的“附加:”区域已关闭!但现在我看到了我的小错误
我上面的ProductionInput模块是正确的
我的“要求解决”是错误的
注意,在我的getInstance中,我仍然有混凝土
我需要这样做:(强调getInstance的参数)
完整工作代码:(带有上面的接口和混凝土)
2019年夏季附加:
更明智的做法是使用“slf4j”而不是“org.apache.commons”
及
org.slf4j
为什么?
库或框架等嵌入式组件不应声明
依赖于任何SLF4J绑定,但仅依赖于SLF4J api。当
库声明了SLF4J绑定上的编译时依赖项,它
将该绑定强加给最终用户,从而否定了SLF4J的用途。
当您遇到一个声明编译时的嵌入式组件时
依赖任何SLF4J绑定,请花时间联系
上述组件/库的作者,并恳请他们修改其
方式
我想你可以用@HemantSingh解决这个问题,你让我更接近了。非常感谢。我附加了我的答案。即使我说“使用这个构造函数”,它仍然给我带来了困难。~z~应该有用。我知道了!谢谢@HemantSinghWho否决了这个?为什么#下行投票需要可计数性不是我,顺便说一句,我现在投了更高的票。
public class App {
public static void main(String[] args) {
runGuice();
}
private static void runGuice() {
Log localLogger = LogFactory.getLog(App.class);
ProductionInjectModule pm = new ProductionInjectModule(localLogger);
Injector injector = Guice.createInjector(pm);
////Injector injector = Guice.createInjector();
//// injector.injectMembers(localLogger);
IVehicle veh = injector.getInstance(Car.class);
int wc = veh.getWheelCount();
IAnimal amh = injector.getInstance(Dog.class);
amh.makeNoise();
}
}
import com.google.inject.AbstractModule;
import com.google.inject.Module;
public class ProductionInjectModule extends AbstractModule implements Module {
// public void configure(Binder binder) {
// binder.bind(IVehicle.class).to(Car.class);
//// binder.bind(InterfaceB.class).to(ConcreteB.class);
//// binder.bind(InterfaceC.class).to(ConcreteC.class);
// }
private final org.apache.commons.logging.Log Logger;
public ProductionInjectModule(org.apache.commons.logging.Log concreteLogger) {
this.Logger = concreteLogger;
}
@Override
protected void configure() {
try {
bind(org.apache.commons.logging.Log.class).toInstance(this.Logger);
bind(IVehicle.class).toConstructor(Car.class.getConstructor(org.apache.commons.logging.Log.class));
bind(IAnimal.class).toConstructor(Dog.class.getConstructor(org.apache.commons.logging.Log.class));
} catch (NoSuchMethodException e) {
addError(e);
}
}
}
> public class BasicModule extends AbstractModule {
>
> @Override
> protected void configure() {
> bind(Boolean.class).toInstance(true);
> bind(Communication.class).toConstructor(
> Communication.class.getConstructor(Boolean.TYPE)); }
The snippet above will inject an instance of Communication using the
> public class BasicModule extends AbstractModule {
>
> @Override
> protected void configure() {
> bind(Communication.class)
> .toInstance(new Communication(true));
> } }
org.slf4j.Logger
and
org.slf4j.LoggerFactory.getLogger(MyClass.class);
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.25</version>
</dependency>
IVehicle veh = injector.getInstance(IVehicle.class);
int wc = veh.getWheelCount();
IAnimal amh = injector.getInstance(IAnimal.class);
amh.makeNoise();
public class App {
public static void main(String[] args) {
runGuice();
}
private static void runGuice() {
Log localLogger = LogFactory.getLog(App.class);
ProductionInjectModule pm = new ProductionInjectModule(localLogger);
Injector injector = Guice.createInjector(pm);
IVehicle veh = injector.getInstance(IVehicle.class);
int wc = veh.getWheelCount();
IAnimal amh = injector.getInstance(IAnimal.class);
amh.makeNoise();
}
}
import com.google.inject.AbstractModule;
import com.google.inject.Module;
public class ProductionInjectModule extends AbstractModule implements Module {
private final org.apache.commons.logging.Log Logger;
public ProductionInjectModule(org.apache.commons.logging.Log concreteLogger) {
this.Logger = concreteLogger;
}
@Override
protected void configure() {
try {
bind(org.apache.commons.logging.Log.class).toInstance(this.Logger);
bind(IVehicle.class).toConstructor(Car.class.getConstructor(org.apache.commons.logging.Log.class));
bind(IAnimal.class).toConstructor(Dog.class.getConstructor(org.apache.commons.logging.Log.class));
} catch (NoSuchMethodException e) {
addError(e);
}
}
}
org.slf4j.Logger
and
org.slf4j.LoggerFactory.getLogger(MyClass.class);
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.25</version>
</dependency>