DZone
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
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Coding
  3. Frameworks
  4. Creating New Spring Beans on Demand

Creating New Spring Beans on Demand

Jens Schauder user avatar by
Jens Schauder
·
Nov. 12, 12 · Interview
Like (1)
Save
Tweet
Share
32.18K 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

  • Secrets Management
  • Top 12 Technical Skills Every Software Tester Must Have
  • Too Many Tools? Streamline Your Stack With AIOps
  • Cloud-Native Application Networking

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • 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: