This week's book giveaway is in the Beginning Java forum.
We're giving away four copies of Get Programming with Java (MEAP only) and have Peggy Fisher on-line!
See this thread for details.
Win a copy of Get Programming with Java (MEAP only) this week in the Beginning Java 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
  • Jeanne Boyarsky
  • Bear Bibeault
  • Knute Snortum
  • Liutauras Vilda
Sheriffs:
  • Tim Cooke
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Ron McLeod
  • Ganesh Patekar
  • salvin francis
Bartenders:
  • Tim Holloway
  • Carey Brown
  • Stephan van Hulst

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?
 
Bartender
Posts: 20146
103
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.
 
Always! Wait. Never. Shut up. Look at this tiny ad.
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database
https://coderanch.com/t/704633/RavenDB-Open-Source-NoSQL-Database
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!