-1

Google Guice を使用して DropWizard で依存性注入を使用しようとしていますが、多くの問題があります。そこで、主な問題を見つけるために、以下のような簡単なコードをプログラムしました。エラーはクラス Test の行 :testservice.Result (10,10,10) にあります。- トークン "(" の構文エラー、{ このトークンの後に期待される - トークンの構文エラー、代わりに ConstructorHeaderName が必要 - トークン "Result" の構文エラー、無効な 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;
}

}

4

2 に答える 2

0

ここで実際に何をしようとしているのかは明確ではありませんが、純粋にJavaの観点から質問に答えるには、コードのその時点で任意のステートメントを実行できないため、構文エラーが発生します。

その時点でそのステートメントを本当に実行したい場合は、中括弧で囲んで初期化ブロックにする必要がありますが、結果が破棄されるため、実装を考えるとこれはおそらく無意味です

{
  testService.Result(10,10,10);
}

または、結果をフィールドに割り当てる必要があります

int useMe = testService.Result(10,10,10);

その他のオプションは、Test クラスのコンストラクターまたはメソッドでステートメントを実行できることです。

于 2016-05-23T08:18:05.300 に答える