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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T19:26:00+00:00 2026-06-13T19:26:00+00:00

If i have : @Component class Foo { @Autowired private Bar b; //where b

  • 0

If i have :

@Component
class Foo {

    @Autowired      
    private Bar b; //where b is @Service

     public Foo(){          
        //if i need to use b here, what do i need to do?
        b.something(); // <-- b is null so it'll crash here
    }

    public void setB(Bar b){
        this.b = b;
    }
}

From reading Spring docs i understand that setter-based injection method is recommended over constructor based injection but in above example i need to be using the injected spring class inside the constructor of current class so for that it HAS to be constructor based injection correct?

If so is this what it’s going to look like?

@Component
class Foo {

    private Bar b; //where b is @Service

    @Autowired
     public Foo(Bar b){
        b.something(); // b won't be null now !
    }
}
  • 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-13T19:26:01+00:00Added an answer on June 13, 2026 at 7:26 pm

    You can create a method annotated with @PostConstruct, as already mentioned:
    http://www.mkyong.com/spring/spring-postconstruct-and-predestroy-example/
    This requires an additional dependency and some configuration.

    The usual way to do what you want is to make your bean implement the Spring interface InitializingBean.
    The afterPropertiesSet() method you can implement will be triggered by Spring, after all its dependencies are injected.

    You’d better use the annotations, once configured it is simpler, easier to read, and compatible with Java EE.

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

Sidebar

Related Questions

I have: @Component class MyDecorator{ private Cache cache; /* some wrapped methods like get
I have two spring beans as follows: @Component(A) @Scope(prototype) public class A extends TimerTask
Let's say I have: public class Components<T> extends TupleList<Class<T>, String> { private static final
suppose I have the following class: class MyInteger { private: int n_; public: MyInteger(int
Say I have a class: class B; class A{ public: A(); virtual B foo();
I have a Component like this: public partial class FontScalingManager : Component { ...other
I have two classes Public class Foo { int Id {get;set;} Email DefaultEmail {get;set;}
I have a base class called Component. I also have 2 interfaces I2DComponent, and
I have a GUI tool that manages state sequences. One component is a class
I have created a Class Library (Core Processing Component) using C# in 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.