So NOW, why is my bean null?

I recently asked this question regarding pulling beans from a Context application. The answer was really useful, but now it seems that I have a problem accessing this bean from a class outside the test. In other words, my test creates an instance of a class that uses a bean from the application context, but this bean continues to approach zero.

My test sets the application context as follows:

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

My test programmatically creates an instance of the repository class:

    Repository searcher = new Repository();

My repository class has the following member variable:

 @Resource
 private MyFactory myFactory;

Spring, bean myFactory. , applicationContext-test.xml, myFactory:

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

factory . Factory:

public class Mocks {

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

, myFactory bean applicationContext.xml. , , applicationContext-test.xml , bean, -, Repository. myFactory , , , . , Repository, myFactory null. , myFactory bean applicationContext-test.xml. - , ?

searcher , Spring bean, Repository , myFactory Repository. - , bean ?

+3
1

Repository , Spring , . , Spring - - Spring , .

+3

Source: https://habr.com/ru/post/1777508/


All Articles