Win a copy of Python Continuous Integration and Delivery this week in the Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Devaka Cooray
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Tim Holloway
  • Claude Moore
  • Stephan van Hulst
Bartenders:
  • Winston Gutkowski
  • Carey Brown
  • Frits Walraven

spring beans and state  RSS feed

 
Ranch Hand
Posts: 317
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By default when I use @Component annotation spring creates only singleton object?

Are there design patterns that go beyond a mickey mouse example that I can use where I can have objects with state?
 
author
Posts: 396
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As a Disney fanatic, I take offense to your use of "mickey mouse" to mean simplistic. (Just kidding. But you know that Mickey turned 90 years old this year. I challenge you to find any bit of software that has that kind of longevity!)

Just because @Component results in a singleton by default, you can use @Scope to make it a prototype (one instance per injection), request-scoped (one instance per request), session-scoped (one instance per session), or create your own scoping rules.

That said, state is great for things like entities but is discouraged in most cases because state can lead to concurrency challenges. But if you know what you are doing and have a need for it, then @Scope is your friend.

Also, it's not used all that often, but Spring has long had an annotation called @Configurable that lets you do dependency injection into components that Spring doesn't manage the lifecycle for (such as JPA entities). It uses aspects to pull this off, so be aware of that.

Kalpesh Soni wrote:By default when I use @Component annotation spring creates only singleton object?

Are there design patterns that go beyond a mickey mouse example that I can use where I can have objects with state?

 
Kalpesh Soni
Ranch Hand
Posts: 317
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK but it gets tricky when you have components that are singleton and components that are not

Any "Strategy" of how they can work well with each other?
 
Saloon Keeper
Posts: 20505
115
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Craig Walls wrote:Also, it's not used all that often, but Spring has long had an annotation called @Configurable that lets you do dependency injection into components that Spring doesn't manage the lifecycle for (such as JPA entities). It uses aspects to pull this off, so be aware of that.



Note that Craig means specifically JPA Entities - that is, the classes that map database rows as discrete objects. Spring JPA manages other aspects of JPA (such as repositories) as part of the JPA module's basic capabilities, so it doesn't need aspects for that part. I've never added special aspects to Entities myself - it's a very dangerous thing to do if you aren't careful.

Kalpesh Soni wrote:
Are there design patterns that go beyond a mickey mouse example that I can use where I can have objects with state?



I would consult some good texts on Design Patterns as to when it's best to use singleton objects as opposed to non-singletons and when to make objects stateful versus when to make them stateless. Factory objects are almost always singletons, and commonly (but not inevitably) are stateful. In fact, the Spring Bean Factory is a stateful Singleton. It maintains a cache of all the singleton Spring beans that it creates.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!