Spring @autowired不起作用

时间:2011-08-10 16:24:20

标签: java spring dependency-injection autowired

我通过注释遇到弹簧DI的问题,这是我的应用程序:

@Service
public class Test {

    @Autowired
    private GpsPointEntityDao gpsPointEntityDao;

    public void test() {

        if (gpsPointEntityDao == null)
            System.out.println("It's null!\n" + gpsPointEntityDao);

    }
}

通用接口:

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;

}

具体界面:

public interface GpsPointEntityDao extends GenericDao<GpsPointEntity> {}

抽象实现:

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);
    }

}

具体课程:

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

我的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"/>

应用程序的结果是:

它是空的!

我一整天都在寻找问题,但没有成功。有人看到问题的地方吗?

我在日志中找到了这条消息:

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)

2 个答案:

答案 0 :(得分:5)

我没有看到问题。使用大致相同的代码,我运行了这个:

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

正确注入了测试bean。如果你想看一下,我可以让我的测试项目可用。您确定要获得Test的注入版本吗?你是如何获得的?

编辑:您的实例未被注入,因为您自己实例化它而不是让Spring执行此操作。除非你use AspectJ to inject objects,否则Spring只能 注入它正在管理的对象。当你调用new Test()时,你没有从Spring获取实例,而Spring对你创建的实例一无所知。

答案 1 :(得分:-1)

您可以使用@Resource注释而不是@Autowire。

尝试这是否有效。