title: How is Environment bean added in the Spring container?
body: <p>I'm learning about Spring MVC and Hibernate with Java configuration and I have this code:</p>
  
  <pre><code>@Configuration
  @ComponentScan("com.dgs.springdemo")
  @EnableWebMvc
  @EnableTransactionManagement
  @PropertySource({"classpath:persistence-mysql.properties"})
  public class WebAppConfig implements WebMvcConfigurer {
  
      @Autowired
      private Environment env;
  
      private Logger logger = Logger.getLogger(getClass());
  
      @Bean
      public InternalResourceViewResolver viewResolver() {
  
          // create view resolver
          InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
  
          // set properties on view resolver
          viewResolver.setPrefix("/WEB-INF/view/");
          viewResolver.setSuffix(".jsp");
  
          return viewResolver;
      }
  
      @Bean
      public DataSource myDataSource() {
  
          // create connection pool
          ComboPooledDataSource myDataSource = new ComboPooledDataSource();
  
          // set the jdbc driver
          try {
              myDataSource.setDriverClass("com.mysql.jdbc.Driver");       
          }
          catch (PropertyVetoException exc) {
              throw new RuntimeException(exc);
          }
  
          // for sanity's sake, let's log url and user ... just to make sure we are reading the data
          logger.info("jdbc.url=" + env.getProperty("jdbc.url"));
          logger.info("jdbc.user=" + env.getProperty("jdbc.user"));
  
          // set database connection props
          myDataSource.setJdbcUrl(env.getProperty("jdbc.url"));
          myDataSource.setUser(env.getProperty("jdbc.user"));
          myDataSource.setPassword(env.getProperty("jdbc.password"));
  
          // set connection pool props
          myDataSource.setInitialPoolSize(Integer.parseInt(env.getProperty("connection.pool.initialPoolSize")));
          myDataSource.setMinPoolSize(Integer.parseInt(env.getProperty("connection.pool.minPoolSize")));
          myDataSource.setMaxPoolSize(Integer.parseInt(env.getProperty("connection.pool.maxPoolSize")));      
          myDataSource.setMaxIdleTime(Integer.parseInt(env.getProperty("connection.pool.maxIdleTime")));
  
          return myDataSource;
      }
  
      private Properties getHibernateProperties() {
  
          // set hibernate properties
          Properties props = new Properties();
  
          props.setProperty("hibernate.dialect", env.getProperty("hibernate.dialect"));
          props.setProperty("hibernate.show_sql", env.getProperty("hibernate.show_sql"));
  
          return props;               
      }
  
      @Bean
      public LocalSessionFactoryBean sessionFactory(){
  
          // create session factorys
          LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
  
          // set the properties
          sessionFactory.setDataSource(myDataSource());
          sessionFactory.setPackagesToScan(env.getProperty("hibernate.packagesToScan"));
          sessionFactory.setHibernateProperties(getHibernateProperties());
  
          return sessionFactory;
      }
  
      @Bean
      @Autowired
      public HibernateTransactionManager transactionManager(SessionFactory sessionFactory) {
  
          // setup transaction manager based on session factory
          HibernateTransactionManager txManager = new HibernateTransactionManager();
          txManager.setSessionFactory(sessionFactory);
  
          return txManager;
      }
  
  }
  </code></pre>
  
  <p>I see that the Environment bean is injected in this class. And I want to know how is this bean added in the Spring container? Who added it? </p>
  
  <p>This is the pom file:</p>
  
  <pre><code>&lt;project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"&gt;
    &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
    &lt;groupId&gt;com.dgs&lt;/groupId&gt;
    &lt;artifactId&gt;SpringMVCHibernateXML&lt;/artifactId&gt;
    &lt;packaging&gt;war&lt;/packaging&gt;
    &lt;version&gt;1.0&lt;/version&gt;
    &lt;name&gt;SpringMVCHibernateXML Maven Webapp&lt;/name&gt;
    &lt;url&gt;http://maven.apache.org&lt;/url&gt;
      &lt;properties&gt;
          &lt;springframework.version&gt;5.0.2.RELEASE&lt;/springframework.version&gt;
          &lt;hibernate.version&gt;5.4.2.Final&lt;/hibernate.version&gt;
          &lt;mysql.connector.version&gt;8.0.12&lt;/mysql.connector.version&gt;
          &lt;c3po.version&gt;0.9.5.2&lt;/c3po.version&gt;
          &lt;maven.compiler.source&gt;1.8&lt;/maven.compiler.source&gt;
          &lt;maven.compiler.target&gt;1.8&lt;/maven.compiler.target&gt;
      &lt;/properties&gt;
  
    &lt;dependencies&gt;
  
          &lt;!-- Spring MVC support --&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework&lt;/groupId&gt;
              &lt;artifactId&gt;spring-webmvc&lt;/artifactId&gt;
              &lt;version&gt;${springframework.version}&lt;/version&gt;
          &lt;/dependency&gt;
  
          &lt;dependency&gt;
            &lt;groupId&gt;org.springframework&lt;/groupId&gt;
            &lt;artifactId&gt;spring-tx&lt;/artifactId&gt;
            &lt;version&gt;${springframework.version}&lt;/version&gt;
          &lt;/dependency&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework&lt;/groupId&gt;
              &lt;artifactId&gt;spring-orm&lt;/artifactId&gt;
              &lt;version&gt;${springframework.version}&lt;/version&gt;
          &lt;/dependency&gt;
  
          &lt;!-- Hibernate Core --&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.hibernate&lt;/groupId&gt;
              &lt;artifactId&gt;hibernate-core&lt;/artifactId&gt;
              &lt;version&gt;${hibernate.version}&lt;/version&gt;
          &lt;/dependency&gt;
  
          &lt;!-- Servlet, JSP and JSTL support --&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;javax.servlet&lt;/groupId&gt;
              &lt;artifactId&gt;javax.servlet-api&lt;/artifactId&gt;
              &lt;version&gt;3.1.0&lt;/version&gt;
          &lt;/dependency&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;javax.servlet.jsp&lt;/groupId&gt;
              &lt;artifactId&gt;javax.servlet.jsp-api&lt;/artifactId&gt;
              &lt;version&gt;2.3.1&lt;/version&gt;
          &lt;/dependency&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;javax.servlet&lt;/groupId&gt;
              &lt;artifactId&gt;jstl&lt;/artifactId&gt;
              &lt;version&gt;1.2&lt;/version&gt;
          &lt;/dependency&gt;
  
          &lt;!-- C3PO --&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;com.mchange&lt;/groupId&gt;
              &lt;artifactId&gt;c3p0&lt;/artifactId&gt;
              &lt;version&gt;${c3po.version}&lt;/version&gt;
          &lt;/dependency&gt;
  
          &lt;!-- MySQL --&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;mysql&lt;/groupId&gt;
              &lt;artifactId&gt;mysql-connector-java&lt;/artifactId&gt;
              &lt;version&gt;${mysql.connector.version}&lt;/version&gt;
          &lt;/dependency&gt;
  
      &lt;dependency&gt;
        &lt;groupId&gt;junit&lt;/groupId&gt;
        &lt;artifactId&gt;junit&lt;/artifactId&gt;
        &lt;version&gt;3.8.1&lt;/version&gt;
        &lt;scope&gt;test&lt;/scope&gt;
      &lt;/dependency&gt;
    &lt;/dependencies&gt;
    &lt;build&gt;
      &lt;finalName&gt;SpringMVCHibernateXML&lt;/finalName&gt;
  
          &lt;pluginManagement&gt;
              &lt;plugins&gt;
                  &lt;plugin&gt;
                      &lt;!-- Add Maven coordinates (GAV) for: maven-war-plugin --&gt;
                      &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
                      &lt;artifactId&gt;maven-war-plugin&lt;/artifactId&gt;
                      &lt;version&gt;3.2.0&lt;/version&gt;
                  &lt;/plugin&gt;
              &lt;/plugins&gt;
          &lt;/pluginManagement&gt;
    &lt;/build&gt;
  &lt;/project&gt;
  </code></pre>
  
  <p>Also I don't understand why I cannot see this Evironment bean in the Spring container? </p>
  
  <p>Here are all the beans from Spring container, and I cannot see the Environment bean:</p>
  
  <pre><code>org.springframework.context.annotation.internalConfigurationAnnotationProcessor : class org.springframework.context.annotation.ConfigurationClassPostProcessor
  org.springframework.context.annotation.internalAutowiredAnnotationProcessor : class org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor
  org.springframework.context.annotation.internalRequiredAnnotationProcessor : class org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor
  org.springframework.context.annotation.internalCommonAnnotationProcessor : class org.springframework.context.annotation.CommonAnnotationBeanPostProcessor
  org.springframework.context.annotation.internalPersistenceAnnotationProcessor : class org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor
  org.springframework.context.event.internalEventListenerProcessor : class org.springframework.context.event.EventListenerMethodProcessor
  org.springframework.context.event.internalEventListenerFactory : class org.springframework.context.event.DefaultEventListenerFactory
  webAppConfig : class com.dgs.springdemo.config.WebAppConfig$$EnhancerBySpringCGLIB$$b1e6ba9c
  customerController : class com.dgs.springdemo.controller.CustomerController
  org.springframework.web.servlet.config.annotation.DelegatingWebMvcConfiguration : class org.springframework.web.servlet.config.annotation.DelegatingWebMvcConfiguration$$EnhancerBySpringCGLIB$$9f4728e5
  requestMappingHandlerMapping : class org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping
  mvcPathMatcher : class org.springframework.util.AntPathMatcher
  mvcUrlPathHelper : class org.springframework.web.util.UrlPathHelper
  mvcContentNegotiationManager : class org.springframework.web.accept.ContentNegotiationManager
  viewControllerHandlerMapping : class org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport$EmptyHandlerMapping
  beanNameHandlerMapping : class org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping
  resourceHandlerMapping : class org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport$EmptyHandlerMapping
  mvcResourceUrlProvider : class org.springframework.web.servlet.resource.ResourceUrlProvider
  defaultServletHandlerMapping : class org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport$EmptyHandlerMapping
  requestMappingHandlerAdapter : class org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter
  mvcConversionService : class org.springframework.format.support.DefaultFormattingConversionService
  mvcValidator : class org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport$NoOpValidator
  mvcUriComponentsContributor : class org.springframework.web.method.support.CompositeUriComponentsContributor
  httpRequestHandlerAdapter : class org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter
  simpleControllerHandlerAdapter : class org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter
  handlerExceptionResolver : class org.springframework.web.servlet.handler.HandlerExceptionResolverComposite
  mvcViewResolver : class org.springframework.web.servlet.view.ViewResolverComposite
  mvcHandlerMappingIntrospector : class org.springframework.web.servlet.handler.HandlerMappingIntrospector
  org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration : class org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration$$EnhancerBySpringCGLIB$$da8643af
  org.springframework.transaction.config.internalTransactionAdvisor : class org.springframework.transaction.interceptor.BeanFactoryTransactionAttributeSourceAdvisor
  transactionAttributeSource : class org.springframework.transaction.annotation.AnnotationTransactionAttributeSource
  transactionInterceptor : class org.springframework.transaction.interceptor.TransactionInterceptor
  org.springframework.transaction.config.internalTransactionalEventListenerFactory : class org.springframework.transaction.event.TransactionalEventListenerFactory
  viewResolver : class org.springframework.web.servlet.view.InternalResourceViewResolver
  myDataSource : class com.mchange.v2.c3p0.ComboPooledDataSource
  sessionFactory : class org.hibernate.internal.SessionFactoryImpl
  transactionManager : class org.springframework.orm.hibernate5.HibernateTransactionManager
  org.springframework.aop.config.internalAutoProxyCreator : class org.springframework.aop.framework.autoproxy.InfrastructureAdvisorAutoProxyCreator
  </code></pre>