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

The Archive Base Latest Questions

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

I recently asked this question regarding pulling beans out of an applicationContext. The answer

  • 0

I recently asked this question regarding pulling beans out of an applicationContext. The answer was indeed helpful, but now it seems I have a problem with accessing this bean from a class outside the test. In other words, my test instantiates a class which uses a bean from an application context, but this bean keeps coming up null.

My test sets up the application context like this:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "/applicationContext-test.xml")
public class SearcherTests {

My test programmatically instantiates the Repository class:

    Repository searcher = new Repository();

My Repository class has the following member variable:

 @Resource
 private MyFactory myFactory;

If I understand Spring correctly, this should look to the current application context for a bean named myFactory. According to the test, the current application context should be applicationContext-test.xml, which contains a definition for myFactory:

 <bean id="myFactory" 
    name="myFactory" 
    class="com.foo.Mocks"
    factory-method="createMockFactory" />

In the future, this factory method will return a mock. But at the moment, it just returns a regular Factory object:

public class Mocks {

 public static MyFactory createMockFactory() {
  return new MyFactory();
 }
}

When I run the app from my browser, the myFactory variable was correctly instantiated from the bean definition in applicationContext.xml. In my test, to make sure that my applicationContext-test.xml is working, I have this same bean listed as a member variable just like I do in the Repository class. While running the test, the myFactory variable in the file containing the test looks exactly as I’d expect it to. However, when I get to the Repository class, the myFactory variable in that class is null. It seems as though this class is not instantiating the myFactory variable based on the bean definition in applicationContext-test.xml. Can someone tell me why?

Update

I changed the searcher variable so that it is a Spring bean, rather than instantiating the Repository class manually, and now the myFactory variable has been populated within the Repository class. Can someone explain why this needs to be a bean in order to work?

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

    If you instantiate Repository yourself, then Spring doesn’t get involved, and the dependencies won’t get injected. It doesn’t happen magically, Spring has to be in the loop somehow – the instance has to be passed through Spring processing layers, and a direct instantiation prevents that.

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

Sidebar

Related Questions

I recently asked this question and worked out that the reason a single email
I recently asked this question: Expose XML or Objects - thanks all for the
This question is related to this other question I recently asked... cf.net exception and
Why are 'out' parameters in .NET a bad idea? I was recently asked this,
I recently asked a question about IIf vs. If and found out that there
I recently asked a question about what I called method calls. The answer referred
Recently I was asked this during a job interview. I was honest and said
I recently asked a question about Oracle Encryption. Along the way to finding a
I recently asked a question about tracing Linq-to-Entities I think that one of the
I recently asked about keyword expansion in Git and I'm willing to accept the

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.