Some Questions on Spring [closed] - spring

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
Please respond to below questions.
1) where are the beans loaded. will they be loaded in Spring ApplicationContext?
OR they are loaded in the heap by ApplicationContext.
2) In a spring Web Application, If the Application is not being used for a day or so, will the ApplicationContext Object garbage collected?
3) How to make singletons thread safe?
4) what are the other Objects that have Threadsafe issues in Spring.
5) BeanPostProcessor's methods execute for each and every bean in the application.
How is it handled in case of thousands of object in a complex application.
Are the beanpostProcessors thread safe?

I think you should better split these questions - each of them deserves a dedicated thread imho.
In any case, Spring loads the beans for you. How? It reads the metadata about the beans (xml/annotations based), and wires up them together. So spring does a "new" for you, But how to access this bean? Here ApplicationContext comes to play. Its just like a container for all these beans, a a container that allows to access the bean instance by its name (getBean("beanName") ).
In a very simplistic approach you can think of the application context as a map of beans by name.
I didn't totally understand the "Heap" concern here. Spring beans are just Java objects living in the JVM, so yes, basically they're loaded in the heap. The only difference is that they're loaded by spring and not by your code.
The application context won't be garbage collected because its stored across (and accessible from) the whole web application.
Of course the beans created by this application context can be garbage collected if they're not singletons or something. I know its a vague answer, feel free to refine your question.
Singleton in its core architecture has nothing to do with a thread safety. Yes, spring provides a 'singleton' bean scope. This means that each time you'll do a getBean call you'll get the same instance, so its a singleton (as long as you manage this bean only in spring). This definition doesn't influence on the design of the bean, so if you, say, create an instance of this class by just calling it's constructor (new keyword), you'll get an entirely different object of this type than that managed by spring.
Its important to understand where the responsibility of Spring as a framework starts and when it ends :)
So if you want your singleton to be thread safe, you should design it to be thread safe.
The best would be doing it stateless, but again, it has nothing to do with spring, so please ask it in a different thread, or just google for "thread-safe design"
Don't really understood the question, so I can't answer, sorry :(
Again the thread safety of the bean post processor depends on what actually written there, its your code there, not the infrastructure code.
Hope this helps

Related

Why does spring use ioc and di?

I'm new to spring 5 and my question is why does spring use DI and IOC? I mean why do we have to write the beans in an XML (legacy) and then create it where we need it? Why don't we use one method instead that gives us that object, until we want to use this complex mechanism that happens in the spring container?
And another question is, does not reading XML slow down the program? Because we are reading from the hard disk anyway.
Note: It is true that we can use annotations, but for now I want to ask a question about reading from xml.
Spring IoC Container is the core of Spring Framework. It creates the objects, configures and assembles their dependencies, manages their entire life cycle. The Container uses Dependency Injection(DI) to manage the components that make up the application. It gets the information about the objects from a configuration file(XML) or Java Code or Java Annotations and Java POJO class. These objects are called Beans. Since the Controlling of Java objects and their lifecycle is not done by the developers, hence the name Inversion Of Control.
More on link HERE
As for your first part of the question.
why does spring use DI
To allow the developer to keep his code loose, and not entagle classes, it keeps your code clean.
In object oriented design, the amount of coupling refers to how much the design of one class depends on the design of another class. In other words, how often do changes in class A force related changes in class B? Tight coupling means the two classes often change together, loose coupling means they are mostly independent. In general, loose coupling is recommended because it's easier to test and maintain.
You may find this paper by Martin Fowler (PDF) helpful.
I mean why do we have to write the beans in an XML (legacy) and then create it where we need it
Note: We write the bean in XML and it is created when application starts when it looks at bean defintion, techinally you are never creating a bean, you are fetching only created bean from Spring Container(IOC) that Spring created for you when you started your application.
We are writing bean blueprint, or just bean, so that it can be constructed, placed in the Spring Container when the application starts, and then we have it at our disposal that we can fetch it using getBean method.
The whole point of "why", is because by default all beans are scoped as singleton, that means, when you fetch a bean, and do with it whatever you want, you do not worry about memory or anything, Spring takes care of the beans for you if they are scoped as a Singleton.
Second question:
And another question is, does not reading XML slow down the program? Because we are reading from the hard disk anyway.
There is no difference in performance between annotation or XML, it is just a different approach, I am not sure what you mean by "reading from hard disk", but one way or another you will have to configure your application, yes, many forums prefer to run away from XML, but in my honest opinion the only reason for that is because when you write a bad configuration in XML it is lot harder to find it compared to configuration in Java that will throw an exception.
XML, application.properties files require a redeployment of the application, while annotation and java configuration require recompilation of your project, so both of them have "flaws", but it is normal and quite understandable to me.
But in the end I believe that it is a matter of preference, I know personally quite a few people that combine annotations with XML configuration and they know lot more about Spring compared to me.
So in summary, it is pain to write beans and their configuration, same as you can write a class with methods without creating an interface for it since the result will be the same, but it will help you in the long run since you do not have to worry about memory or if you destroyed that bean or if you did not.
It would be nice that you read about
1.Lazy initialization of beans
2.Eager initialization of beans
3.Singleton scope of beans
4.Prototype scope of beans

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

Thread-safety of Singleton-Scoped Spring Beans in Context per Thread Environment

I deal with a framework "X" which starts multiple threads and from each of them calls methods from a self-written JAR library. I wanted to get benefits from both of frameworks "X" and Spring, so i've designed the method which is being called from "X" in a way that it initializes Spring Context. In such way I have a Spring Context in each Thread. I can't show the code, so that's the brief description of the case. Everything is fine, just I'm just wondered about some circumstances:
Does that situation mean that every singleton scoped bean is becoming automatically Thread-safe (framework X handles threads itself) so I don't have to bother about ThreadLocals, local variables and can I do everything without synchronization with instance variables.
Do class variables still shared among all threads/spring context or bean class variables in this case become local to context?
I have some other question if you don't mind I'll edit this question later yet. And maybe I'll write some resume of conclusions if get answers here.
Thank You all in advance.

How do I get one common instance of ApplicationContext using Spring in Java

I know that Spring is most useful for dependency injection. But what I don't understand is how do I have one common "ApplicationContext ac=....." for the whole project, lets say I have a WebApplication and it has multiple number of packages but all of them still in one project , so how do I make the ApplicationContext instantiate only once. I had read somewhere that in Spring objects are initialized only once something as singleton beans but what I dont understand is that how is it different than Singleton design pattern, there is one question on this in SO but I couldn't quite clearly understand the answer as I am a newbie to Spring trying to learn by myself. Any help would really be appreciated. Sorry if the Q is too long. Hope I was able to explain my doubt clearly.
Spring veans are by default Singleton, although it is possible to configure them as prototype which means that a new bean will be created upon each request. In practice, singleton means one instance per context and are their lifecycle is managed by Spring nwhich also provides hooks into the various stages. Spring does not manage prototype beans once they have been created.
It is common in a SpringMVC application to have more than one context (one for the business services, the other for the web controllers). You would only need to create an ApplicationContext when building a standalone application. SpringMVC applications use the ContextLoaderListener to create the necessary contexts.

Is Spring Container is just like a jvm? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Is Spring Container is just like a jvm? or it is different?
Why is Spring IOC mainly used? If it is for creating the objects without using new operator? what is wrong in using new operator?
If we are creating singleton objects and return the same object whenever application wants, we are loading all the objects on server start up? will that not make the application heavy?
If it is so, then why we need spring core?
How is filter,bean post processor, aop is different?
if aop is used for implementing cross cutting concern, why do we need beanProcessor interface?
Is Spring Container is just like a jvm?
No, Spring is a Java framework. It provides classes you can use to run a Java application on a JVM.
Why is Spring IOC mainly used?
Learn what Inversion of Control is and you will understand why it is used so heavily.
If it is for creating the objects without using new operator? what is
wrong in using new operator?
The new keyword forces compile time dependencies. Inversion of Control and Dependency Injection remove those dependencies, mostly through reflection.
If we are creating singleton objects and return the same object
whenever application wants, we are loading all the objects on server
start up? will that not make the application heavy?
You will usually want all those objects at startup so it's a non-issue. You can delay the initialization of those objects (beans) with lazy loading.
If it is so, then why we need spring core?
If what is so?
How is filter,bean post processor, aop is different?
The BeanFactory creates and initializes beans. A BeanPostProcessor is meant to wrap a bean in a proxy or modify that bean's properties. The javadoc has more details.
Aspect oriented programming is a style of programming. In order to implement it with plain old Java, you need to use JDK or CGLIB proxies. Those are applied using BeanPostProcessor instances by wrapping the processed bean. Calls going to the target bean will be intercepted by the proxy which will (possibly) perform logic before delegating to the target bean. Java's AOP capabilities are almost completely limited to method calls.

Resources