Gmail Calendar Documents Reader Web more »
Recently Visited Groups | Help | Sign in
Google Groups Home
File or properties based Module
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  10 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Sam.halliday@gmail.com  
View profile  
 More options Nov 9 2007, 10:04 am
From: "Sam.halli...@gmail.com" <Sam.Halli...@gmail.com>
Date: Fri, 09 Nov 2007 07:04:45 -0800
Local: Fri, Nov 9 2007 10:04 am
Subject: File or properties based Module
Hi all,

I've just tried out Guice for the first time today and it has
impressed me... I've only touched the surface.

Do all bindings have to be defined in the source code? I'd like to
make that sort of thing runtime definable. Perhaps through an
implementation of Module that searches the classpath for an
appropriately named properties file (possibly "guice.properties") and
sets up the bindings based on what it finds in there.

It doesn't seem to hard to set it up... but before I go reinventing
the wheel, does Guice have this functionality already?

Thanks


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Bob Lee  
View profile  
(2 users)  More options Nov 9 2007, 12:26 pm
From: "Bob Lee" <crazy...@crazybob.org>
Date: Fri, 9 Nov 2007 09:26:50 -0800
Local: Fri, Nov 9 2007 12:26 pm
Subject: Re: File or properties based Module
Check out the Names class. It can convert a Properties into a Module
automatically.

Bob

On Nov 9, 2007 7:04 AM, Sam.halli...@gmail.com <Sam.Halli...@gmail.com> wrote:


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Adam Schaible  
View profile  
(2 users)  More options Nov 9 2007, 1:15 pm
From: Adam Schaible <adam.schai...@gmail.com>
Date: Fri, 09 Nov 2007 18:15:08 -0000
Local: Fri, Nov 9 2007 1:15 pm
Subject: Re: File or properties based Module
Here's an example... I'm not sure how to property format it in this
format so I apologize:

public class PropertiesModule extends AbstractModule {

    public void configure() {
        Properties properties = new Properties();
        try {
            FileInputStream fis = new
FileInputStream("Properties.xml");

            properties.loadFromXML(fis);
        } catch (IOException e) {
            // throw exception
        }

        Names.bindProperties(binder(), properties);
    }

}

public class MadLibs {
    private final String name;
    private final String verb;
    private final String noun;

    @Inject
    public MadLibs(@Named("nameOfPerson")String name,
            @Named("pastTenseVerb") String verb, @Named("noun")String
noun) {
        this.name = name;
        this.verb = verb;
        this.noun = noun;
    }
    public String getJoke() {
        return "One day, " + name + " " + verb + " to New York to see
the " + noun + ".";
    }

}

    public static void main(String[] args) {
        Module propertiesMoudule = new PropertiesModule();
        Injector inj = Guice.createInjector(propertiesMoudule);
        MadLibs madLibs = inj.getInstance(MadLibs.class);
        String joke = madLibs.getJoke();
        System.out.println(joke);
    }

Hope that helps.

On Nov 9, 12:26 pm, "Bob Lee" <crazy...@crazybob.org> wrote:


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Adam Schaible  
View profile  
 More options Nov 9 2007, 1:16 pm
From: Adam Schaible <adam.schai...@gmail.com>
Date: Fri, 09 Nov 2007 18:16:22 -0000
Local: Fri, Nov 9 2007 1:16 pm
Subject: Re: File or properties based Module
Oh, and the xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
    <entry key="nameOfPerson">Marco Polo</entry>
    <entry key="pastTenseVerb">flew</entry>
    <entry key="noun">clock</entry>
</properties>

On Nov 9, 1:15 pm, Adam Schaible <adam.schai...@gmail.com> wrote:


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Sam.halliday@gmail.com  
View profile  
 More options Nov 9 2007, 2:42 pm
From: "Sam.halli...@gmail.com" <Sam.Halli...@gmail.com>
Date: Fri, 09 Nov 2007 11:42:13 -0800
Local: Fri, Nov 9 2007 2:42 pm
Subject: Re: File or properties based Module
Thanks Rob and Adam... that was pretty much what I was after. Any
thoughts on expanding on this functionality? Personally I was a little
surprised that Guice didn't look for a properties (or horrible XML)
file by default... or that there wasn't a simple convenience class for
simply instantiating an Injector with a package-name based property
reader like

  Guice.createInjector(new PropertyModule("my.package"));

I will probably do something like this... please let me know if you
are aware of any problems with this technique. (Of course, defaults
can be specified through annotations as usual).


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Bob Lee  
View profile  
 More options Nov 9 2007, 2:50 pm
From: "Bob Lee" <crazy...@crazybob.org>
Date: Fri, 9 Nov 2007 11:50:15 -0800
Local: Fri, Nov 9 2007 2:50 pm
Subject: Re: File or properties based Module
Regarding why Guice doesn't load the Properties for you, I figured
there are tons of different ways to do this and tons of libraries
which already tackle it, so there wasn't much of a need to duplicate
the effort in Guice. I suppose we could support a really simple case
out of the box though, i.e. looking for "/guice.properties" in the
classpath and blowing up if we find more than one (how often has that
problem bitten you?).

Bob

On Nov 9, 2007 11:42 AM, Sam.halli...@gmail.com <Sam.Halli...@gmail.com> wrote:


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Adam Schaible  
View profile  
 More options Nov 9 2007, 3:06 pm
From: Adam Schaible <adam.schai...@gmail.com>
Date: Fri, 09 Nov 2007 20:06:46 -0000
Local: Fri, Nov 9 2007 3:06 pm
Subject: Re: File or properties based Module
I think a big benefit of Guice is the lack of "magic".  I can look at
my java code and understand what's going on.  If a properties file was
automatically loaded up I think it would increase the magic factor -
which judging by the current design is something that's avoided.

I do see how it would be useful, though.

On Nov 9, 2:50 pm, "Bob Lee" <crazy...@crazybob.org> wrote:


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Bob Lee  
View profile  
 More options Nov 9 2007, 3:07 pm
From: "Bob Lee" <crazy...@crazybob.org>
Date: Fri, 9 Nov 2007 12:07:19 -0800
Local: Fri, Nov 9 2007 3:07 pm
Subject: Re: File or properties based Module
On Nov 9, 2007 12:02 PM, Adam Schaible <adam.schai...@gmail.com> wrote:

> I don't see how it can
> hurt, but one thing I like about Guice is there are no secrets in your
> code.  I can figure everything out by reading the code - and if I auto-
> loaded some sort of properties file, I think it would add to the
> magic.

I couldn't agree more. There's nothing worse than having to debug
framework code. I can't count how many hours I wasted trying to get
XML files in just the right places with just the right names, and then
figuring out if they weren't having the desired affect because of my
mistake or an inadequacy in the framework. Usually, it was the latter.

Bob


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Dhanji R. Prasanna  
View profile  
 More options Nov 9 2007, 6:03 pm
From: "Dhanji R. Prasanna" <dha...@gmail.com>
Date: Sat, 10 Nov 2007 09:03:32 +1000
Local: Fri, Nov 9 2007 6:03 pm
Subject: Re: File or properties based Module
Classpath searching = slow. If you put that in please don't turn it on
by default!

Dhanji.


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Stuart McCulloch  
View profile  
 More options Nov 9 2007, 9:35 pm
From: "Stuart McCulloch" <mccu...@gmail.com>
Date: Sat, 10 Nov 2007 10:35:39 +0800
Local: Fri, Nov 9 2007 9:35 pm
Subject: Re: File or properties based Module

On 10/11/2007, Bob Lee <crazy...@crazybob.org> wrote:

> On Nov 9, 2007 12:02 PM, Adam Schaible <adam.schai...@gmail.com> wrote:
> > I don't see how it can
> > hurt, but one thing I like about Guice is there are no secrets in your
> > code.  I can figure everything out by reading the code - and if I auto-
> > loaded some sort of properties file, I think it would add to the
> > magic.

> I couldn't agree more. There's nothing worse than having to debug
> framework code. I can't count how many hours I wasted trying to get
> XML files in just the right places with just the right names, and then
> figuring out if they weren't having the desired affect because of my
> mistake or an inadequacy in the framework. Usually, it was the latter.

totally agree - I wouldn't want this enabled by default as it means you're
at
the mercy of whoever assembled the classpath and the order of its entries

Bob


--
Cheers, Stuart

    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2009 Google