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)
source share