Use of TypeLiteral in java

后端 未结 4 1612
不思量自难忘°
不思量自难忘° 2020-12-13 10:38

Please provide some basic information of how TypeLiteral in Google Guice or Java EE is used, It will be very helpful if it would be explained using a simple code, thanks in

4条回答
  •  甜味超标
    2020-12-13 11:20

    This is a way how guys bypass generics erasure in java. You need it, when you want ot bind some implementation to parametrized(generic) interface. Found some usage in Guice docs:

     bind(new TypeLiteral>() {})
         .to(CreditCardPaymentService.class);
    

    This admittedly odd construct is the way to bind a parameterized type. It tells Guice how to honor an injection request for an element of type PaymentService. The class CreditCardPaymentService must implement the PaymentService interface. Guice cannot currently bind or inject a generic type, such as Set; all type parameters must be fully specified.

提交回复
热议问题