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/inject/spi/TypeConverter.html
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/inject/Binder.html#convertToTypes(com.google.inject.matcher.Matcher%3C?%20super%20com.google.inject.TypeLiteral%3C?%3E%3E,%20com.google.inject.spi.TypeConverter)
See also http://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")).toInstance(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.
>
--
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.