title: Spring @Autowired fields - which access modifier, private or package-private?
body: <p>Let's say that we use the <code>@Autowired</code> annotation over various fields in a class, and that we didn't write setters or constructors that can also set the fields.</p>
  
  <p>Question - what should the access modifier be, <code>private</code> or <code>package-private</code> (i.e. none) ?</p>
  
  <p>For example: </p>
  
  <pre><code>public class MyClass {
      @Autowired
      private MyService myService;
  }
  </code></pre>
  
  <p>vs</p>
  
  <pre><code>public class MyClass {
      @Autowired
      MyService myService;
  }
  </code></pre>
  
  <p>In the first case (<code>private</code> fields) Spring uses reflection to wire up the field, even if it doesn't have a setter.</p>
  
  <p>The second case (<code>package-private</code> fields) allows us to be able to access those fields (for example, to set up mocks) if we need to extend the class for testing purposes.</p>
  
  <p>So both cases work fine, but which is more recommended, particularly with regards to testing?</p>