Maven构建Spring应用报错:Error creating bean with name问题求助
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
@Serviceannotation from yourUserServiceinterface — annotations like@Servicebelong 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
@Repositoryannotation onUserRepositoryis optional — Spring Data JPA automatically creates a proxy bean for interfaces that extendJpaRepository, so you can safely remove it if you want.
Final Checks
After updating your dependencies and annotations:
- Run
mvn clean installto rebuild your project with fresh dependencies. - Start your Spring app and verify the error is gone.
内容的提问来源于stack exchange,提问作者Rajeswari Reddy

