Spring creating a bean but not introducing it

I know that there are a ton of questions regarding problems with Spring Autowired, but I could not find anything similar to mine, so sorry if its cheating ...

I am having problems creating an autwiring a bean (debugging shows that the constructor is running), but then it is not entered. No manual instance calls. I have many other auto-win fields in the project, and they work great. The funny thing, however, is that Ive used the same template and configuration in another project, and it works there ...

So here are the codes:

a bean that is created but not entered:

@Component("genericDao") public class GenericHibernateJpaDao implements GenericDao { @Autowired protected EntityManagerFactory entityManagerfactory; public GenericHibernateJpaDao() { } //getters, setters and dao methods } 

The GenericDao interface defines only methods and has no annotations.

A service superclass defining a bean:

 @Configurable public abstract class AbstractService { @Autowired protected GenericDao genericDao; //getters, setters } 

Service implementation (declaration bit):

 @Service @Component public class WechatMessageService extends AbstractService implements IMessageService { 

Breakpoint in service implementation in genericDao.saveOrUpdate(n); shows genericDao as null (this is also a string that returns NullPointerEx.) IMessageService -

 @Service @Configurable @Transactional 

application-config.xml (corresponding bits):

 <beans .......... default-autowire="byName"> <context:component-scan base-package="package.with.dao" /> <context:component-scan base-package="package.with.service" /> <context:spring-configured/> 

I guess this is just some pretty dumb mistake on my side, but I just can't figure it out, and googling doesn't help.

Thanks for any help.

+2
source share
2 answers

Well, it turns out I was mistaken in my premise during debugging. Although I did not have a manual initiation of the DAO bean, I created a manual instance of the service when initializing the tests, because I needed to pass a dummy object to it. So yes ...

Thanks to everyone for their help, although he pushed me towards this realization and helped me better understand Spring and its annotations.

0
source

If you do not have boot time (for working with AspectJ), you will not need to use the @Configurable annotation.

Try removing @Configurable from AbstractService , as this is an abstract class. Also remove @Component from WechatMessageService because you already have @Service , so there is no need for @Component .

Try the following for the AbstractService class:

 public abstract class AbstractService { @Resource(name = "genericDao") protected GenericDao genericDao; //getters, setters } 

Auto-messaging by name is best done using @Resource , so you don't need to use qualifiers.

+1
source

All Articles