title: Inject primitive properties to Spring bean when using @Autowired?
body: <p>try to inject myInt of the following through Spring</p>
  
  <pre><code>public class MyBean {
  @Resource (name="myValue")
  private int myInt;
  </code></pre>
  
  <p>Any idea? I have got the following <a href="https://stackoverflow.com/questions/5467803/spring-bean-primitive-properties-when-using-component-and-autowired">post</a>. But it don't work for me. I still get the following error message.</p>
  
  <pre><code>Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'myValue' must be of type [int], but was actually of type [java.lang.Integer]
  </code></pre>
  
  <h1>Detail:</h1>
  
  <p>The following is the test servlet that I have create for this testing.</p>
  
  <pre><code>@WebServlet("/myTestServlet")
  public class myTestServlet extends HttpServlet {
  
  private static final long serialVersionUID = 1L;
  
  public myTestServlet() {
      super();
  }
  
  protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
      doProcess(request);
  }
  
  protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
      doProcess(request);
  }
  
  private void doProcess(HttpServletRequest request)
  {
      WebApplicationContext applicationContext =
      WebApplicationContextUtils.getWebApplicationContext(
          ((HttpServletRequest) request).getSession().getServletContext());
      MyService service = applicationContext.getBean(MyService.class);
      service.doPrint();
  
  }
  }
  </code></pre>
  
  <p>The following is service bean that I am using for this test.</p>
  
  <pre><code>public class MyService {
  //  @Autowired
  //  @Qualifier("myValue")
  
  
  @Resource (name="myValue")
  private int myInt;
  
  public void doPrint ()
  {
      System.out.println("myInt is " + myInt);
  }
  }
  </code></pre>
  
  <p>The following is the <code>applicationContext.xml</code> that I am using</p>
  
  <pre><code>&lt;context:annotation-config /&gt;
  
  &lt;bean id="myService" class="tsttst.MyService" &gt;
  &lt;/bean&gt;
  
  &lt;!-- Global configuration --&gt;
  &lt;bean id="myValue" class="java.lang.Integer" factory-method="valueOf"&gt;
      &lt;constructor-arg value="1000" /&gt;
  &lt;/bean&gt;
  </code></pre>
  
  <p>The following is the error that I have got when use <code>@Resource</code></p>
  
  <pre><code>Caused by: org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'myValue' must be of type [int], but was actually of type [java.lang.Integer]
      at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:349)
      at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
      at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.autowireResource(CommonAnnotationBeanPostProcessor.java:435)
      at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.getResource(CommonAnnotationBeanPostProcessor.java:409)
      at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor$ResourceElement.getResourceToInject(CommonAnnotationBeanPostProcessor.java:541)
      at org.springframework.beans.factory.annotation.InjectionMetadata$InjectedElement.inject(InjectionMetadata.java:147)
      at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:84)
      at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessPropertyValues(CommonAnnotationBeanPostProcessor.java:297)
      ... 21 more
  </code></pre>
  
  <p>However, if I use <code>@Autowired</code>, the following error will be resulted</p>
  
  <pre><code>Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private int tsttst.MyService.myInt; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [int] 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), @org.springframework.beans.factory.annotation.Qualifier(value=myValue)}
      at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:507)
      at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:84)
      at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:283)
      ... 21 more
  Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [int] 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), @org.springframework.beans.factory.annotation.Qualifier(value=myValue)}
      at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:914)
      at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:783)
      at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:697)
      at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:478)
      ... 23 more
  </code></pre>