title: Cannot resolve bean in SpEL for Spring Data MongoDB collection name
body: <p>I am trying to customize the collection name where an entity class is saved into and indexed, using Spring Data MongoDB and Spring Batch. The class is declared as follows:</p>
  
  <pre><code>@Document
  @CompoundIndex(name = "unique_source", def = "{'fid': 1, 'sid': 1}", unique = true, background = true)
  public class VariantSource {
      ...
  }
  </code></pre>
  
  <p>And the item writer:</p>
  
  <pre><code>public class VariantSourceMongoWriter extends MongoItemWriter&lt;VariantSource&gt; {
  
      public VariantSourceEntityMongoWriter(MongoOperations mongoOperations, String collectionName) {
          setTemplate(mongoOperations);
          setCollection(collectionName);
      }
  }
  </code></pre>
  
  <p>Saving works fine: the objects are written into the collection provided as argument. The problem is that the indexes are created in the default collection, named after the class name (<code>variantSource</code>).</p>
  
  <p>After reading <a href="https://stackoverflow.com/questions/29597482/spring-data-mongodb-repository-with-custom-collection-name">this</a> and <a href="https://jira.spring.io/browse/DATAMONGO-525" rel="nofollow noreferrer">this</a>, I created the following:</p>
  
  <pre><code>public class MongoCollections {
  
      public String getCollectionFilesName() {
          return "my_custom_collection_name"; // TODO Dynamic value
      }
  }
  
  
  @Configuration
  public class MongoCollectionsConfiguration {
  
      @Bean
      public MongoCollections mongoCollections() {
          return new MongoCollections(); 
      }
  
  
  @RunWith(SpringRunner.class)
  @ContextConfiguration(classes = {MongoCollectionsConfiguration.class})
  public class VariantSourceMongoWriterTest {
  
      @Autowired
      private MongoCollections mongoCollections;
  
  }
  </code></pre>
  
  <p>I have checked the instance is correctly autowired into the unit tests, but I can't make it work with SpEL.</p>
  
  <p>After changing the <code>@Document</code> annotation to look like this:</p>
  
  <pre><code>@Document(collection = "#{@mongoCollections.getCollectionFilesName()}")
  </code></pre>
  
  <p>the following exception is thrown:</p>
  
  <blockquote>
    <p>org.springframework.expression.spel.SpelEvaluationException: EL1057E:(pos 1): No bean resolver registered in the context to resolve access to bean 'mongoCollections'</p>
  </blockquote>
  
  <p>And if I use this:</p>
  
  <pre><code>@Document(collection = "#{mongoCollections.getCollectionFilesName()}")
  </code></pre>
  
  <p>the exception is this one:</p>
  
  <blockquote>
    <p>org.springframework.expression.spel.SpelEvaluationException: EL1007E:(pos 0): Property or field 'mongoCollections' cannot be found on null</p>
  </blockquote>
  
  <p>Finally, the following creates a collection with the name as specified, symbols included:</p>
  
  <pre><code>@Document(collection = "@mongoCollections.getCollectionFilesName()")
  </code></pre>