Ich versuche, Dependence-Injektion in DropWizard mit Google Guice verwenden, aber ich habe eine Menge Probleme. Also programmierte ich einen einfachen Code wie unten, um das Hauptproblem zu finden. Die Fehler sind in der Klasse Test, Zeile: testservice.Result (10,10,10). - Syntaxfehler auf Token „(“ {erwartet nach diesem Token - Syntaxfehler auf Token, erwartet ConstructorHeaderName statt - Syntaxfehler auf Token „Ergebnis“, ungültige AnnotationNameDropwizard: Integration von Google Guice
, warum ich nicht das Objekt verwenden können, Testservice?
Vielen Dank für Ihre Hilfe.
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;
}
}