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

  • Home
  • SEARCH
  • 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 6153429
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T20:05:34+00:00 2026-05-23T20:05:34+00:00

We use spring to construct/inject our java beans. Here a snippet: <bean id=myAppConfigs class=my.cool.webapp.ApplicationConfig

  • 0

We use spring to construct/inject our java beans. Here a snippet:

<bean id="myAppConfigs" class="my.cool.webapp.ApplicationConfig" scope="singleton">
    <constructor-arg value="8080" />
    <constructor-arg value="MyAppName1" /> 
</bean>

We use the singleton-pattern in

public static ApplicationConfig getCurrentInstance(ServletContext sctx) {
    if (instance == null) {
                WebApplicationContext wac = null;
                if (sctx != null) {
                    wac = WebApplicationContextUtils.getWebApplicationContext(sctx);
                }
return (ApplicationConfig) wac.getBean("myAppConfigs");

Since the bean is only reading some properties which are always the same I doubt there could be problems. But I’m still curious for a nice thread safe way to implement it
There is of course the Double Checked Locking with usage of volatile which is threadsafe.
But is there another way maybe to use the Initialization on demand holder idiom together with function/constructor arguments?

  • 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-05-23T20:05:36+00:00Added an answer on May 23, 2026 at 8:05 pm
    public static ApplicationConfig getCurrentInstance(ServletContext sctx) {
      if (sctx == null) {
        throw new AssertionError("ServletContext is null");
      }
      WebApplicationContext wac = WebApplicationContextUtils.getWebApplicationContext(sctx);
      if (wac == null) {
        throw new AssertionError("No ApplicationContext associated with ServletContext");
      }
      return (ApplicationConfig) wac.getBean("myAppConfigs");
    }
    

    This is plenty thread-safe. Better yet is to strive to use injection all the way (either through annotations or XML bean definitions), but that is not always possible.

    Using Spring (DI) mixed with a singleton-pattern and explicit thread-synchronization is kind of anti-patternish and not really necesarry. The Spring bean factory is in itself thread-safe so you shouldn’t need to add any additional locking/sync on top of it.

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

Sidebar

Related Questions

We use Spring + Hibernate for a Webapp. This Webapp will be deployed on
We have started to use spring aop for cross cutting aspects of our application
In Java, i like to use constructs such as List<String> list = new ArrayList<String>()
I'm trying to use Spring IoC with an interface like this: public interface ISimpleService<T>
Can you use a Spring-WS WebserviceTemplate for calling a webservice and avoid that it
Is it possible to use a Spring container for DI from inside Eclipse plugins?
I'm working on a demo that makes use of Spring.NET IoC capability in ASP.NET
What is a very good jsp framework to use with Spring MVC. Are the
In my Spring app, I'd like to use FreeMarker to generate the text of
My company has been evaluating Spring MVC to determine if we should use it

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.