org.springframework.beans.factory.NoSuchBeanDefinitionException:没有名为“环境”的豆可用

时间:2019-02-20 08:19:38

标签: spring

我在运行代码时在生产中遇到以下异常。我不知道自己在做什么错,同一代码在我的本地计算机上愉快地运行,请帮忙

  

2月19日13:46:40 ip-10-0-77-139服务器:ShopifyGetItems:服务   函数shop.getShopifyDomain():   org.springframework.beans.factory.BeanCreationException:错误   创建名称为“ hibernateConfig”的bean:注入自动装配   依赖失败;嵌套异常为   org.springframework.beans.factory.NoSuchBeanDefinitionException:否   名为“环境”的咖啡豆可用

import java.util.Properties;

import javax.sql.DataSource;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.orm.hibernate4.HibernateTransactionManager;
import org.springframework.orm.hibernate4.LocalSessionFactoryBean;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@Configuration
@EnableTransactionManagement
@ComponentScan({ "com.webbee.app" })
@PropertySource(value = { "classpath:internal.properties" })
public class HibernateConfig {

    @Autowired
    private Environment environment;

    private Properties hibernateProperties() {
        System.out.println(environment.getRequiredProperty("hibernate.dialect"));
        Properties properties = new Properties();
        properties.put("hibernate.dialect", environment.getRequiredProperty("hibernate.dialect"));
        properties.put("hibernate.show_sql", environment.getRequiredProperty("hibernate.show_sql"));
        properties.put("hibernate.format_sql", environment.getRequiredProperty("hibernate.format_sql"));
        properties.put("hibernate.hbm2ddl.auto", environment.getRequiredProperty("hibernate.hbm2ddl.auto"));
        properties.put("hibernate.jdbc.batch_size", 1000); // batch size for save or update
        return properties;
    }

}

1 个答案:

答案 0 :(得分:1)

如果可以调试问题,那就很好了-

System.out.println("Created beans: " + Arrays.toString(context.getBeanNamesForType(Environment.class)));

如果列表为空,则意味着bean没有在spring容器中实例化,并且组件扫描存在一些问题。