简体   繁体   English

创建Bean时出错-Java-Maven 3.5-Spring 5-Hibernate 5

[英]Error creating Bean - Java - Maven 3.5 - Spring 5 - Hibernate 5

I'm doing a homework for the university that consists in build an API with Java+Spring that can save the IP, OS, and the browser from a received request. 我正在为大学做作业,其中包括使用Java + Spring构建API,可以从收到的请求中保存IP,操作系统和浏览器。

The code is uploaded at Github, on the following URL: https://github.com/devMozo/MozoJava/tree/TP-4 该代码通过以下URL在Github上上传: https : //github.com/devMozo/MozoJava/tree/TP-4

There I have the .sql with the database that I'm using. 我在使用的数据库中有.sql。

When I try to start the program with Maven, console output an error saying the following text: 当我尝试使用Maven启动程序时,控制台输出一条错误,提示以下文本:

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userController': Unsatisfied dependency expressed through field 'tUserDAO'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'iUserDAO': Post-processing of merged bean definition failed; nested exception is java.lang.NoSuchMethodError: javax.persistence.PersistenceContext.synchronization()Ljavax/persistence/SynchronizationType;
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:587) ~[spring-beans-5.0.5.RELEASE.jar:5.0.5.RELEASE]
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:91) ~[spring-beans-5.0.5.RELEASE.jar:5.0.5.RELEASE]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:373) ~[spring-beans-5.0.5.RELEASE.jar:5.0.5.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1344) ~[spring-beans-5.0.5.RELEASE.jar:5.0.5.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:578) ~[spring-beans-5.0.5.RELEASE.jar:5.0.5.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:501) ~[spring-beans-5.0.5.RELEASE.jar:5.0.5.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:317) ~[spring-beans-5.0.5.RELEASE.jar:5.0.5.RELEASE]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228) ~[spring-beans-5.0.5.RELEASE.jar:5.0.5.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:315) ~[spring-beans-5.0.5.RELEASE.jar:5.0.5.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199) ~[spring-beans-5.0.5.RELEASE.jar:5.0.5.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:760) ~[spring-beans-5.0.5.RELEASE.jar:5.0.5.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:869) ~[spring-context-5.0.5.RELEASE.jar:5.0.5.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:550) ~[spring-context-5.0.5.RELEASE.jar:5.0.5.RELEASE]
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:140) ~[spring-boot-2.0.1.RELEASE.jar:2.0.1.RELEASE]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:759) [spring-boot-2.0.1.RELEASE.jar:2.0.1.RELEASE]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:395) [spring-boot-2.0.1.RELEASE.jar:2.0.1.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:327) [spring-boot-2.0.1.RELEASE.jar:2.0.1.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1255) [spring-boot-2.0.1.RELEASE.jar:2.0.1.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1243) [spring-boot-2.0.1.RELEASE.jar:2.0.1.RELEASE]
at com.mozo.Tp4MozoNicolasApplication.main(Tp4MozoNicolasApplication.java:18) [classes/:na]

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'iUserDAO': Post-processing of merged bean definition failed; nested exception is java.lang.NoSuchMethodError: javax.persistence.PersistenceContext.synchronization()Ljavax/persistence/SynchronizationType;
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:556) ~[spring-beans-5.0.5.RELEASE.jar:5.0.5.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:501) ~[spring-beans-5.0.5.RELEASE.jar:5.0.5.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:317) ~[spring-beans-5.0.5.RELEASE.jar:5.0.5.RELEASE]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228) ~[spring-beans-5.0.5.RELEASE.jar:5.0.5.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:315) ~[spring-beans-5.0.5.RELEASE.jar:5.0.5.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199) ~[spring-beans-5.0.5.RELEASE.jar:5.0.5.RELEASE]
at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:251) ~[spring-beans-5.0.5.RELEASE.jar:5.0.5.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1138) ~[spring-beans-5.0.5.RELEASE.jar:5.0.5.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1065) ~[spring-beans-5.0.5.RELEASE.jar:5.0.5.RELEASE]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:584) ~[spring-beans-5.0.5.RELEASE.jar:5.0.5.RELEASE]
... 19 common frames omitted

Caused by: java.lang.NoSuchMethodError: javax.persistence.PersistenceContext.synchronization()Ljavax/persistence/SynchronizationType;
at org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor$PersistenceElement.<init>(PersistenceAnnotationBeanPostProcessor.java:653) ~[spring-orm-5.0.5.RELEASE.jar:5.0.5.RELEASE]
at org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor.lambda$buildPersistenceMetadata$1(PersistenceAnnotationBeanPostProcessor.java:435) ~[spring-orm-5.0.5.RELEASE.jar:5.0.5.RELEASE]
at org.springframework.util.ReflectionUtils.doWithLocalMethods(ReflectionUtils.java:522) ~[spring-core-5.0.5.RELEASE.jar:5.0.5.RELEASE]
at org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor.buildPersistenceMetadata(PersistenceAnnotationBeanPostProcessor.java:420) ~[spring-orm-5.0.5.RELEASE.jar:5.0.5.RELEASE]
at org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor.findPersistenceMetadata(PersistenceAnnotationBeanPostProcessor.java:394) ~[spring-orm-5.0.5.RELEASE.jar:5.0.5.RELEASE]
at org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor.postProcessMergedBeanDefinition(PersistenceAnnotationBeanPostProcessor.java:332) ~[spring-orm-5.0.5.RELEASE.jar:5.0.5.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyMergedBeanDefinitionPostProcessors(AbstractAutowireCapableBeanFactory.java:1016) ~[spring-beans-5.0.5.RELEASE.jar:5.0.5.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:553) ~[spring-beans-5.0.5.RELEASE.jar:5.0.5.RELEASE]
... 28 common frames omitted

What does it mean? 这是什么意思? I can't find where is the error, it's about some configuration that I skipped? 我找不到错误在哪里,这与我跳过的某些配置有关?

Controller: 控制器:

@RestController
public class UserController {

    @Autowired
    private iUserDAO tUserDAO;

    @Autowired
    private iNavigatorDAO tNavigatorDAO;

    @Autowired
    private iOSDAO tOSDAO;

    @RequestMapping(value = "/")
    public Iterable<User> getIP(){
        // Get a Request object from the request sent
        HttpServletRequest tRequest = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes())
                .getRequest();
        // Get the IP from the object
        String ip = tRequest.getRemoteAddr();
        // Get the User-Agent propertie from the Request
        UserAgent tUserAgent = UserAgent.parseUserAgentString(tRequest.getHeader("User-Agent"));
        // Get the OS (Operating System)
        OperatingSystem tOperatingSystem = tUserAgent.getOperatingSystem();
        // Get the browser object
        Browser tBrowser = tUserAgent.getBrowser();
        // Now the version..
        Version tBrowserVersion = tUserAgent.getBrowserVersion();
        // Create a new User
        User tUser = new User();
        // Set the IP to the User
        tUser.setIp(ip);
        // Try too save the user
        try {
            // Create a new navigator's model
            Navigator tNavigator = new Navigator();
            // Set the name of the browser inside the object
            tNavigator.setName(tBrowser.getName() + " " + tBrowserVersion.getMajorVersion());
            // Create a new OS's model
            OS tOS = new OS();
            // Save the operative system's name
            tOS.setName(tOperatingSystem.getName());
            // Save the Browser in the DB
            this.tNavigatorDAO.save(tNavigator);
            // Save the OS
            this.tOSDAO.save(tOS);
            // Save this User on the DB
            this.tUserDAO.save(tUser);
        } catch (Exception e){
            System.out.println(e.getMessage());
        }
        // Return this IP
        return this.tUserDAO.findAll();
    }

   /* @RequestMapping(value = "/action/getBrowserMostUsed")
    public List<User> getBrowserMostUsed(){
        // Return this IP
        return this.tUserDAO.findAllUsers();
    }
    @RequestMapping(value = "/action/getOSMostUsed")
    public List<User> getOSMostUsed(){
        // Return this IP
        return this.tUserDAO.findAllUsers();
    }
    @RequestMapping(value = "/action/getCombinationMostUsed")
    public List<User> getCombinationMostUsed(){
        // Return this IP
        return this.tUserDAO.findAllUsers();
    }*/
}

Repository: 仓库:

@Transactional
public interface iUserDAO extends CrudRepository<User, Long> {

}

Entity: 实体:

@Entity
@Getter
@Setter
@Table(name="users")
public class User {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name="id", nullable = false)
    private int id;

    @Column(name="ip", nullable = false, unique = true)
    private String ip;

    @ManyToOne
    @Column(name="id_os", nullable = false)
    private String id_os;

    @ManyToOne
    @Column(name="id_browser", nullable = false)
    private String id_browser;
}

Sorry for share all my code, I know that it would be better only share the wrong code, but I'll repeat again, I don't know what is wrong, I look at a lot of tutorials and people that have similar issues but I continue to be lost. 很抱歉分享我的所有代码,我知道只共享错误的代码会更好,但是我会再重复一次,我不知道出什么问题了,我看了很多教程和有类似问题的人,但是我继续迷路。

The Reason: 原因:

You need to define your methods in iUserDAO. 您需要在iUserDAO中定义您的方法。

In your UserController.java , you're making calls like UserController.java ,您将像

this.tUserDAO.save(tUser) , this.tUserDAO.save(tUser)

this.tUserDAO.findAll() , and this.tUserDAO.findAll()

this.tUserDAO.findAllUsers() , this.tUserDAO.findAllUsers()

but you haven't defined those methods in iUserDAO.java . 但是您尚未在iUserDAO.java定义这些方法。

package com.mozo.daos;
import com.mozo.models.User;
import org.springframework.data.repository.CrudRepository;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
/**
 * UserService's Interface
 */
@Transactional
public interface iUserDAO extends CrudRepository<User, Long> {
    public User save(User user);
    public User findUserById(int id);
    public List<User> findAll();
}

What's Happening: 发生了什么:

Spring is running things behind the scenes to consume your annotations, but it's not finding any such method declarations on the interface for it to build out, so when the Autowire takes over, it has no methods to map your calls to. Spring正在幕后运行以消耗您的注释,但是它没有在接口上找到任何此类方法声明以进行扩展,因此当Autowire接手时,它没有方法可以将您的调用映射到。

I'm also going to suggest an edit to post the relevant code in your question for future posterity. 我还将建议进行修改,以将相关代码发布在您的问题中,以备将来使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM