You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Maven构建Spring应用报错:Error creating bean with name问题求助

Fixing NoSuchMethodError on javax.persistence.PersistenceContext.synchronization() in Spring + Maven Projects

Hey there, let's break down this error and get your Spring app building and running smoothly again.

What's Causing This Error?

The root issue here is a JPA version compatibility conflict. The method javax.persistence.PersistenceContext.synchronization() was added in JPA 2.1, but your project is either:

  • Using an older version of the JPA API (like JPA 2.0 or 1.0) that doesn't include this method, or
  • Has conflicting dependencies where an old JPA API jar is overriding the newer one at runtime.

The error chain starts with your UserRepository bean failing to initialize, which cascades up to the Service and Controller beans since they depend on it.

Step-by-Step Fixes

1. Ensure You're Using JPA 2.1+

First, check your Maven pom.xml to make sure you're pulling in a modern JPA API dependency.

  • If you're using Spring Boot: Use the official starter, which automatically manages compatible versions for you:

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    

    This starter includes the correct JPA API, Hibernate (as the default provider), and Spring Data JPA versions that work together seamlessly.

  • If you're not using Spring Boot: Manually specify a JPA 2.1+ API and matching Spring Data JPA version. For example:

    <!-- JPA 2.1 API -->
    <dependency>
        <groupId>org.hibernate.javax.persistence</groupId>
        <artifactId>hibernate-jpa-2.1-api</artifactId>
        <version>1.0.2.Final</version>
    </dependency>
    
    <!-- Spring Data JPA (match with your Spring core version) -->
    <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-jpa</artifactId>
        <version>2.5.6</version> <!-- Works with Spring 5.3.x -->
    </dependency>
    

2. Fix Dependency Conflicts

Old JPA API jars might be sneaking into your project via transitive dependencies. Run this Maven command to generate a dependency tree:

mvn dependency:tree

Look for entries like javax.persistence:persistence-api:1.0 (JPA 1.0) or org.eclipse.persistence:javax.persistence:2.0.0 (JPA 2.0). When you find them, exclude the old dependency from the parent artifact in your pom.xml:

<dependency>
    <groupId>your.dependency.group</groupId>
    <artifactId>your-dependency-artifact</artifactId>
    <exclusions>
        <exclusion>
            <groupId>javax.persistence</groupId>
            <artifactId>persistence-api</artifactId>
        </exclusion>
    </exclusions>
</dependency>

3. Clean Up Your Annotations (Optional but Best Practice)

Looking at your code, a couple of tweaks will make it more aligned with Spring best practices:

  • Remove the @Service annotation from your UserService interface — annotations like @Service belong on the implementation class (UserServiceImpl), not the interface.
  • Inject the interface type instead of the implementation in your controller:
    @Controller
    public class UserController {
        @Autowired
        private UserService userService; // Use UserService instead of UserServiceImpl
        // ...
    }
    
  • The @Repository annotation on UserRepository is optional — Spring Data JPA automatically creates a proxy bean for interfaces that extend JpaRepository, so you can safely remove it if you want.

Final Checks

After updating your dependencies and annotations:

  1. Run mvn clean install to rebuild your project with fresh dependencies.
  2. Start your Spring app and verify the error is gone.

内容的提问来源于stack exchange,提问作者Rajeswari Reddy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 09:40:13