Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7793059
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T22:22:01+00:00 2026-06-01T22:22:01+00:00

I have a spring injected service class A{ List l = new ArrayList(); public

  • 0

I have a spring injected service

 class A{
      List l = new ArrayList();

      public m1(){
           //do some additions in list 
      }

      public m2(){
           //do some new additions in list
      }
 }

Now because creating of objects of A, is in the hands of spring the behavior of program is not what is expected. (I expect list to be available empty always but not initialized by methods for some wired reason)

Will Spring always create only one instance of A, so that list l will keep on growing, I have configured bean as singleton in application context.

If yes, naturally I must initialize the list inside the functions m1 & m2 or callee must past the reference, and in my case callee being struts2 actions they are not singleton so this issue can be solved?

Or

Does spring provide any support in configuration to initialize member variables at every call or something else?

More generally what are best practices to have in writing services injected by spring about using member variables/ local variables for performance & efficiency.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T22:22:03+00:00Added an answer on June 1, 2026 at 10:22 pm

    Trying to answer following :
    Does spring provide any support in configuration to initialize member variables at every call or something else?

    By default Spring beans are singleton. Initialized only once and use the same object again and again.

    However, if requirement changes as you asked. You need to understand scope attribute provided by the Spring.

    <bean id="id" class="com.test.TP" scope="prototype">
    </bean>
    

    Refer following for detail understanding.
    Scope Description

    • singleton

      Scopes a single bean definition to a single object instance per
      Spring IoC container.

    • prototype

      Scopes a single bean definition to any number of object instances.

    • request

      Scopes a single bean definition to the lifecycle of a single HTTP
      request; that is each and every HTTP request will have its own
      instance of a bean created off the back of a single bean definition.
      Only valid in the context of a web-aware Spring ApplicationContext.

    • session

      Scopes a single bean definition to the lifecycle of a HTTP Session.
      Only valid in the context of a web-aware Spring ApplicationContext.

    • global session

      Scopes a single bean definition to the lifecycle of a global HTTP
      Session. Typically only valid when used in a portlet context. Only
      valid in the context of a web-aware Spring ApplicationContext.

    http://static.springsource.org/spring/docs/3.0.0.M3/spring-framework-reference/html/ch04s04.html

    It is also possible to have user defined scope such as thread scope.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given I have a Spring bean configured as @Service(myService) public class DefaultService extends MyService
I have the following class (which is a JPA entity listener): @Service public class
I have some legacy code that I am now trying to re-use under Spring.
We have an application with a plugin which contains a service: public class TaskService
I have two Spring proxies set up: <bean id=simpleBean class=org.springframework.aop.framework.ProxyFactoryBean> <property name=target> <ref local=simpleBeanTarget/>
I have this Spring config: <bean id=boo class=com.x.TheClass/> The class TheClass implements TheInterface .
I have a service manager class used to abstract my calls from my MVC
I have a page with a datatable that reads data from a service class.
based on this example : @Service public class Purchase { @PersistenceContext private EntityManager em;
I have three hierarchical layers injected in Spring - rest, business logic and database

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.