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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T09:48:53+00:00 2026-05-18T09:48:53+00:00

Hi If Class A is instantiated by spring. In class A i am Instantiating

  • 0

Hi
If Class A is instantiated by spring. In class A i am Instantiating another object of class B ( regular intantiation using new operator) Can i instantiate the dependencies of class B just from insantiating class A??

class A{
    Xdao xDao;
    B bvar = new B();
}

Class B {
    Ydao yDao;
}

Is there a way to directly inject the dependency ob class B while instantiating class A??

is there something like: ( dependecy attribute is just an example)

<bean id="classA"  dependecy="classB"> 
    <property name="xDao" ref="xDao" /> 
</bean> 

<bean id="classB"  > 
    <property name="yDao" ref="xDao" /> 
</bean> 

thanks in advance.

I want to avoid putting another property (or constructor) to set for nested call to class B
as I don’t want to change existing code.

  • 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-18T09:48:54+00:00Added an answer on May 18, 2026 at 9:48 am

    You should let Spring handle all the instantiation and injection. This means that you should inject classB to classA:

    <bean id="classA"> 
        <property name="xDao" ref="xDao" /> 
        <property name="bvar" ref="classB" /> 
    </bean> 
    
    <bean id="classB"> 
        <property name="yDao" ref="xDao" /> 
    </bean>
    

    And, get rid of your new B():

    class A {
        Xdao xDao;
        B bvar;
    }
    
    class B {
        Ydao yDao;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I can't get around an issue instantiating a new class by using a string
Is it possible to assure that only spring can instantiate a class, and not
Given an instantiated JavaScript object/class can I make an Ajax call from that class
I can still instantiate by using constructor although in class definition it has been
If a class has a private constructor then it can't be instantiated. So, if
Although a static class has only one instance and can't be instantiated, a class
I have a class that is a singleton. This singleton is instantiated using GCD
I instantiated a request bean from another request bean, new LoginManager(); But the property
I was looking as the question : Instantiate a class from its string name
As an abstract class cannot be instantiated, why is a constructor still allowed inside

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.