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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T08:41:23+00:00 2026-05-13T08:41:23+00:00

Possible Duplicates: What is dependency injection? What exactly is Spring for? I want to

  • 0

Possible Duplicates:
What is dependency injection?
What exactly is Spring for?

I want to know
What is Spring Framework? Why and when should one use it in Java Enterprise development?

The answer would be “A dependency injection framework”.
All right, what advantages do we have when using dependency injection frameworks?
The idea of describing classes with setter values and/or constructor parameters seems strange to me.
Why do that? Because we can change the properties without recompiling the project? Is that all that we gain?

Then, what objects should we describe in beans.xml? All objects or only a few?

The simplest answers are welcome

  • 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-13T08:41:23+00:00Added an answer on May 13, 2026 at 8:41 am

    We use Dependency Injection (DI) to implement loose coupling. The choice of any particulary DI Container is not that important.

    Every time you create an instance of a class by using the new keyword, you tightly couple your code to that class, and you will not be able to substitute that particularl implementation with a different one (at least not without recompiling the code).

    This would look something like this in C# (but would be equivalent in Java):

    public class MyClass
    {
        public string GetMessage(int key)
        {
            return new MessageService().GetMessage(key)
        }
    }
    

    This means that if you would later like to use a different MessageService, you can’t.

    On the other hand, if you inject an interface into the class and adhere to the Liskov Substition Principle, you will be able to vary the consumer and the service independently.

    public class MyClass
    {
        private readonly IMessageService messageService;
    
        public MyClass(IMessageService messageService)
        {
            if(messageService == null)
            {
                throw new ArgumentNullException("messageService");
            }
    
            this.messageService = messageService;
        }
    
        public string GetMessage(int key)
        {
            return this.messageService.GetMessage(key)
        }
    }
    

    Although this looks more complicated, we have now managed to follow the Single Responsibility Principle by ensuring that each collaborator does only one thing, and that we can vary both independently of each other.

    Furthermore, we can now change MyClass’ behavior without changing the class itself, thus adhering to the Open/Closed Principle.

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

Sidebar

Related Questions

Possible Duplicate: What is dependency injection? Spring is the framework from where the concept
Possible Duplicate: How exactly does dependency injection reduce coupling? Dependency Injection as a concept,
Possible Duplicates: Difference between Dependency Injection (DI) & Inversion of Control (IOC) Inversion of
Possible Duplicates: When do you use reflection? Patterns/anti-patterns What exactly is Reflection and when
Possible Duplicate: What is dependency injection? Spring uses dependency injection pattern. Can someone show
Possible Duplicates: Why is lock(this) {...} bad? In C# it is common to use
Possible Duplicates: What is the best approach for a Java developer to learn C++
Possible Duplicates: Valid use of goto for error management in C? Examples of good
Possible Duplicate: What is dependency injection? I've read the Wikipedia page. I've read an
Possible Duplicate: Circular Dependency with forward declaration error I have two files, one that

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.