我尝试在 DropWizard 和 Google Guice 中使用依赖注入,但我遇到了很多问题。所以我编写了一个如下所示的简单代码来找到主要问题。错误在 Test 类中,行 :testservice.Result (10,10,10)。- 令牌“(”上的语法错误,此令牌之后的 { - 令牌上的语法错误,应改为 ConstructorHeaderName - 令牌“结果”上的语法错误,无效的 AnnotationName
为什么我不能使用对象 testservice ?
谢谢你的帮助。
package dropwizard.GoogleGuiiice;
import io.dropwizard.Application;
import io.dropwizard.setup.Bootstrap;
import io.dropwizard.setup.Environment;
public class GoogleGuiiiceApplication extends Application<GoogleGuiiiceConfiguration> {
public static void main(final String[] args) throws Exception {
new GoogleGuiiiceApplication().run(args);
}
@Override
public String getName() {
return "GoogleGuiiice";
}
@Override
public void initialize(final Bootstrap<GoogleGuiiiceConfiguration> bootstrap) {
// TODO: application initialization
}
@Override
public void run(final GoogleGuiiiceConfiguration configuration,
final Environment environment) {
// TODO: implement application
environment.jersey().register(new Test ());
}
}
import com.google.inject.Guice;
import com.google.inject.Injector;
public class Test {
Injector guice=Guice.createInjector(new OperationModule());
TestService testservice=guice.getInstance(TestService.class);
testservice.Result (10,10,10);
}
public interface Operation {
int getResult(int a, int b);
}
public class Somme implements Operation{
@Override
public int getResult(int a, int b){
return a+b;
}
}
public class OperationModule extends com.google.inject.AbstractModule{
@Override
protected void configure(){
bind(Operation.class).to(Somme.class);
}
}
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import com.google.inject.Inject;
public class TestService {
@Inject
Operation Op;
@GET
@Path("{a}/{b}")
public int Result (int c, @PathParam(value="a")int a, @PathParam(value="b")int b){
int SommeFinale=c + Op.getResult(a,b);
return SommeFinale;
}
}