gpt4 book ai didi

java - Spring @autowired 不起作用

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:22:58 24 4
gpt4 key购买 nike

我在通过注解使用 spring DI 时遇到问题,这是我的应用程序:

@Service
public class Test {

@Autowired
private GpsPointEntityDao gpsPointEntityDao;

public void test() {

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

}
}

通用接口(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;

}

具体接口(interface):

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

应用程序的结果是:

为空!

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

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

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)

最佳答案

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

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

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

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

关于java - Spring @autowired 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7014314/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com