2011-02-06 163 views
0

我是Spring DI的新手,我第一次在我的web应用程序中使用DI。 它似乎我做错了,它涉及我使用弹簧scope.here是我的问题 我有一个serive类,我注入各种其他对象,以实现所需的功能 这里是我的春天文件条目Spring依赖注入范围混淆

<bean id="ImportServiceImpl" class="ImportServiceImpl" factory-method="getInstance"> 
     <property name="browseDirectory" ref="browseDirectoryImpl"/> 
     <property name="xmlUnmarshaller" ref="xmlUnmarshallerImpl"/> 
     <property name="Adaptar" ref="AdaptarImpl"/> 
     <property name="DAOService" ref="DAO"/> 
    </bean> 

    <bean id="browseDirectoryImpl" class="BrowseDirectoryImpl" factory-method="getInstance" /> 
    <bean id="xmlUnmarshallerImpl" class="XMLUnmarshallerImpl"/> 
    <bean id="AdaptarImpl" class="AdaptarImpl" factory-method="getInstance"/> 

现在我adaptarImpl类我有一个refrence其他一些对象,是在构造函数初始化

private AdaptarImpl(){ 
    foo=new Foo(); 
} 

现在首次在服务器启动和运行该服务的拳头时一切好的foo正在被启动d为其正确的初始化值,但对于所有其他后续调用Spring正在回应参考先前初始化的foo对象,我希望为每个新请求创建一个新的foo实例。 它在我看来,在上面的代码factory-method="getInstance" 中使用的工厂方法被调用时只有一次调用服务器和所有其他后续calles返回相同的引用,我想为每个请求的新实例。 这里是我的facory方法

public static ImportServiceImpl getInstance(){ 
    return new ImportServiceImpl(); 
} 

我知道我在做基础错误的DI,但不知道它is.I我使用Struts2的使用Spring DI 在这方面的任何帮助将是MVC和服务层乐于助人

感谢 Umesh制作

回答

3

我不知道我理解你的问题,但它听起来像你应该问Spring向Foo注入引用到你的AdapterImpl并使其范围“请求”。如果您需要Spring来控制对象创建,请不要调用“new”。

0
+0

为什么古代版本? Spring 2.0已超过5年!这里是[Bean Scopes Table]的当前版本(http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/beans.html#beans-factory-scopes) – 2011-02-07 07:56:28

+0

@谢恩谢谢。我只是跟着谷歌的第一个链接;)更新... – hvgotcodes 2011-02-07 15:56:29