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

重启电脑/IntelliJ后出现BeanCreationException,求Spring+Hibernate排查提示

Troubleshooting Your Spring+Hibernate Connection Issue

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: use systemctl commands) and make sure your database service is up and running.

  • Verify your database credentials in app.properties
    Double-check the JDBC properties (like jdbc.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 dataSource bean configuration
    Your DataConfig class cuts off at the dataSource() method—make sure this bean is properly set up to pull values from app.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.properties match what you're referencing here.

  • Check for conflicts in hibernate.cfg.xml
    If your hibernate.cfg.xml has its own JDBC connection settings, it might conflict with the dataSource you're injecting in DataConfig. Remove any duplicate connection configs from the Hibernate XML file since you're already handling this via Spring's DataSource.

  • 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 like Connection refused (database not running) or Access 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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.21 03:43:18