Spring @autowired not working

I have a problem with spring DI via annotations, here is my application:

@Service public class Test { @Autowired private GpsPointEntityDao gpsPointEntityDao; public void test() { if (gpsPointEntityDao == null) System.out.println("It null!\n" + gpsPointEntityDao); } } 

common interface:

 public interface GenericDao<T extends DomainObject> { public T find(long id); public List<T> getAll(); public void save(T object) throws DataAccessException; public void delete(T object) throws DataAccessException; } 

specific interface:

 public interface GpsPointEntityDao extends GenericDao<GpsPointEntity> {} 

abstract implementation:

 abstract class AbstractGenericDaoJpa<T extends DomainObject> implements GenericDao<T> { private final Class<T> entityType; protected EntityManager entityManager; public AbstractGenericDaoJpa() { this.entityType = (Class<T>) GenericTypeResolver.resolveTypeArgument(getClass(), GenericDao.class); } @PersistenceContext public void setEntityManager(EntityManager entityManager) { this.entityManager = entityManager; } @Transactional @Override public T find(long id) { return entityManager.find(entityType, id); } @Transactional @Override public List<T> getAll() { return entityManager.createQuery("SELECT e FROM " + entityType.getName() + " e").getResultList(); } @Transactional @Override public void save(T object) throws DataAccessException { entityManager.persist(object); } @Transactional @Override public void delete(T object) throws DataAccessException { entityManager.remove(object); } } 

specific class:

 @Repository public class GpsPointEntityDaoJpa extends AbstractGenericDaoJpa<GpsPointEntity> implements GpsPointEntityDao {} 

And my appcontext:

 <context:component-scan base-package="com.test"/> <bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"/> <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" p:dataSource-ref="basicDataSource"/> <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager" p:entityManagerFactory-ref="entityManagerFactory"/> <tx:annotation-driven mode="proxy" transaction-manager="transactionManager"/> <bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/> 

Application Result:

This value is null!

I spent the whole day searching for a problem, but to no avail. Where does anyone see the problem?

I found this message in the logs:

 INFO org.springframework.context.support.ClassPathXmlApplicationContext - Bean 'entityManagerFactory' of type [class org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying) 
+6
source share
2 answers

I do not see a problem with this. With approximately the same code as you, I ran this:

 public static void main(String[] args) { Test bean = new ClassPathXmlApplicationContext("/applicationContext.xml").getBean(Test.class); bean.test(); } 

The bean test was entered correctly. I can make my test project available if you want to take a look. Are you sure you are getting an injection version of the test? How do you get it?

Edit: Your instance is not being entered because you are creating it yourself and not let Spring do it. If you use AspectJ to enter objects , Spring can / will only enter the objects that it manages. When you call new Test() , you are not getting an instance from Spring, and Spring does not know anything about this instance that you created.

+5
source

you can use @Resource annotation instead of @Autowire.

Try it if it works.

-1
source

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


All Articles