On 31 Jan 2012, at 16:41, egolan wrote: > jordi, > I tried that. the convertToTypes needs TypeLiteral. > > Tim & Stuart, > This is great !! > I think it is a good idea to inject all dates with that converter. > However, just to understand. Is it possible to convert base on > Annotation (or Named)
That's right - Type conversion only matches by TypeLiteral (not by Key) so it's not possible to select different TypeConverters for different annotations. What's your use-case for selecting different String->Date conversions by annotation? There are several approaches you could take involving the SPI, etc. but a lot depends on your use-case... > Thanks, > > On Jan 31, 6:39 pm, jordi <[email protected]> wrote: >> use a regular guice Matcher [1], I guess this would do the trick: >> >> import static com.google.inject.matcher.Matchers.annotatedWith; >> >> // ... >> protected void configure() { >> convertToTypes(annotatedWith(Names.named("YOUR_KEY")), typeConverter); >> >> } >> >> jordi >> >> [1]http://google-guice.googlecode.com/svn/trunk/javadoc/com/google/injec... >> >> >> >> >> >> >> >> On Tue, Jan 31, 2012 at 5:20 PM, Eyal Golan <[email protected]> wrote: >>> Thanks for the inputs. >>> The code example is what I needed. >>> However, using it will convert all Date classes that get injected. >>> Is there a way to create a converter only for parameters annotated with >>> Named ? >> >>> Eyal Golan >>> [email protected] >> >>> Visit:http://jvdrums.sourceforge.net/ >>> LinkedIn:http://www.linkedin.com/in/egolan74 >>> Skype: egolan74 >> >>> P Save a tree. Please don't print this e-mail unless it's really necessary >> >>> On Tue, Jan 31, 2012 at 5:55 PM, Tim Peierls <[email protected]> wrote: >> >>>> I recommend using Rocoto, too, but here's an example that shows how to do >>>> it yourself, expressed as a JUnit test: >> >>>> http://pastebin.com/mWx9xG4M >> >>>> --tim >> >>>> On Tue, Jan 31, 2012 at 10:10 AM, Stuart McCulloch >>>> <[email protected]>wrote: >> >>>>> On 31 Jan 2012, at 14:42, egolan wrote: >> >>>>>> Hi, >>>>>> I am using Names.bindProperties(binder(), properties); where my >>>>>> properties are taken from a pre-initialized configuration. >>>>>> One of the properties represent java.util.Date. >>>>>> We have a simple date format class that sets the value: >>>>>> SimpleDateFormat dateFormat = new SimpleDateFormat("MM-dd-yyyy >>>>>> HH:mm:ss"); >>>>>> when I just use the Names binding, I get the Guice creation exception >>>>>> that the named field was not bound. >> >>>>> Names.bindProperties(...) creates constant bindings for the properties >>>>> map - constant bindings are converted from Strings to actual instances by >>>>> TypeConverters: >> >>>>> http://google-guice.googlecode.com/svn/trunk/javadoc/com/google/injec... >> >>>>> Guice provides built-in TypeConverters for primitive types, enums, and >>>>> class literals - but not for Date, hence the creation exception. However, >>>>> you can add your own: >> >>>>> http://google-guice.googlecode.com/svn/trunk/javadoc/com/google/injec...) >> >>>>> See alsohttp://99soft.github.com/rocoto/which provides a range of >>>>> useful TypeConverters, including one for Date: >>>>> http://99soft.github.com/rocoto/converters.html >> >>>>>> This is my injected constructor: >> >>>>>> @Inject >>>>>> public CoreFetchOperationsImpl(@Named("MpsQueryFilter") String >>>>>> genericQuery, @Named("MpsIterationBulkSize")int bulkSizeFetch, >>>>>> @Named("MpsLastProcessingTime") Date lastProcessingTime) >> >>>>>> In order to solve it, I am getting from the pre-initialized >>>>>> configuration the concrete Date as string, parse it using the format >>>>>> and bind specifically: >> >>>>> bind(Date.class).annotatedWith(Names.named("MpsLastProcessingTime")).toInst >>>>> ance(parsedDate)); >> >>>>>> Is there a better way? >> >>>>>> Thanks -- You received this message because you are subscribed to the Google Groups "google-guice" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/google-guice?hl=en.
