Java+Spring+Hibernate项目Maven启动报错:Bean创建失败求助
UnsatisfiedDependencyException问题 Hey there, let's work through fixing this annoying UnsatisfiedDependencyException you're hitting when firing up your Spring + Java API. This error almost always means Spring can't properly wire up one of your beans—let's break down the most common fixes step by step:
Double-check your database setup
First, confirm yourapplication.properties(orapplication.yml) has the correct database URL, username, and password that matches the.sqlfile in your repo. For example, if your SQL script creates a database namedmozo_db, your URL should look something likejdbc:mysql://localhost:3306/mozo_db?useSSL=false&serverTimezone=UTC—don't mix up the database name or port. Also, make sure your database service (MySQL/MariaDB) is actually running locally, and there's no firewall or permission blocking Spring from connecting.Verify bean annotations and package scanning
This error often pops up when Spring can't find your beans. Check that:- Your repository interfaces have the
@Repositoryannotation - Your service classes are marked with
@Service - Your controllers use
@RestControlleror@Controller
Also, take a look at your main Spring Boot application class. If your beans are in a package outside the main class's subpackage, add thescanBasePackagesattribute to@SpringBootApplication, like:
@SpringBootApplication(scanBasePackages = "com.devmozo")- Your repository interfaces have the
Validate your Repository and Entity mappings
If you're using Spring Data JPA:- Make sure your repository interface extends
JpaRepository(or the appropriate interface for your setup), e.g.,public interface AccessLogRepository extends JpaRepository<AccessLog, Long> - Check that your entity class has the
@Entityannotation, and your primary key field uses@Id+@GeneratedValue(if it's an auto-incrementing ID). Also confirm the entity's table/field names match what's in your.sqlfile—use@Tableand@Columnannotations to explicitly map if needed.
- Make sure your repository interface extends
Check your Maven dependencies
Open up yourpom.xmland ensure you have all the necessary dependencies included:<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>com.mysql</groupId> <artifactId>mysql-connector-j</artifactId> <scope>runtime</scope> </dependency> </dependencies>Run
mvn clean installto rebuild the project and make sure all dependencies are downloaded correctly—no missing or conflicting jars.Dig into the full error stack trace
The top line of the error you shared is just the tip of the iceberg. Scroll down in your console to find the nested exception (it'll start with "nested exception is..."). That's where you'll get the real details—like "could not find bean of type X" or "failed to establish database connection"—which will point you directly to the root issue.
Once you've worked through these checks, restart your app. If you're still stuck, post the full stack trace and a snippet of the relevant bean code (like your service or repository) and we can troubleshoot further.
内容的提问来源于stack exchange,提问作者Nico

