使用工厂方法的Spring autowire对象(Spring autowire object using factory method)

系统教程 行业动态 更新时间:2024-06-14 17:01:34
使用工厂方法的Spring autowire对象(Spring autowire object using factory method)

我试图弄清楚如何将这个java代码更改为spring

private MyObject myObject = MyObjectFactory.getService();

在我的Foo.java课程中

@Autowired private MyObject myObject;

在春天的xml中,我有:

<bean id="MyObject" class="path.to.MyObjectFactory" factory-method="getService"></bean> <bean id="Foo" class="path.to.Foo"> <property name="myObject" ref="MyObject"/> </bean>

错误是

No matching bean of type [path.to.MyObject] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency.

I'm trying to figure out how to change this java code into spring

private MyObject myObject = MyObjectFactory.getService();

In my Foo.java class I have

@Autowired private MyObject myObject;

In the spring xml, I have:

<bean id="MyObject" class="path.to.MyObjectFactory" factory-method="getService"></bean> <bean id="Foo" class="path.to.Foo"> <property name="myObject" ref="MyObject"/> </bean>

The error is

No matching bean of type [path.to.MyObject] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency.

最满意答案

您尝试将类型为MyObjectFactory的对象注入path.to.Foo,其中需要MyObject类型的对象。 请改变你的代码:

富:

public class Foo { private MyObject myObject; public void setMyObject(MyObject value) { myObject=value;} }

MyObjectFactory:

public class MyObjectFactory { public MyObject getService() { return new MyObject(); } }

spring xml:

<bean class="MyObjectFactory" id="factory" /> <bean id="myObject" factory-bean="factory" factory-method="getService" scope="prototype" /> <bean id="Foo" class="path.to.Foo"> <property name="myObject" ref="MyObject"/> </bean>

顺便说一下,@ @Autowired和<property ...>在你的情况下表达同样的事情,所以你可以把这一个或另一个留下来。

更新:

看看这里有关春季工厂豆的更多信息。

我假设你不想要单身,这就是我添加scope="prototype" 。 如果您的MyObject实例假设是单例,请将其删除。

you try to inject a object of type MyObjectFactory into path.to.Foo, where a object of type MyObject is expected. please change your code like this:

Foo:

public class Foo { private MyObject myObject; public void setMyObject(MyObject value) { myObject=value;} }

MyObjectFactory:

public class MyObjectFactory { public MyObject getService() { return new MyObject(); } }

spring xml:

<bean class="MyObjectFactory" id="factory" /> <bean id="myObject" factory-bean="factory" factory-method="getService" scope="prototype" /> <bean id="Foo" class="path.to.Foo"> <property name="myObject" ref="MyObject"/> </bean>

By the way, @Autowired and <property ...> express the same thing in your case, so you could leave the one or the other out.

UPDATE:

take a look here for more information factory beans in spring.

i assumed you do not want a singleton, that is why i added scope="prototype". remove it, if your instance of MyObject suppose to be a singleton.

更多推荐

MyObject,java,myObject,Foo,bean>,电脑培训,计算机培训,IT培训"/> <meta name

本文发布于:2023-04-20 18:49:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/2dc342646cfbd498d35d0df60d6582a6.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:对象   工厂   方法   Spring   autowire

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!