title: @Configuration class not being able to use JPA repository during its initialization causing NoSuchBeanDefinitionException
body: <p>I have a strange issue with a Spring <code>@Configuration</code> class: one of the bean defined in it will not find its dependencies.</p>
  
  <p>Here is the <code>@Configuration</code> class:</p>
  
  <pre><code>@Configuration
  @EnableGlobalMethodSecurity(prePostEnabled = true)
  public class MethodSecurityConfiguration extends GlobalMethodSecurityConfiguration {
  
      @Override
      protected AuthenticationManager authenticationManager() {
          AuthenticationManager authenticationManager = new ProviderManager();
          return authenticationManager;
      }
  
      @Override
      protected MethodSecurityExpressionHandler createExpressionHandler() {
          DefaultMethodSecurityExpressionHandler expressionHandler = new DefaultMethodSecurityExpressionHandler();
          expressionHandler.setPermissionEvaluator(permissionEvaluator());
          return expressionHandler;
      }
  
      @Bean
      public ApplicationPermissionEvaluator permissionEvaluator() {
          return new ApplicationPermissionEvaluator(permissionMap());
      }
  
      private Map&lt;String, Permission&gt; permissionMap() {
          Map&lt;String, Permission&gt; map = new HashMap&lt;&gt;();
          map.put("updateAdvertisementIsAllowed", advertisementOwnerPermission());
          map.put("advertisementByIdOwnerPermission", advertisementByIdOwnerPermission());
  
          return map;
      }
  
      @Bean
      public AdvertisementOwnerPermission advertisementOwnerPermission() {
          return new AdvertisementOwnerPermission();
      }
  
      @Bean
      public AdvertisementByIdOwnerPermission advertisementByIdOwnerPermission() {
          return new AdvertisementByIdOwnerPermission();
      }
  
  
  }
  </code></pre>
  
  <p>Here is the <code>AdvertisementByIdOwnerPermission</code> bean:</p>
  
  <pre><code>@Component
  public class AdvertisementByIdOwnerPermission implements Permission {
  
      @Autowired
      private AdvertisementRepository advertisementRepository;
  ...
  </code></pre>
  
  <p>Here is the stacktrace:</p>
  
  <pre><code>Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'advertisementByIdOwnerPermission': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.bignibou.repository.advertisement.AdvertisementRepository com.bignibou.configuration.security.permission.AdvertisementByIdOwnerPermission.advertisementRepository; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.bignibou.repository.advertisement.AdvertisementRepository] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
      at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:292)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1185)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475)
      at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:304)
      at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
      at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:300)
      at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:195)
      at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:324)
      at com.bignibou.configuration.security.MethodSecurityConfiguration$$EnhancerBySpringCGLIB$$b8785a07.advertisementByIdOwnerPermission(&lt;generated&gt;)
      at com.bignibou.configuration.security.MethodSecurityConfiguration.permissionMap(MethodSecurityConfiguration.java:49)
      at com.bignibou.configuration.security.MethodSecurityConfiguration.permissionEvaluator(MethodSecurityConfiguration.java:41)
      at com.bignibou.configuration.security.MethodSecurityConfiguration$$EnhancerBySpringCGLIB$$b8785a07.CGLIB$permissionEvaluator$6(&lt;generated&gt;)
      at com.bignibou.configuration.security.MethodSecurityConfiguration$$EnhancerBySpringCGLIB$$b8785a07$$FastClassBySpringCGLIB$$82172390.invoke(&lt;generated&gt;)
      at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228)
      at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:312)
      at com.bignibou.configuration.security.MethodSecurityConfiguration$$EnhancerBySpringCGLIB$$b8785a07.permissionEvaluator(&lt;generated&gt;)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:483)
      at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:166)
      ... 60 more
  Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.bignibou.repository.advertisement.AdvertisementRepository com.bignibou.configuration.security.permission.AdvertisementByIdOwnerPermission.advertisementRepository; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.bignibou.repository.advertisement.AdvertisementRepository] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
      at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:508)
      at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
      at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:289)
      ... 81 more
  Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.bignibou.repository.advertisement.AdvertisementRepository] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
      at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1103)
      at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:963)
      at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:858)
      at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:480)
      ... 83 more
  </code></pre>
  
  <p>I can't figure out why Spring won't find the <code>AdvertisementRepository</code> defined in the <code>AdvertisementByIdOwnerPermission</code> whereas it is properly picked by the rest of the application...</p>
  
  <p>Can anyone please help?</p>
  
  <p><strong>edit 1</strong>: Information about my Spring Data Jpa usage:</p>
  
  <p>Spring Data Jpa configuration:</p>
  
  <pre><code>@Configuration
  @EnableJpaRepositories(basePackages = "com.bignibou.repository")
  public class JpaConfiguration {
  
  }
  </code></pre>
  
  <p>Interface for Repository:</p>
  
  <pre><code>@RooJpaRepository(domainType = Advertisement.class)
  public interface AdvertisementRepository {
  
  }
  </code></pre>
  
  <p><strong>edit 2</strong>: I thought it might have something to do with order of initialization of my configuration classes. I tried adding <code>@Order(Ordered.LOWEST_PRECEDENCE + 10)</code> to my MethodSecurityConfiguration class but it still does not work.</p>