我试图弄清楚如何将这个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
发布评论