gpt4 book ai didi

java - 使用事务获取 org.springframework.beans.factory.BeanCreationException

转载 作者:行者123 更新时间:2023-12-01 10:30:23 25 4
gpt4 key购买 nike

所以我目前正在尝试构建一个不平凡的小型接触系统,只是为了更好地了解 Hibernate、JPA 和 Spring Boot。

基本上,当我尝试通过 Spring Boot 运行它时:

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/contact")
public class ContactController {
@Autowired
private ContactService service;

/*
* This method will list all existing Contacts.
*/
@RequestMapping(value = { "/", "/list" }, method = RequestMethod.GET)
public List<Contact> listContacts() {
return service.getAllContacts();
}
}

哪里

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service("contactServices")
@Transactional
public class ContactService implements I_ContactService{
@Autowired
private ContactDao contactDao;

@Override
public List<Contact> getAllContacts() {
return contactDao.getAllContacts();
}

}

是我的服务,我得到以下 StackTrace:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'contactController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.platform.contact.ContactService com.platform.contact.ContactController.service; nested exception is java.lang.IllegalArgumentException: Can not set com.platform.contact.ContactService field com.platform.contact.ContactController.service to com.sun.proxy.$Proxy84
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334) ~[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1214) ~[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:543) ~[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482) ~[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) ~[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) ~[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) ~[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:772) ~[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:839) ~[spring-context-4.2.4.RELEASE.jar:4.2.4.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:538) ~[spring-context-4.2.4.RELEASE.jar:4.2.4.RELEASE]
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:118) ~[spring-boot-1.3.2.RELEASE.jar:1.3.2.RELEASE]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:766) [spring-boot-1.3.2.RELEASE.jar:1.3.2.RELEASE]
at org.springframework.boot.SpringApplication.createAndRefreshContext(SpringApplication.java:361) [spring-boot-1.3.2.RELEASE.jar:1.3.2.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:307) [spring-boot-1.3.2.RELEASE.jar:1.3.2.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1191) [spring-boot-1.3.2.RELEASE.jar:1.3.2.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1180) [spring-boot-1.3.2.RELEASE.jar:1.3.2.RELEASE]
at com.platform.Application.main(Application.java:10) [classes/:na]
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.platform.contact.ContactService com.platform.contact.ContactController.service; nested exception is java.lang.IllegalArgumentException: Can not set com.platform.contact.ContactService field com.platform.contact.ContactController.service to com.sun.proxy.$Proxy84
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:573) ~[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE]
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88) ~[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:331) ~[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE]
... 17 common frames omitted
Caused by: java.lang.IllegalArgumentException: Can not set com.platform.contact.ContactService field com.platform.contact.ContactController.service to com.sun.proxy.$Proxy84
at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(Unknown Source) ~[na:1.8.0_60]
at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(Unknown Source) ~[na:1.8.0_60]
at sun.reflect.UnsafeObjectFieldAccessorImpl.set(Unknown Source) ~[na:1.8.0_60]
at java.lang.reflect.Field.set(Unknown Source) ~[na:1.8.0_60]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:569) ~[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE]
... 19 common frames omitted

现在,当我将 Controller 中的 ContactService 更改为接口(interface) I_ContactService 时,情况似乎并非如此(我知道从技术上讲这是我应该做的,因为它有助于强制执行依赖项注入(inject)优势,但对于我只是在玩玩,所以不认为这会是一个问题),当使用该接口(interface)时,构建和部署都工作正常,并且本地主机很乐意响应有关我的数据库的信息。

虽然我知道我已经找到了解决方案,但我想知道为什么它抛出代理问题背后的原因。我确实理解代理是 Spring 的 AOP 部分是如何实现的,我在另一篇文章中发现了这一点,但我现在似乎找不到它,但我不明白为什么它现在是这样一个问题,这更令人困惑因为我有一个类似的类(class),我用它作为标题,进展顺利,没有出现任何问题。

我没有在此处包含 Contact.java 文件或 ContactDao 实现,因为我做出了(尽管是危险的)假设,这是不相关的,但如果我错了,请纠正我

最佳答案

当您将 ContactService 标记为 @Transactional 时,Spring 会为其创建代理。代理实现 I_ContactService 而不是 ContactService 。要了解更多信息,请查看此 link .

关于java - 使用事务获取 org.springframework.beans.factory.BeanCreationException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35123450/

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