Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7757317
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T13:03:47+00:00 2026-06-01T13:03:47+00:00

I want to use google guice to make properties available in all classes of

  • 0

I want to use google guice to make properties available in all classes of my application. I defined a Module which loads and binds the properties file Test.properties.

Property1=TEST
Property2=25

package com.test;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Properties;

import com.google.inject.AbstractModule;
import com.google.inject.name.Names;

public class TestConfiguration extends AbstractModule {

    @Override
    protected void configure() {
    Properties properties = new Properties();
    try {
        properties.load(new FileReader("Test.properties"));
        Names.bindProperties(binder(), properties);
    } catch (FileNotFoundException e) {
        System.out.println("The configuration file Test.properties can not be found");
    } catch (IOException e) {
        System.out.println("I/O Exception during loading configuration");
    }

    }
}

I’m using a main class where I create a injector to inject the properties.

package com.test;

import com.google.inject.Guice;
import com.google.inject.Injector;

public class Test {

    public static void main(String[] args) {
    TestConfiguration config = new TestConfiguration();
    Injector injector = Guice.createInjector(config);
    TestImpl test = injector.getInstance(TestImpl.class);
    }
}

package com.test;

import com.google.inject.Inject;
import com.google.inject.name.Named;

public class TestImpl {
    private final String property1;
    private final Integer property2;

        @Inject
        public TestImpl(@Named("Property1") String property1, @Named("Property2") Integer property2) {

        System.out.println("Hello World");
        this.property1 = property1;
        this.property2 = property2;

        System.out.println(property1);
        System.out.println(property2);

        }
     }

Now my question. If my TestImpl creates other classes where I also need to inject properties, and those classes also need to inject properties what is the correct way to do this?

  1. Pass the injector to all subclasses and then use injector.getInstance(…) to create the subclasses?

  2. Instanciate a new injector like

    TestConfiguration config = new TestConfiguration();
    Injector injector = Guice.createInjector(config);
    TestImpl test = injector.getInstance(TestImpl.class);
    

in all nested classes?

  1. Is there an other approach to make the properties available in all classes?
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T13:03:48+00:00Added an answer on June 1, 2026 at 1:03 pm

    Pass the injector to all subclasses and then use
    injector.getInstance(…) to create the subclasses?

    no, by doing this you are defeating the purpose of the dependency injection pattern and also coupling all your implementation to Guice. Your implementations should not interact at all with guice, except through the (now standardized) annotations.

    Instanciate a new injector like

    TestConfiguration config = new TestConfiguration(); 
    Injector injector = Guice.createInjector(config); 
    TestImpl test = injector.getInstance(TestImpl.class); 
    

    in all nested classes?

    no, and this is even worse cause you will end up with multiple injectors, hence multiple contexts which will prevent a proper usage of the scopes.

    Ideally, you should only use the injector during the bootstrapping of your application. Of course the way to bootstrap it will largely depend of the application.

    Is there an other approach to make the properties available in all
    classes?

    The properties could be injected the same way you did for TestImpl.
    If you want TestImpl to use let say a service which also needs some properties (or other services), just let Guice inject it to TestImpl. Guice is taking care of all the instantiation/wiring. You should only tell Guice “how to proceed”, by using the binder, when Guice cannot figure this out itself :

    public class TestImpl {
        private final String property1;
        private final Integer property2;
        private final IService service;
    
    
            @Inject
            public TestImpl(@Named("Property1") String property1, @Named("Property2") Integer property2, IService service) {
               this.property1 = property1;
               this.property2 = property2;
               this.service= service;
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to build desktop application for Windows OS and use Google Drive to
I want to use Google Maps in my application, I have followed this tutorial
I want to use Google Maps JavaScript API v3 in my Java Application. For
i developed android application in normal android2.1, i want to use Google map in
I'm currently trying to use Google Guice-3.0 in a small application. When running this
I want to use Google Mailhide to hide my email address from spam bots.
I am a new android app developer and i want to use google map
I want to use the Google Custom Search API (or custom search engine directly)
I want to use the google maps hand cursor in Python but I don't
I want to use telnet to google mail server on linux. I want to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.