title: No bean named 'org.springframework.context.annotation.ConfigurationClassPostProcessor.importRegistry' is defined
body: <p>i am trying to run full package of junit test classes, and i have an audit classes for my domain classes as follows:</p>
  
  <pre><code>    @PrePersist
      public void prePersist(AuditableEntity e) {
  
        UserService userService = SpringBeanFactory.getBean(UserService.class);  
        // some auditing here  
      }
  </code></pre>
  
  <p><strong>- SpringBeanFactory class:</strong></p>
  
  <pre><code>public class SpringBeanFactory {
  
      private static ApplicationContext applicationContext;
  
      public static &lt;T&gt; T getBean(final String name, final Class&lt;T&gt; requiredType) {
          T bean = null;
          if (applicationContext != null) {
              bean = applicationContext.getBean(name, requiredType);
          }
          return bean;
      }
  
      public static &lt;T&gt; T getBean(final Class&lt;T&gt; requiredType) {
          T bean = null;
          if (applicationContext != null) {
              bean = applicationContext.getBean(requiredType);
          }
          return bean;
      }
  
      public static void setApplicationContext(final ApplicationContext applicationContext) {
          if (SpringBeanFactory.applicationContext == null) {
              SpringBeanFactory.applicationContext = applicationContext;
          }
      }
  
  }
  </code></pre>
  
  <p><strong>-Test class config:</strong></p>
  
  <pre><code>@Autowired
  private ApplicationContext applicationContext;
  
  @Before
  public void before() throws Exception {
  
      SpringBeanFactory.setApplicationContext(applicationContext);
  
  }
  </code></pre>
  
  <p><strong>-SpringTestingConfig class:</strong></p>
  
  <pre><code>@Configuration
  @ComponentScan(basePackages = "com.myapp.data", excludeFilters = { @Filter(Configuration.class) })
  @PropertySource("classpath:/test.properties")
  @Profile("test")
  public class SpringTestingConfig {
  
      private static Logger log = (Logger)LoggerFactory.getLogger(SpringTestingConfig.class);
  
      @Autowired
      private ApplicationContext applicationContext;
  
      @Bean
      public DataSource XdataSource() {
          DriverManagerDataSource dataSource = new DriverManagerDataSource();
          if(log.isDebugEnabled()) log.debug("profile.name", "test");
          System.setProperty("profile.name", "test");
  
          dataSource.setDriverClassName("org.h2.Driver");
          String schemaName = ConfigurationUtil.config().getString("db.schema.name").toLowerCase();
          log.debug("SCHEMA IS " + schemaName);
          String url = "jdbc:h2:mem:test;MODE=Mysql;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE;INIT=CREATE SCHEMA IF NOT EXISTS " +schemaName +"\\;" + "SET SCHEMA "+schemaName;
          dataSource.setUrl(url);
          //dataSource.setUrl("jdbc:h2:mem:test;MODE=Mysql;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE;INIT=CREATE SCHEMA IF NOT EXISTS "    + schemaName);
  
          dataSource.setUsername("sa");
  
          //use your own local mysql in tests here...
  //      dataSource.setDriverClassName("com.mysql.jdbc.Driver");
  //      dataSource.setUrl("jdbc:mysql://localhost:3306/mv_tests?characterEncoding=UTF-8");
  //      dataSource.setUsername("tomcat");
  //      dataSource.setPassword("tomcat");
  //        
          return dataSource;
      }
  
      @Bean
      public DataSource dataSource() {
  
          SpringBeanFactory.setApplicationContext(applicationContext);
          LoggerUtils.setAllApplicationLogs("DEBUG");
          DriverManagerDataSource dataSource = new DriverManagerDataSource();
          if(log.isDebugEnabled()) {
              log.debug("profile.name", "test");
          }
          System.setProperty("profile.name", "test");
  
          dataSource.setDriverClassName("com.mysql.jdbc.Driver");
          String schemaName = ConfigurationUtil.config().getString("db.schema.name");
          String username = ConfigurationUtil.config().getString("db.username");
          String password = ConfigurationUtil.config().getString("db.password");
          if( log.isDebugEnabled() ) {
              log.debug( "SCHEMA IS " + schemaName );
              log.debug( "Username IS " + username );
              log.debug( "Password IS " + password );
          }
  
          dataSource.setUrl("jdbc:mysql://localhost:3306/"+schemaName);
          dataSource.setUsername(username);
          dataSource.setPassword(password);
  
          return dataSource;
      }
  
  }
  </code></pre>
  
  <p><strong>-Test class annotations:</strong></p>
  
  <pre><code>@RunWith(SpringJUnit4ClassRunner.class)
  @TestExecutionListeners({ WebContextTestExecutionListener.class, DependencyInjectionTestExecutionListener.class, DirtiesContextTestExecutionListener.class, TransactionalTestExecutionListener.class })
  @ActiveProfiles("test")
  @DirtiesContext
  @ContextConfiguration(loader = AnnotationConfigContextLoader.class, classes = { SpringConfig.class, SpringTestingConfig.class, SpringLocalContainerJPAConfig.class, CustomConfiguration.class })
  @Transactional
  </code></pre>
  
  <p>when my test method tries to save an entity, it makes call to PrePersist method which in turn makes call to the getting spring service:</p>
  
  <pre><code>UserService userService = SpringBeanFactory.getBean(UserService.class);
  </code></pre>
  
  <p>which in turns produces the following exception:</p>
  
  <pre><code>Error creating bean with name 'userService': 
  Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: 
  Could not autowire field: private com.motivosity.data.repository.UserRepository com.motivosity.service.impl.UserServiceImpl.userRepository; 
  nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userRepositoryImpl': 
  Injection of persistence dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: 
  Error creating bean with name 'springLocalContainerJPAConfig': Injection of autowired dependencies failed; 
  nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: 
  javax.sql.DataSource com.motivosity.data.config.SpringLocalContainerJPAConfig.dataSource; 
  nested exception is org.springframework.beans.factory.BeanCreationException: 
  Error creating bean with name 'springTestingConfig': Initialization of bean failed; 
  nested exception is org.springframework.beans.factory.BeanCreationException: 
  Error creating bean with name 'org.springframework.cache.annotation.ProxyCachingConfiguration': 
  Initialization of bean failed; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: 
  No bean named 'org.springframework.context.annotation.ConfigurationClassPostProcessor.importRegistry' is defined
  </code></pre>
  
  <p>i have to mention that this exception occurs when running full package of test classes, but when running this test class separately no exception is produced.</p>
  
  <p>BTW, i am using <strong>spring 3.2.3.RELEASE</strong></p>
  
  <p><strong>UPDATE:</strong> when i upgraded the spring version to latest release 4.0.3, i am getting a new exception on the same get UserService line:</p>
  
  <pre><code>org.springframework.context.support.GenericApplicationContext@3aa54263 has been closed already
  </code></pre>
  
  <p>please advise how to fix this exception.</p>