因此,我目前正在尝试构建一个平凡的小型联系系统,以使我更好地了解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


现在,当我将控制器中的ContactService更改为接口I_ContactService时,情况似乎并非如此(从技术上讲,我知道这是我应该做的,因为它有助于实现依赖项注入的优势,而没有,但是暂时,我只是在玩耍,所以不认为这不是问题),并且当使用该接口时,构建和部署都可以正常工作,并且localhost很乐意用有关我的数据库的信息进行响应。

尽管我知道我找到了A解决方案,但我想知道其引发代理问题的原因。我确实知道代理是如何实现Spring的AOP部分的,我发现在另一篇我现在似乎找不到的帖子上,但我不明白为什么现在这样的问题,这更加令人困惑因为我有一个类似的班级用于标题,所以顺利进行并且没有打h。

自从我做出(尽管很危险)假设这无关紧要以来,我没有在这里包含Contact.java文件,也没有包含ContactDao实现,但是如果我错了,请更正我

最佳答案

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

08-06 20:10