DZone
Java Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Java Zone > Creating New Spring Beans on Demand

Creating New Spring Beans on Demand

Jens Schauder user avatar by
Jens Schauder
·
Nov. 12, 12 · Java Zone · Interview
Like (1)
Save
Tweet
31.93K Views

Join the DZone community and get the full member experience.

Join For Free

 We lately had a little Spring related challenge in our application at work. We had a singleton bean (TheClient) that on every call of a method needed a group of other beans. These beans (TheService) unfortunately where stateful and couldn’t be reused on the next call. So they had to be recreated every time.

Not a big deal we thought at first, nothing forces us to use Spring, so we just created the bean using … drum roll … new.

This works fine until you need something from your Spring context. Maybe another bean or something provided via AOP. Of course you can make your bean ApplicationContextAware and look it up using the getBean method on the ApplicationContext.

But this really negates the idea of dependency injection and typically is a sign something is messed up. But how to do it properly? I was a little surprised that my google searches didn’t turn up anything useful. So I brought my question to the place where every IT question belongs these days: Stackoverflow. Sure enough Oliver Gierke, whom I know from him talking at our local java user group pointed me to the solution.

TheClient provides an abstract method returning the required bean:

        public abstract TheService createService()

If configured correctly Spring will provide the implementation for the method.

   <bean id="theService" class="TheService" scope="prototype" />
 
    <bean class="TheClient">
        <lookup-method name="createService" bean="theService" />
    </bean>

Now you can call createService any time you need a fresh Spring managed instance of TheService


Spring Framework

Published at DZone with permission of Jens Schauder, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Package and Deploy a Lambda Function as a Docker Container With AWS CDK
  • Handling Sensitive Data: A Primer
  • Everything I Needed to Know About Observability, I Learned from ‘Bewitched’
  • Waterfall Vs. Agile Methodologies: Which Is Best For Project Management?

Comments

Java Partner Resources

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo