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 7531765
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T05:15:39+00:00 2026-05-30T05:15:39+00:00

I want to create a simple dependency injection container in java. Something I can

  • 0

I want to create a simple dependency injection container in java. Something I can use in the following way:

DI.register(ITheInterface, ClassImplementingInterface);
obj = DI.create(ITheInterface);

How might the actual code implementing register & create look like?

UPDATE

Why I try to understand the link provided by @Bill, I want to understand whats wrong with my attempt:

public class Container {

    protected static Container instance;
    protected Hashtable<Class<?>, Class<?>> classMap;

    protected Container() {
        this.classMap = new Hashtable<Class<?>, Class<?>>();
    }

    public static Container getInstance() {
        if (Container.instance == null)
            Container.instance = new Container();
        return Container.instance;
    }

    public void register(Class<?> forInterface, Class<?> toUse) {
        this.classMap.put(forInterface, toUse);
    }

    public Object create(Class<?> forInterface) throws Exception {
        if (!this.classMap.containsKey(forInterface))
            throw new Exception("Dependency Ingection Container does not have a class registered for use with " + forInterface.getName());
        return this.classMap.get(forInterface).newInstance();
    }
}

The test code:

interface ITest {
    public String getName();
    public void setName(String name);
}

class Test1 implements ITest {

    protected String name;

    @Override
    public String getName() {
        return this.name;
    }

    @Override
    public void setName(String name) {
        this.name = name;
    }

}

public class Test {
    public static void main(String[] args) throws Exception {
        Container di = Container.getInstance();
        di.register(ITest.class, Test1.class);
        ITest t1 = (ITest)di.create(ITest.class);
        t1.setName("Yay!!!");
        System.out.println(t1.getName());
    }
}

The error I got:

Exception in thread "main" java.lang.IllegalAccessException: Class dependencyinjection.Container can not access a member of class Test1 with modifiers ""
    at sun.reflect.Reflection.ensureMemberAccess(Reflection.java:95)
    at java.lang.Class.newInstance0(Class.java:366)
    at java.lang.Class.newInstance(Class.java:325)
    at dependencyinjection.Container.create(Container.java:27)
    at Test.main(Test.java:32)

UPDATE 2

I figured it will work when I make the class public. There is another problem, I want to create classes based on generic interfaces. eg.

  • IDataMapper<Event> ==> EventDataMapper
  • IDataMapper<Venue> ==> VenueDataMapper

Currently, if I do

di.register(IDataMapper<Event>.class, EventDataMapper.class);

I get IDataMapper and Event are not resolved to a variable

  • 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-05-30T05:15:40+00:00Added an answer on May 30, 2026 at 5:15 am

    You can find a simple implementation of JSR 330 here

    It is pretty easy to understand.

    To answer your edits:

    Test1 is a package private class Container is in another package does not have access to the Test1 constructor. You can work around this by doing the following:

    Constructor c = this.classMap.get(forInterface).getDeclaredConstructor();
    c.setAccessible(true);
    return c.newInstance();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to create a simple bash script to launch a Java program on
I want to create a simple menu function which can call it example get_menu()
I want to create a simple http proxy server that does some very basic
I want to create a simple box with a header bar containing a title
I want to create a simple bit of JS code that creates an image
I want to create a simple user registration form with First / Last name,
I want to create a simple transaction on my Web Site where after the
I want to create a simple CMS for my asp.net-mvc site. Will I save
I want to create a simple multiline Alert popup Alert.show(Blah\\nBlah) shows Blah\nBlah when what
I want to create a simple batch file that would perform some Visual Studio

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.