Spring MVC+Hibernate项目遇UnsatisfiedDependencyException求助
org.springframework.beans.factory.UnsatisfiedDependencyException in Spring MVC + Hibernate Projects Hey there, let's dig into this frustrating issue you're facing. That HTTP 500 error from the servlet init is a classic sign Spring can't properly wire up your beans—so let's break down the most common culprits and fixes step by step:
1. Validate Component Scanning Setup
First, make sure Spring is actually finding your controllers, DAOs, and other beans. If it can't locate them, it can't inject their dependencies.
- For XML config: Double-check you have
<context:component-scan base-package="com.your.project.root"/>where the base package covers all your annotated classes (like@Controller,@Repository,@Service). - For Java config: Ensure your configuration class has
@ComponentScan("com.your.project.root")to scan the right packages.
2. Confirm Correct Annotations on Your Classes
Spring relies on stereotypes to identify beans and their roles. Don't skip these:
- Controllers must have
@Controller(not just generic@Component) - DAO implementations need
@Repository—this also tells Spring to translate Hibernate exceptions into Spring's data access exception hierarchy - If you're using a service layer, mark those classes with
@Service - Use constructor injection (preferred over field injection with
@Autowired) for dependencies—this makes your code more testable and avoids hidden wiring issues
Example of a properly annotated DAO:
@Repository public class UserDaoImpl implements UserDao { private final SessionFactory sessionFactory; // Constructor injection (no @Autowired needed in Spring 4.3+) public UserDaoImpl(SessionFactory sessionFactory) { this.sessionFactory = sessionFactory; } // Your Hibernate CRUD methods here }
And a controller using it:
@Controller @RequestMapping("/users") public class UserController { private final UserDao userDao; public UserController(UserDao userDao) { this.userDao = userDao; } // Request mapping methods here }
3. Check Hibernate SessionFactory Configuration
Most dependency issues in Spring+Hibernate projects boil down to a misconfigured SessionFactory. Verify these key points:
- Your DataSource bean has the correct DB URL, username, password, and driver class
LocalSessionFactoryBeanis set up to scan your entity packages and has valid Hibernate properties (dialect, hbm2ddl.auto, etc.)HibernateTransactionManageris wired to your SessionFactory
Example XML config snippet:
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/your_db"/> <property name="username" value="db_user"/> <property name="password" value="db_pass"/> </bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="packagesToScan" value="com.your.project.model"/> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.MySQL8Dialect</prop> <prop key="hibernate.hbm2ddl.auto">update</prop> <prop key="hibernate.show_sql">true</prop> </props> </property> </bean> <bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"/> </bean>
4. Ensure All Required Dependencies Are Present
Missing JARs can cause Spring to fail creating beans. Check your pom.xml (Maven) or build.gradle (Gradle) for these core dependencies:
- Spring MVC, Spring Core, Spring Context
- Spring ORM (for Hibernate integration)
- Hibernate Core
- Your database's JDBC driver
Example Maven dependencies:
<!-- Spring MVC --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.3.24</version> </dependency> <!-- Spring ORM --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-orm</artifactId> <version>5.3.24</version> </dependency> <!-- Hibernate Core --> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>5.6.14.Final</version> </dependency> <!-- MySQL JDBC Driver --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.33</version> </dependency>
5. Dig Into the Full Stack Trace
Don't stop at the top-level 500 error! The UnsatisfiedDependencyException always has a root cause that tells you exactly what's missing. Look for lines like:
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.your.project.dao.UserDao' available: expected at least 1 bean which qualifies as autowire candidate.
This line will point you directly to the problem—like Spring not finding your DAO bean because you forgot @Repository or component scanning isn't covering its package.
6. Rule Out Circular Dependencies
If two beans depend on each other (e.g., a Service depends on a DAO, and the DAO depends on the same Service), Spring can't wire them up. Refactor your code to eliminate the cycle, or use @Lazy as a temporary fix (though refactoring is better practice).
If you've gone through all these steps and still hit a wall, share the full stack trace and snippets of your Spring config files (web.xml, applicationContext.xml) and relevant classes— that'll help narrow down the issue even further.
内容的提问来源于stack exchange,提问作者bharath

