java - Singleton implementation of a Spring bean -
We have a spring bean applied as a singleton (default). This bean is used as part of a web service, and at times when multiple simultaneous requests are initiated, throws a NullPointerException while trying to retrieve the response (singleton bean). This usually occurs when the reaction is created, and then a new request begins before sending the original reaction back.
Is this possible due to the Eklavya implementation of Bean? If so, the prototype resolving my problem will not change. What about always starting with the new operator? Will there be any performance effect on doing this?
Edit :
Code Description
Public Class BuildToolRequestProcessor {Private BuildToolResponse buildToolResponse ; ..... and it has been referenced in different ways in the code, setting the response details. The public string process (BuildToolRequestXml buildToolRequestXml) throws exception {buildToolResponse.setLocation (location); ... Public string handles (BuildLuxXXML BuildToolEquestExcill) throws exceptions {buildToolResponse.setSchedule (schedule); ... // And in the second method, when I try to get the location, it throws an Null Pointer exception. BuildToolResponse.getLocation (). GetPinCode () // Bean Configuration & lt; Bean id = "buildToolResponse" class = "com.raj.buildTool.processor.bildToolResponse" /> Extra notes : I tried to delay the first delay before bulding the response to the request. The second request resets the space to clear, and hence the NPE is thrown when trying to retrieve the location. What could be the cause of singletons? Apart from this, I have not initialized buildToolResponse with the new operator, but the BuildToolResponseBuilder is expanding the class BuildToolResponse, which I'm starting to use to create the feedback 'New'.
What could be the reason for the single-prone implementation of bean? If so, the prototype resolving my problem will not change.
If you have a singleton bean, make sure that this bean not maintain any position. This means that in addition to the injection of any other beans or resources done by spring, any area based on some methods should not be restarted. This may be the reason for concurrency issues, especially when Bean is used in many threads (in this case, to participate in many requests for your web service).
This poorly designed for a spring bean will be used on many threads:
@Component public class singletonbird default bean {personal StringBiller ResponseBuilder; @ Owned Private FooService fooService; Public String Method UserComposition () {// Here you will have an answer to a concurrency problem Builder = new StringBuilder (); // In response to writing content / ... // feedback return feedback Büilder.toString () return; }}
To solve this, you have two approaches:
Remove any state of the bean and apply the local variable Move to:
@Component Public Square SingletonBeed Default Bean {// Private StringBuilder RipperBuilder; @ Owned Private FooService fooService; Public string method in user maximum threads () {stringbilder feedbackbuilder = new stringbilder (); // In response to writing content / ... // feedback return feedback Büilder.toString () return; }}
Change the scope of the prototype of Bean
@Component @Scope ("prototype") public class SingletonByDefaultBean { Private stringbuilder feedbackbuilder; @ Owned Private FooService fooService; Public string Vidhiupyogkrta Menadiktm Threads () {responseBuilder = new StringBuilder (); // In response to writing content / ... // feedback return feedback Büilder.toString () return; What is it about always getting started with the new operator? To
To learn how you can create instances manually their classes and manage them through the spring. It is not easy and I will use these methods only when you actually understand what you are doing.
Comments
Post a Comment