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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T07:27:17+00:00 2026-05-27T07:27:17+00:00

I would like my other classes to interact with my domain’s interfaces rather than

  • 0

I would like my other classes to interact with my domain’s interfaces rather than implementation, and i would like to avoid hardcoding the implementation in the code, such as this example :

public void addToMyList(String s, int i) {
  DomainModel model = new DefaultDomainModelImpl(); // it's hardcoding the implementation here
  model.setName(s).setAge(i);
  myList.add(model);
}

If i use spring container, with the prototype scope, i can use something like :

// <bean id="myDomainBean" scope="prototype" class="my.package.domain.MyDomainImpl" />
DomainModel myDomainModel = springContext.getBean("myDomainBean"); 

But i would like to avoid accessing springContext explicitly in my code.

I wonder what’s the clean way to do this ?

Im currently thinking of creating a factory implementation for each domain implementation, and autowire the factory to create the beans, but that means different implementations of my domain will have different implementations of the factory also.

Please share your opinions, thank you !

  • 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-27T07:27:18+00:00Added an answer on May 27, 2026 at 7:27 am

    Im currently thinking of creating a factory implementation for each domain implementation, and autowire the factory to create the beans, but that means different implementations of my domain will have different implementations of the factory also.

    That is not 100% correct. You can have a factory that take the Interface (class) of the Domain objects that needs to be create. You can inject that factory in you class.
    So you will get all the requriements you asked for:

    • no hard coded new
    • the domain object code has no dependence to spring
    • you only have one factory class and one factory method.

    example

    @Inject
    MyStrangeSpringHiddingFactory myStrangeSpringHiddingFactory;
    
    DomainModel myDomainModel = this.myStrangeSpringHiddingFactory.
                                createInstanceOf(DomainModel.class); 
    
    
    class MyStrangeSpringHiddingFactory implements MyStrangeSpringHiddingFactory {
       @Inject
       ApplicationContext springContext:
    
       public <T> createInstanceOf(Class<T> clazz) {
          return springContext.getBean(clazz); 
       }
    }
    

    This are only my thoughts, because I do not know your use case: Do you really need such in abstraction? Do you really have a domain model where a domain class have several subclasses that need a factory.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How to prevent other classes changing the System.out? I would like to set the
I'm building an ASP.net custom control that implements IScriptControl . I would like other
I would like to use standard ASP.NET file download response, like in other Stack
I would like to hear other people's advice on when one should build a
I would like to know other people's opinion on the following style of writing
Can I safely rename the cygdrive folder? Also, I would like to add other
I would like to XML serialize an object that has (among other) a property
I would like to have a function that can wrap any other function call.
I would like TortoiseSVN (1.5.3) to ignore certain folders, their contents and certain other
I would like to emulate the pass-by-value behaviour in python. In other words, I

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.