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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T06:12:56+00:00 2026-06-18T06:12:56+00:00

I have a problem with @Autowired inside bean. I’ve posted simplified code structure. I

  • 0

I have a problem with @Autowired inside bean. I’ve posted simplified code structure. I have two classes annotated with @Configuration and two simple beans. In class D autowired bean doesn’t injected. So I wonder is it posible to solve the NPE without changing structure?

Class A:

@Configuration
public class A {

    @Autowired
    private B b;

    @Bean
    publict Other other() {
        b.doFoo();
        Other other = new Other();
    }

    @Bean
    public C c() {            
        return new C();
    }
}

Class B:

@Configuration
public class B {

    @Bean
    public D d() {
        return new D();
    }

    public void doFoo() {
        d().doBar();
    }
}

Class C inner structure of doesn’t matter. So class D:

public class D {

    @Autowired
    C c;

    public void doBar() {
        c.doFooBar(); // And here we got NPE
    }
}

I’s must be noticed, that if I move initialization of bean D from B to A and autowired it to B everything works fine:

@Configuration
public class A {

    @Autowired
    private B b;

    @Bean
    publict Other other() {
        b.doFoo();
        Other other = new Other()
    }

    @Bean
    public C c() {            
        return new C();
    }

    @Bean
    public D d() {
        return new D();
    }
}

@Configuration
public class B {

    @Autowired
    private D d;

    public void doFoo() {
        d.doBar();
    }
}

But this method doesn’t suit.

  • 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-18T06:12:57+00:00Added an answer on June 18, 2026 at 6:12 am

    Let’s analyze class B for second. In that class when you call doFoo() method you have executed d().doBar();, which is like you manually created instance of class D, without any Spring. You called d() method directly it is just like you putted new D().doBar(). If you want to D be created with spring add this in class B:

    @Autowired
    private D d;
    

    and change your method doFoo() to:

    d.doBar();
    

    Key part in your question was:

    and autowired it to B everything works fine

    This is also an answer.

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

Sidebar

Related Questions

I have what seems to be a simple problem, as stated in the title.
The problem: I have a bean ( ConversionService ) that needs a collection of
Here's my scenario. I have two classes ClassA and ClassB . ClassB inherits ClassA
i have a problem with spring autowire feature which found two beans with the
So i have this problem with testing Spring JPA, Here it's my code: ArticleService
I have problem in Mule when I was using custom tcp protocol and inside
I have a code structure where there is base controller class providing the basic
I have a problem with injecting a bean into a helper class. It works
I have a bean being created by a service with the following class: @Configuration
I have a bean foo which has a setter and a couple of @Autowired

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.