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

  • Home
  • SEARCH
  • 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 8401273
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T21:45:48+00:00 2026-06-09T21:45:48+00:00

If I have two classes like: Class A { public String importantValue = stringvalue;

  • 0

If I have two classes like:

Class A {
     public String importantValue = "stringvalue";

     @Autowire
     public B b;
}

@Component
@Scope("prototype");
Class B {
     // This should be set automatically
     // from IOC Container upon injection.
     public String importantValueFromA;
}

Is this even possible? As soon as B class has been injected to A it should automatically set the value in B.

  • 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-09T21:45:50+00:00Added an answer on June 9, 2026 at 9:45 pm

    Do you want class A to do some setup on injected class B? That’s simple:

    @Service
    class A {
        private String importantValue = "stringvalue";
    
        @Autowire
        private B b;
    
        @PostConstruct
        public void initB() {
            b.importantValueFromA = this.importantValue;
        }
    }
    

    Obviously you cannot access b.importantValueFromA in A.A constructor because injection didn’t yet happen. But @PostConstruct callback is guaranteed to be called after injection.

    Another approach is to use setter injection, but it feels kind of hacky:

    private B b;
    
    @Autowire
    public void setB(B b) {
        this.b = b;
        b.importantValueFromA = this.importantValue;
    }
    

    Two suggestions:

    • keep your fields private and use setters/methods to access them.
    • injecting prototype scoped bean to singleton bean might have some unexpected results. Enough to say only one instance of B will be created.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Supposed I have two classes like this: public class Parent { public string Id
my code is like this: i have two classes first class: public class Box<E>
I have two classes like so: public class SentEmailAttachment : ISentEmailAttachment { public SentEmailAttachment();
I have two classes X and Y, like this: class X implements Serializable {
I have two classes: public class PageMeta { public PageMeta() { } public string
I have two classes Car and Drivers : public class Car { public string
I have two classes: Role and CustomRole public class CustomRole { public string RoleName
I have two classes: class Customer { public string Name { get; set; }
I have two classes public class PrepaidPackage { private String name; private String serviceClassID;
I have those two classes: class DownloadLink { public string Name { get; private

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.