简体   繁体   中英

Scope of Spring beans enforcement

I have a scenario for example.

<bean id="xyzService" class="XyzServiceImpl" scope="prototype">
    <property name="aDependency" ref="aDependency" />
    <property name="bDependency" ref="bDependency" />
</bean>

<bean id="useService" class="UseServiceImpl">
    <property name="xyzService" ref="xyzService"/>
</bean>

Java Class :

public class XyzServiceImpl implements XyzService{
     private ADependency aDependency= null;
   private BDependency bDependency= null;
   // getters and setters...
}

 public class UseServiceImpl implements UseService {
     private XyzService xyzService= null;
 // getters and setters...
    xyzService.doSomething();
}

Now every time inside the UseServiceImpl I expect a new Instance of xyzService, but i always return the same singleton instance. Also there is a scenario that the aDependency and bDependency may internally have again some more references to other beans.

Now I have a question like how do I get an new Instance of xyzService. Am I doing something wrong?

By default scope of spring bean is singleton , You need to mark the scope prototype to instruct spring

<bean id="beanId" class="some.class.Name" scope="prototype"/>

Spring will create new instance on each request of Bean


See

I could easily find the solution by implementing the ApplicationContextAware Interface which has the getter and setter method for context. From the context I can say getBean and get the new Instance

public class UseServiceImpl implements UseService,ApplicationContextAware {
     private ApplicationContext context;
     XyzService xyzService= context.getBean(XyzServiceImpl.class);
   // getter and setter for applicationContext
     private XyzService xyzService= null;
   // getters and setters...
    xyzService.doSomething();
}

If you have the following:

<bean id="xyzService" class="XyzServiceImpl" scope="prototype">
    <property name="aDependency" ref="aDependency" />
    <property name="bDependency" ref="bDependency" />
</bean>

<bean id="useService1" class="UseServiceImpl">
    <property name="xyzService" ref="xyzService"/>
</bean>

<bean id="useService2" class="UseServiceImpl">
    <property name="xyzService" ref="xyzService"/>
</bean>

Then you should be able to verify that the xyzService property for useService1 and useService2 do contain different instances of xyzService . That's the effect of declaring xyzService to be scoped as a prototype. If you really want new instances of the xyzService bean to be available during the lifetime of the useService bean, I think you'll need a different approach - take a look at the documentation for Method injection .

In your example, every time you request spring container an instance of userService , it will return the singleton instance and injecting a new instance of xyzService .

However, when spring creates a new instance of xyzService , it will use the singleton instance of aDependency and bDependency unless otherwise they are also defined as prototype .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM