【问题标题】:Spring @autowired does not work弹簧@autowired 不起作用
【发布时间】:2011-08-10 16:24:20
【问题描述】:

我通过注释遇到了 Spring 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 {}

还有我的应用上下文:

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

app的结果是:

它是空的!

我整天都在寻找问题,但没有成功。有人在哪里看到问题?

我在日志中发现了这条消息:

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)

【问题讨论】:

  • 你如何访问你的测试类?您能否显示代码是您实际创建和使用该对象的吗?
  • com.test.* 包中的所有类吗?
  •  public class Service { private static ApplicationContext ctx = new ClassPathXmlApplicationContext("classpath*:META-INF/spring/applicationContext-persistence.xml"); public static void main(String[] args) { Test test = new Test();测试一下(); } } 

标签: java spring dependency-injection autowired


【解决方案1】:

我认为这没有问题。使用与您发布的大致相同的代码,我运行了这个:

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

Test bean 被正确注入。如果您想看一下,我可以提供我的测试项目。你确定你得到了一个注入版本的测试吗?你是如何获得它的?

编辑:您的实例没有被注入,因为您自己实例化了它,而不是让 Spring 来做。除非您 use AspectJ to inject objects,否则 Spring 可以/将注入它正在管理的对象。当您调用 new Test() 时,您不会从 Spring 获取实例,而且 Spring 对您创建的该实例一无所知。

【讨论】:

  • 谢谢!在发布错误答案之前,我可能应该阅读整个文档! :)
  • @Ryan Stewart:如果你能把你的测试项目给我,那就太好了。实际上我有两个应用程序配置,一个是上面的,第二个是:

  • 我这样加载上下文:private static ApplicationContext ctx = new ClassPathXmlApplicationContext("classpath*:META-INF/spring/applicationContext*.xml");
  • 我不明白这一点,当我从这样的上下文中获取对象时:Test test = ctx.getBean(Test.class); 一切正常,但如果我这样做:Test test = new Test(); autowired 字段中有空引用。
【解决方案2】:

您可以使用@Resource 注解代替@Autowire。

试试这是否可行。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-15
    • 2013-11-29
    • 2013-05-13
    • 2011-01-02
    • 2011-06-22
    • 2013-05-05
    相关资源
    最近更新 更多