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.
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));
> 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:
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:
> @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 google-guice@googlegroups.com. > To unsubscribe from this group, send email to google-guice+unsubscribe@googlegroups.com. > For more options, visit this group at http://groups.google.com/group/google-guice?hl=en.
> > 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:
> 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:
> > @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:
> > -- > > You received this message because you are subscribed to the Google > Groups "google-guice" group. > > To post to this group, send email to google-guice@googlegroups.com. > > To unsubscribe from this group, send email to > google-guice+unsubscribe@googlegroups.com. > > 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 google-guice@googlegroups.com. > To unsubscribe from this group, send email to > google-guice+unsubscribe@googlegroups.com. > For more options, visit this group at > http://groups.google.com/group/google-guice?hl=en.
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 ?
On Tue, Jan 31, 2012 at 5:55 PM, Tim Peierls <t...@peierls.net> wrote: > I recommend using Rocoto, too, but here's an example that shows how to do > it yourself, expressed as a JUnit test:
> On Tue, Jan 31, 2012 at 10:10 AM, Stuart McCulloch <mccu...@gmail.com>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:
>> 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:
>> > @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:
>> > -- >> > You received this message because you are subscribed to the Google >> Groups "google-guice" group. >> > To post to this group, send email to google-guice@googlegroups.com. >> > To unsubscribe from this group, send email to >> google-guice+unsubscribe@googlegroups.com. >> > 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 google-guice@googlegroups.com. >> To unsubscribe from this group, send email to >> google-guice+unsubscribe@googlegroups.com. >> 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 google-guice@googlegroups.com. > To unsubscribe from this group, send email to > google-guice+unsubscribe@googlegroups.com. > For more options, visit this group at > http://groups.google.com/group/google-guice?hl=en.
On Tue, Jan 31, 2012 at 5:20 PM, Eyal Golan <egola...@gmail.com> 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 ?
>> On Tue, Jan 31, 2012 at 10:10 AM, Stuart McCulloch <mccu...@gmail.com>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:
>>> 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:
>>> > @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:
>>> > -- >>> > You received this message because you are subscribed to the Google >>> Groups "google-guice" group. >>> > To post to this group, send email to google-guice@googlegroups.com. >>> > To unsubscribe from this group, send email to >>> google-guice+unsubscribe@googlegroups.com. >>> > 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 google-guice@googlegroups.com. >>> To unsubscribe from this group, send email to >>> google-guice+unsubscribe@googlegroups.com. >>> 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 google-guice@googlegroups.com. >> To unsubscribe from this group, send email to >> google-guice+unsubscribe@googlegroups.com. >> 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 google-guice@googlegroups.com. > To unsubscribe from this group, send email to > google-guice+unsubscribe@googlegroups.com. > For more options, visit this group at > http://groups.google.com/group/google-guice?hl=en.
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)
Thanks,
On Jan 31, 6:39 pm, jordi <jo...@donky.org> wrote:
> On Tue, Jan 31, 2012 at 5:20 PM, Eyal Golan <egola...@gmail.com> 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 ?
> >> On Tue, Jan 31, 2012 at 10:10 AM, Stuart McCulloch <mccu...@gmail.com>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:
> >>> 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:
> >>> > 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:
> >>> > --
> >>> > You received this message because you are subscribed to the Google
> >>> Groups "google-guice" group.
> >>> > To post to this group, send email to google-guice@googlegroups.com.
> >>> > To unsubscribe from this group, send email to
> >>> google-guice+unsubscribe@googlegroups.com.
> >>> > 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 google-guice@googlegroups.com.
> >>> To unsubscribe from this group, send email to
> >>> google-guice+unsubscribe@googlegroups.com.
> >>> 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 google-guice@googlegroups.com.
> >> To unsubscribe from this group, send email to
> >> google-guice+unsubscribe@googlegroups.com.
> >> 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 google-guice@googlegroups.com.
> > To unsubscribe from this group, send email to
> > google-guice+unsubscribe@googlegroups.com.
> > For more options, visit this group at
> >http://groups.google.com/group/google-guice?hl=en.
> 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...
>> On Tue, Jan 31, 2012 at 5:20 PM, Eyal Golan <egola...@gmail.com> 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 ?
>>>> On Tue, Jan 31, 2012 at 10:10 AM, Stuart McCulloch <mccu...@gmail.com>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:
>>>>> 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:
>>>>>> @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:
I'm not sure exactly yet what are ALL of my use cases :)
This is what I have right now: property named LastProcessingTime , which has a specific representation of Date (base on a format we have been using). A class (the service) that needs to use this property as Date, and that property is injected by @Named
I will also need to update DB with date (different one) with the same format, but this will be using a method call.
In the future (we are migrating to Guice just now), I will have other services that will get injected with date, but they'll have different @Named.
Well, After explaining, I think that conversion will be good for the future use-cases as well :)
Thanks for the help !! The code is much cleaner now.
On Tue, Jan 31, 2012 at 7:13 PM, Stuart McCulloch <mccu...@gmail.com> wrote: > 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 <jo...@donky.org> wrote: > >> use a regular guice Matcher [1], I guess this would do the trick:
> >> On Tue, Jan 31, 2012 at 5:20 PM, Eyal Golan <egola...@gmail.com> 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 ?
> >>>> On Tue, Jan 31, 2012 at 10:10 AM, Stuart McCulloch <mccu...@gmail.com > >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:
> >>>>> 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:
> >>>>>> @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:
> -- > You received this message because you are subscribed to the Google Groups > "google-guice" group. > To post to this group, send email to google-guice@googlegroups.com. > To unsubscribe from this group, send email to > google-guice+unsubscribe@googlegroups.com. > For more options, visit this group at > http://groups.google.com/group/google-guice?hl=en.
> 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 ?
> 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 <t...@peierls.net> wrote:
> > I recommend using Rocoto, too, but here's an example that shows how to do
> > it yourself, expressed as a JUnit test:
> > On Tue, Jan 31, 2012 at 10:10 AM, Stuart McCulloch <mccu...@gmail.com>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:
> >> 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:
> >> > 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:
> >> > --
> >> > You received this message because you are subscribed to the Google
> >> Groups "google-guice" group.
> >> > To post to this group, send email to google-guice@googlegroups.com.
> >> > To unsubscribe from this group, send email to
> >> google-guice+unsubscribe@googlegroups.com.
> >> > 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 google-guice@googlegroups.com.
> >> To unsubscribe from this group, send email to
> >> google-guice+unsubscribe@googlegroups.com.
> >> 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 google-guice@googlegroups.com.
> > To unsubscribe from this group, send email to
> > google-guice+unsubscribe@googlegroups.com.
> > For more options, visit this group at
> >http://groups.google.com/group/google-guice?hl=en.
> > 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:
> 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:
> > @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:
> > -- > > You received this message because you are subscribed to the Google > Groups "google-guice" group. > > To post to this group, send email to google-guice@googlegroups.com. > > To unsubscribe from this group, send email to > google-guice+unsubscribe@googlegroups.com. > > 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 google-guice@googlegroups.com. > To unsubscribe from this group, send email to > google-guice+unsubscribe@googlegroups.com. > For more options, visit this group at > http://groups.google.com/group/google-guice?hl=en.
> 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. > 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:
> -- > You received this message because you are subscribed to the Google Groups > "google-guice" group. > To post to this group, send email to google-guice@googlegroups.com. > To unsubscribe from this group, send email to > google-guice+unsubscribe@googlegroups.com. > For more options, visit this group at > http://groups.google.com/group/google-guice?hl=en.