0

在运行我的代码时,我在生产中遇到了以下异常。我不知道我在犯什么错误,相同的代码在我的本地机器上愉快地运行,请帮助

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: No bean named 'environment' available

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;
    }

}
4

2 回答 2

1

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

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

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

于 2019-02-20T08:43:13.643 回答
0

这在 Spring Boot 2.4 中帮助了我,但在 2.6 中没有帮助

spring:
  config:
    use-legacy-processing: false
于 2022-01-16T18:12:43.697 回答