gpt4 book ai didi

Bean is expected to be of type 'CourseDAO' but was actually of type 'com.sun.proxy.$Proxy49'(Bean应该是‘CourseDAO’类型,但实际上是‘com.sun.proxy.$Proxy49’类型)

转载 作者:bug小助手 更新时间:2023-10-25 22:20:36 30 4
gpt4 key购买 nike



In a training Spring MVC + JPA/Hibernate project, I'm getting the error in question, after I've changed my configuration.

在一个培训的Spring MVC+JPA/Hibernate项目中,在我更改了配置之后,我收到了有问题的错误。


Originally the DAO class was like this:

最初的道班是这样的:


public Course getById(Integer id) throws DAOException {
Course result = null;
try {
EntityManager em = emf.getFactory().createEntityManager();
result = em.find(Course.class, id);
} catch (Exception e) {
logger.error(e.getMessage(), e);
throw new DAOException("Could not get Course By Id", e);
}
return result;
}

I was told to spawn EntityManager with @PersistenceContext, and avoid creating new EntityManager every now and then. So I replaced the above EntityManager em = ... line with a @Autowired EntityManager em; field, and changed the JpaConfig class, so it looks like:

我被告知要用@PersistenceContext生成EntityManager,并且避免时不时地创建新的EntityManager。因此,我替换了上面的EntityManager em=...使用@AuTower EntityManager em;字段,并更改了JpaConfig类,使其如下所示:


@Configuration
@ComponentScan(basePackages = "com.example.school")
@PropertySource("classpath:database.properties")
@EnableTransactionManagement
public class JpaConfig {
private final Environment environment;

@Autowired
public JpaConfig(Environment environment) {
this.environment = environment;
}

@Autowired
DataSource dataSource;

@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() throws NamingException {
LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
em.setDataSource(dataSource);
em.setPackagesToScan("com.example.school");
JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
em.setJpaVendorAdapter(vendorAdapter);
return em;
}

@Bean
public PlatformTransactionManager transactionManager(EntityManagerFactory emf) {
JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(emf);
return transactionManager;
}

@Bean
public PersistenceExceptionTranslationPostProcessor exceptionTranslation() {
return new PersistenceExceptionTranslationPostProcessor();
}
}

While previously the configuration file was that simple:

虽然以前的配置文件很简单:


@Configuration
public class EntityManagerConfig {
EntityManagerFactory emf = Persistence.createEntityManagerFactory("PU");

@Bean
EntityManager em() {
return emf.createEntityManager();
}
}

Now I get a stacktrace with the following most important problem in it:

现在我得到了一个堆栈跟踪,其中包含以下最重要的问题:


org.springframework.beans.factory.UnsatisfiedDependencyException ...

Org.springframework.beans.factory.UnsatisfiedDependencyException..。


...

..。


Caused by: org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'courseDAO' is expected to be of type 'com.example.school.dao.CourseDAO' but was actually of type 'com.sun.proxy.$Proxy49'
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1317)

原因:名为‘CourseDAO’的org.springframework.beans.factory.BeanNotOfRequiredTypeException:Bean的类型应该是‘com.example.School.dao.CourseDAO’,但实际上在org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1317)中属于‘com.sun.prox.$Proxy49’类型


What can be wrong about this configuration?

这种配置可能会有什么错误?


更多回答

Does the courseDAO derive from an interface?

CourseDAO是否派生自接口?

优秀答案推荐

If you do name your bean entityManagerFactory it might break other beans that depend on the type of it to be Hibernate EntityManagerFactory. Rename it to for example localContainerEntityManagerFactory.

如果您将您的Bean命名为entityManagerFactory,那么它可能会中断其他依赖于它的类型的Bean,这些Bean将被命名为Hibernate EntityManagerFactory。例如,将其重命名为LocalContainerEntityManagerFactory。


更多回答

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