2011-06-09 66 views
0

試圖通過注入春天在使用@Autowired時向Spring bean注入基本屬性?

public class MyBean { 
@Resource (name="myValue") 
private int myInt; 

任何想法以下的敏?我有以下post。但它對我不起作用。我仍然收到以下錯誤消息。

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] 

詳細信息:

以下是測試的servlet,我已創建了這個測試。

@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(); 

} 
} 

以下是我用於此測試的服務bean。

public class MyService { 
// @Autowired 
// @Qualifier("myValue") 


@Resource (name="myValue") 
private int myInt; 

public void doPrint() 
{ 
    System.out.println("myInt is " + myInt); 
} 
} 

下面是我使用

<context:annotation-config /> 

<bean id="myService" class="tsttst.MyService" > 
</bean> 

<!-- Global configuration --> 
<bean id="myValue" class="java.lang.Integer" factory-method="valueOf"> 
    <constructor-arg value="1000" /> 
</bean> 

以下的applicationContext.xml是,我有錯誤時使用@Resource

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 

但是,如果我用@Autowired中,將會導致以下錯誤

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 

回答

4

int是如何定義的?

如果是屬性(與佔位符配置者爲例),你可以使用@Value

如果用<bean id="myValue" class="java.lang.Integer">..</bean>用constructor-arg或工廠方法定義它,那麼@Autowired/@Resource應該工作。

至於autounboxing - 看來這是行不通的,所以你需要一個Integer
您也可以試試<util:constant .. />

+0

由於值不經常更改並且不想添加更多屬性文件,因此此時不使用佔位符。我在你的答案中使用第二種方法,但是得到了附加的錯誤。我似乎認爲我的spring容器認爲int類型與Java Integer類型不兼容。有任何設置可能會導致此錯誤?謝謝。 PS。當我使用@Autowired而不是@Resource時,出現接縫錯誤。 (我的春天版本是3.0.3,這是否會有所不同?) – 2011-06-10 02:10:32

+0

看到更新.... – Bozho 2011-06-10 05:47:00