Announcement Announcement Module
No announcement yet.
Question related to protype scoped beans Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Question related to protype scoped beans

    Hello ,

    I have a question regarding protype scope.

    I am using spring 3.0.I am using spring webservice module to create webservices and spring jdbc for data access.
    The dao methods are Autowired to webservice.

    Spring webservice uses a helper class which has dao methods autowired to it. Earlier this class was assumed to be singleton so it was autowired to service everything worked fine.

    Due to design change now this class need to maintain the state . I.e. remember values between method call. So we need to create a new instance of helper class everytime service is called. To resolve this I changed application context.xml and added bean with prototype scope like this :

    <bean id="abcHelper" class="" scope="prototype">
    I am getting the bean from application context .

    My bean class look like this :

    public class AbcHelper {
    private AncDAO abcDAO;
    public void initialize(String xyz){
            //initialization stuff
    public String getXXX(){
    I am calling this from spring webserivce as below :

    AbcHelper abcHelper = (AbcHelper) context.getBean("abcHelper");
    //some more stuff
    Question : Is this approach correct. In general creating prototype scoped beans is considered a bad idea . If this approach is not correct what else can be done in my case. I need to call the helper class which needs dataaccess objects injected by spring container. And this also needs to maintain the state between invocations.

    This webservice is gonna be a heavily hit webservice with millions of hit each day. Would prototype scope will make it slow ?

  • #2
    I don't see any issue with this approach if you're sure that you have to use prototype scoped bean. I don't think using prototype bean necessarily slows down but of course since it is created per invocation and then may-be garbage collected, so there's some overhead. Rest depends upon the helper, if its instantiation is time consuming and so on.


    • #3
      I don't see any affair with this access if you're abiding that you accept to use ancestor scoped bean.

      I don't anticipate application ancestor bean necessarily slows down but of advance back it is created per abracadabra and again may-be debris collected, so there's some overhead. Rest depends aloft the helper, if its instantiation is time arresting and so on.