开发者

Spring Dependency injection scope confusion

I am new to Spring DI and i am implimenting DI for the first time in my webapplication. it seems i am doing somehing wrong and it related the way i am using spring scope.here is my problem i have a serive class where i am injecting various other object in order to achieve the desired functionality here is my spring file entries

<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="Adapta开发者_运维知识库rImpl" class="AdaptarImpl" factory-method="getInstance"/>

now in my adaptarImpl class i have a refrence to some other object which is being initialized in the constructor

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

now for the first time when the server start and this service run fist time everything is fine foo is being initilized to its proper initialization value but for all other subsequent calls Spring is returing refrence to the previous initialized foo object where i want that for each new request a new instance of foo should be created. it apperas to me that the factory method being used in the above code factory-method="getInstance" is being called only once when server called and all other subsequent calles returning same refrence where i want a new instance for every request. here is my facory method

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

i know i am doing basics wrong in DI but not sure where it is.I am using Struts2 for MVC and for service layer using Spring DI any help in this regard will be helpful

Thanks Umesh


I'm not sure I understand your question, but it sounds like you ought to ask Spring to inject the reference to Foo into your AdapterImpl and make its scope "request". If you need Spring to control object creation, don't call "new".


I think you need to look at

http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/beans.html#beans-factory-scopes

and look at table 3.3.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜