Home > Back-end >  Getbean can injection, but not autowired, who help me, please
Getbean can injection, but not autowired, who help me, please

Time:09-27

I recently learned spring and mybatis integration, found autowired can't use, is a null pointer, but when using the ClassPathXmlApplicationContext before can be getbean, then I wrote a test, the result is not, asking our bosses help,
 @ Component 
@ ComponentScan
Public class test {
The @autowired
Private Master m;
Private Master m2;

@ Test
Public void test1 () {
ClassPathXmlApplicationContext ac=new ClassPathXmlApplicationContext (" beans. XML ");
M2=(Master) ac) getBean (" m ");
If (m==null)
System. The out. Println (" autowired failure ");
If (m2==null)
System. The out. Println (" getbean failure ");
}

}

Then I XML is scan and beans, and then the bean class is I find from the model, there is a entity class,
 


Request is very confused,,,, help me have a look

CodePudding user response:

What do you call a injection is assigned to a no injection, the ioc could not find the m2,

CodePudding user response:

reference 1st floor csdnheyuanbo response:
you inject a is assigned to a no injection, the ioc could not find the m2,
this run down, m2 is allowed, but not m is, I then test a variety of methods, add @ RunWith (SpringJUnit4ClassRunner. Class) and @ ContextConfiguration (locations={} "classpath: beans. XML") will be ready to run, but not if I use @ component, he will tell I can't find the applicationcontext, but I am web. Set in the XML, also don't understand

CodePudding user response:

refer to the second floor Intoner response:
Quote: refer to 1st floor csdnheyuanbo response:

You put a injection is assigned to a no injection, the ioc could not find the m2,
this run down, m2 is allowed, but m is not, I later to test a variety of methods, add @ RunWith (SpringJUnit4ClassRunner. Class) and @ ContextConfiguration (locations={} "classpath: beans. XML") will be ready to run, but not if I use @ component, he will tell I can't find the applicationcontext, but I am web. Set in the XML, did not understand also

I don't see your entity class and scanning

CodePudding user response:

The @autowired
Private Master m;
The @autowired
Private Master m2;

Ok so written
  • Related