org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'environment' available

I am getting this following exception on my production while running my code. I don't know what I am making mistake, the same code is happily running on my local machine, please help

Feb 19 13:46:40 ip-10-0-77-139 server: ShopifyGetItems: Service function shop.getShopifyDomain(): org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'hibernateConfig': Injection of autowired dependencies failed; nested exception is 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;
    }

}

Solution 1:

It will be good, if you can debug the issue -

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

if you are getting empty list it means, your bean is not instantiated in spring container and there is some problem with component scan.