0

I started to working on Spring Framework.I didn't understand some points.For example I wrote a hibernateConfig file but I did not understand where the environment object is initialized by spring? How can i reach its methods?

My config class:

 package com.ugur.train.configuration;
 import java.util.Properties;

 import javax.sql.DataSource;

 import org.hibernate.SessionFactory;
 import org.springframework.beans.factory.annotation.Autowired;
 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.ugur.train.configuration" })
 @PropertySource(value = { "classpath:application.properties" })
  public class HibernateConfiguration {
      @Autowired 
    private Environment environment;  

    @Bean
    public LocalSessionFactoryBean sessionFactory() {
        LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
        sessionFactory.setDataSource(dataSource());
        sessionFactory.setPackagesToScan(new String[] { "com.huawei.train.model" });
        sessionFactory.setHibernateProperties(hibernateProperties());
        return sessionFactory;
     }

    @Bean
    public DataSource dataSource() {
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName(environment.getRequiredProperty("jdbc.driverClassName"));
        dataSource.setUrl(environment.getRequiredProperty("jdbc.url"));
        dataSource.setUsername(environment.getRequiredProperty("jdbc.username"));
        dataSource.setPassword(environment.getRequiredProperty("jdbc.password"));
        return dataSource;
    }

    private Properties hibernateProperties() {
        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"));
        return properties;        
    }

    @Bean
    @Autowired 
    public HibernateTransactionManager transactionManager(SessionFactory s) {


       HibernateTransactionManager txManager = new HibernateTransactionManager();
       txManager.setSessionFactory(s);
       return txManager;
    }
 }
  • Read https://stackoverflow.com/questions/19414734/understanding-spring-autowired-usage – Vaibs Jul 18 '17 at 06:37
  • Ok.I am reading it – user2986250 Jul 18 '17 at 06:38
  • `environment` is initialized by Spring, so you do not need to take care. That's Spring's purpose. And you are using it's methods already `environment.getRequiredProperty` if not initialized properly, there would be `NullPointerException`... – Betlista Jul 18 '17 at 06:38
  • Thank you very much @Betlista – user2986250 Jul 18 '17 at 06:44
  • @Betlista why do we use autowired annotation here? I can reach all method of the Environment object without autowired annotation – user2986250 Jul 18 '17 at 07:47
  • Autowired is used to inject the Environment object which is created by spring into your configuration class. If you create the Environment object yourself, then its not managed by spring and it wont be able to get the values. – Sangam Belose Jul 18 '17 at 07:47
  • @user2986250 Very likely by "I can reach all methods" you meant in development phase (writing the code). You can do this, because of your import and IDE, but in runtime you will have NPE without `@Autowired` (or if not configured properly as I wrote). – Betlista Jul 18 '17 at 10:40

1 Answers1

0

In @Configuration the recommended way to work with properties is to use Environment and @PropertySource as

@PropertySource(value = { "classpath:application.properties" })

inject the environment as

@Inject
private Environment environment;

add this class to the list of initializers in the @ContextConfiguration

the environment object is initialized as you define @PropertySource

Betlista
  • 10,327
  • 13
  • 69
  • 110
Anshul Sharma
  • 3,432
  • 1
  • 12
  • 17