scope of commerce facade in hybris - spring

why is the scope of commerce facades marked prototype by default ? Is it that we always need a new instance of the requested facade every time we refer it or am I missing something here while understanding the concept of commerce facades in hybris ?

In commercefacades-spring.xml only some populators and some POJOs are marked prototype.
The facades are still singletons since they are noting but special services.

Hybris v5.x
Facades are singleton scoped beans by default. There is no need to create a new instance of facade object on every request.
Facades in Hybris are the implementation of Facade Design Pattern. To understand them deeply follow the links:
https://wiki.hybris.com/display/release5/Using+Facades+and+DTOs+-+Best+Practice
https://en.wikipedia.org/wiki/Facade_pattern

Related

How concurrency works in SpringBoot when the default lifetime of objects is singleton

In ASP.NET the default lifecycle of objects created in IoC container is per web request. While learning SpringBoot + Webflux, I found that the default lifecycle (Bean, Repository, Service, etc) created by the IoC container is singleton. I know I can change the default scope like this:
#Scope("prototype")
but I have not yet found an example where it would be used. So if IoC creates all object as singletons, how come there are no problems with concurrency. Can someone please explain this to me.
It's a good question. Generally speaking where concurrency is an issue, for example a transaction context in the database layer, springbook uses a thread based locking mechanism. See for example 1.2. Understanding the Spring Framework Transaction Abstraction. Otherwise, yes, anything injected with CDI is a singleton unless specifically annotated otherwise. That means that you should not keep state variables in your #Component or #Service classes. As long as the methods use only parameters passed in or variable local to the method concurrency isn't an issue because ever variable is created on the stack which is unique for each call. I have seen an application work great up until the day two people log in at once.
If you have to have a class with state variables you need to do a new of that class.
Each spring-context is created with a unique thread, so where objects are created or injected that are not stateless then state information is attached to the spring-context which runs in its own thread.
See also How does Spring bean Handle concurrency

Lifespan of a class provided by Laravel Service provider

What is the life span of a custom class (whether singleton or non-singleton) created by custom laravel service provider?
My current understanding is that it gets created and destroyed every time user access a web page that needs the instance of this singleton class.
Also it does not matter whether you have created PHP session or not. Meaning enabling user session does not impact the lifespan of the class provided by service provider.
Is this understanding right or wrong? I could not find a way to figure this out from current laravel documentation. Is there any documentation available that provides more elaboration on this subject?

Laravel - Response:: or response() - which one is better

I was just curious about Facade and service container binding functions in Laravel 5.1, let's say Reponse::json() and response()->json() are the same. But is there any reason that one of them is better than the other?
It is already stated on the Laravel's documentation
Facades provide a "static" interface to classes that are available in
the application's service container. Laravel ships with many facades,
and you have probably been using them without even knowing it! Laravel
"facades" serve as "static proxies" to underlying classes in the
service container, providing the benefit of a terse, expressive syntax
while maintaining more testability and flexibility than traditional
static methods.
And this article by the creator of Laravel will also help you.

ehcache restful service methods override/extend

my question may be vague to ask. I am trying to use ehcache restful service methods such as get or put. I am not sure how this works under hood, is there any way I can override these methods? I mean, get always looks up in cache, but I want to look up in database if not found in cache. I can implement this behavior by writing my own rest service methods, but is there anyway to use ehcache built-in rest functionality by overriding their methods or extend their methods to look up in database if not found in cache. Appreciate your help, thanks.

Should service layer classes be singletons?

I am using Spring framework. Should my service classes be created as singletons? Can someone please explain why or why not? Thanks!
Yes, they should be of scope singleton.
Services should be stateless, and hence they don't need more than one instance.
Thus defining them in scope singleton would save the time to instantiate and wire them.
singleton is the default scope in spring, so just leave your bean definitions as they are, without explicitly specifying the scope attribute.
You can read more about scopes in the spring docs.
Spring is easier to use if you stick with singleton-scoped beans. Singletons are its "default position", if you like. Yes, it supports other scopes (using scope="xyz" in the XML file), but it makes things harder to use and hurts performance.
Essentially, unless you have a good reason to do otherwise, stick with singletons.
You need mostly singletons. (Spring default.) Singletons must be thread-safe, because parallel requests will use the same single instance. In fact, they must be completely stateless, because it can be destroyed and recreated at any time.
If you need to keep track of state inside of your bean (you should not, this should be in the database or stored in the request), you will get many instances of the same type of bean, memory usage goes up with the number of requests, whereby with singletons you will still have just one instance.
Even if you scope you beans to a request, they must still need be at least thread-safe (requests coming from the same browser at the same time).
Service layer should be Singleton, otherwise for every incoming request a new object will be created and these objects are heavy contains business logic and lots of line of code. They must be Singleton.

Resources