使用@Autowired时将原始属性注入Spring bean?

发布于 2024-11-15 01:03:53 字数 5311 浏览 4 评论 0原文

尝试通过 Spring 注入以下 myInt

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

有什么想法吗?我收到以下帖子。但这对我不起作用。我仍然收到以下错误消息。

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);
}
}

以下是我正在使用的 applicationContext.xml

<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>

以下是我在使用 @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

try to inject myInt of the following through Spring

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

Any idea? I have got the following post. But it don't work for me. I still get the following error message.

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]

Detail:

The following is the test servlet that I have create for this testing.

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

}
}

The following is service bean that I am using for this test.

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


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

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

The following is the applicationContext.xml that I am using

<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>

The following is the error that I have got when use @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

However, if I use @Autowired, the following error will be resulted

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

別甾虛僞 2024-11-22 01:03:53

int是如何定义的?

如果它是属性(例如带有占位符配置器),您可以使用@Value

如果它是通过 .. 以及构造函数参数或工厂方法定义的,则@Autowired / @Resource 应该可以工作。

至于自动拆箱 - 似乎它不起作用,所以你需要一个Integer
您还可以尝试

How is the int defined?

If it is property (with a placeholder configurer for example) you can use @Value.

If it is defined with <bean id="myValue" class="java.lang.Integer">..</bean> with either constructor-arg or a factory-method, then @Autowired / @Resource should work.

As for autounboxing - it seems it does not work, so you'd need an Integer.
You can also try <util:constant .. />

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文