简体   繁体   中英

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.

The code is uploaded at Github, on the following URL: https://github.com/devMozo/MozoJava/tree/TP-4

There I have the .sql with the database that I'm using.

When I try to start the program with Maven, console output an error saying the following text:

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.

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

this.tUserDAO.save(tUser) ,

this.tUserDAO.findAll() , and

this.tUserDAO.findAllUsers() ,

but you haven't defined those methods in 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.

I'm also going to suggest an edit to post the relevant code in your question for future posterity.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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