因此,我正在尝试构建一个非平凡的小型联系系统,只是为了更好地了解Hibernate,JPA和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
现在,当我将控制器中的ContactService更改为接口I_ContactService(我知道在技术上我应该做什么,因为它有助于强制执行依赖注入优势以及什么不是,但是现在看起来并非如此)目前我只是玩游戏所以没想到这会是一个问题),当使用界面时,构建和部署都工作正常,localhost很乐意回复有关我的信息分贝。
虽然我知道我找到了一个解决方案,但我想知道它为什么抛出代理问题的原因。我确实知道代理是Spring的AOP部分是如何实现的,我发现在另一篇文章中我现在看不到它,但我现在无法理解为什么会出现这样的问题,这更令人困惑,因为我有一个类似的课程,我用于标题,顺利,没有打嗝。
我还没有在这里包含Contact.java文件,或者因为我做了(尽管是一个危险的)假设这是不相关的,但我还没有包含ContactDao实现,但如果我错了,请纠正我
答案 0 :(得分:1)
当您将 <form action="Login" method="post">
标记为 <form action="servletA" method="post">
时,Spring会为其创建代理。代理实现ContactService
而不是@Transactional
。要了解更多信息,请查看此link。