重启电脑/IntelliJ后出现BeanCreationException,求Spring+Hibernate排查提示
Hey, let's work through this problem together—your error trace shows a cascading failure starting with an unable to obtain JDBC Connection issue. That's stopping Hibernate's sessionFactory from initializing, which then breaks the autowiring for your CategoryDaoImpl. Here are the key areas to check first:
Check if your database service is running
After restarting your computer, local databases (like MySQL, PostgreSQL) often don't start automatically. Head to your system's service manager (Windows: Services app; macOS/Linux: usesystemctlcommands) and make sure your database service is up and running.Verify your database credentials in
app.properties
Double-check the JDBC properties (likejdbc.url,jdbc.username,jdbc.password) for typos. Common mistakes include wrong port numbers (MySQL defaults to 3306, PostgreSQL to 5432), misspelled database names, or incorrect passwords. Try connecting to the database directly using a client (like MySQL Workbench or pgAdmin) with these credentials to confirm they work.Validate your
dataSourcebean configuration
YourDataConfigclass cuts off at thedataSource()method—make sure this bean is properly set up to pull values fromapp.properties. A correct implementation should look something like this:@Bean public DataSource dataSource() { DriverManagerDataSource dataSource = new DriverManagerDataSource(); dataSource.setDriverClassName(env.getProperty("jdbc.driver")); dataSource.setUrl(env.getProperty("jdbc.url")); dataSource.setUsername(env.getProperty("jdbc.username")); dataSource.setPassword(env.getProperty("jdbc.password")); return dataSource; }Ensure all the property keys in
app.propertiesmatch what you're referencing here.Check for conflicts in
hibernate.cfg.xml
If yourhibernate.cfg.xmlhas its own JDBC connection settings, it might conflict with thedataSourceyou're injecting inDataConfig. Remove any duplicate connection configs from the Hibernate XML file since you're already handling this via Spring'sDataSource.Dig into the root JDBC exception
The error message you shared is a nested exception—look further down your logs to find the original JDBC error. It might say something likeConnection refused(database not running) orAccess denied(bad credentials), which will point you straight to the problem.Confirm database user permissions
Make sure the user you're using has the necessary permissions to access the target database (e.g., create tables, read/write data). While rare, sometimes permission settings can get reset or misconfigured after a system restart.
内容的提问来源于stack exchange,提问作者Ziiiijun

