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.
Related
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
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 4 years ago.
Improve this question
I don't get the benefit of using Spring instead of just using regular Java EE, and I think the reason is I don't have a good grasp of some of benefits of the design principles that Spring employs (dependency injection, etc.).
For example I don't understand what benefits we get by adding #Bean, #Component #Autowired and #Repository annotations.
My question is not what do those annotations do, the question is more what are the principles that Spring makes the implementation of easier or more effective?
Dependency injection is a fantastic pattern. It often allows you to easily unit test components that would otherwise require complicated mocking or stubbing.
It's worth stating that it's possible to use dependency injection without handing the object lifecycle and "wiring" over to a container. I always use dependency injection. However I would only consider using a dependency injection framework for a large project. For instance, last year I wrote a ~2k line webserver in Java and chose not to use "managed" dependency injection. My team agreed that this arrangement provided all the architectural benefits of dependency injection without the drawbacks of a DI framework (bloat, boilerplate, reflection, etc). However if it was 200k lines I may have made a different choice.
Since you're asking about the benefits of Spring and not DI/IoC in general, I have to say that I think Spring is poorly designed. The history of Spring goes hand in hand with the history of antipatterns and bad abstractions that have plagued the Enterprise Java community. To be fair, engineers that I respect have told me that Spring Boot is better. Certainly try it out, you will gain perspective on what does or doesn't work. But maybe also consider other options that adhere to the UNIX philosophy by focusing just on dependency injection.
Spring makes it easy to create Java enterprise applications. It
provides everything you need to embrace the Java language in an
enterprise environment, with support for Groovy and Kotlin as
alternative languages on the JVM, and with the flexibility to create
many kinds of architectures depending on an application’s needs. As of
Spring Framework 5.0, Spring requires JDK 8+ (Java SE 8+) and provides
out-of-the-box support for JDK 9 already.
Read the manual:
https://docs.spring.io/spring/docs/5.1.4.BUILD-SNAPSHOT/spring-framework-reference/
There are tons of useful information.
My question is not what do those annotations do, the question is more
what are the principles that Spring makes the implementation of easier
or more effective?
IoC container, many projects and integrations, Spring Boot with the embedded servlet container. Many built-in features.
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 5 years ago.
Improve this question
So I am having my rest Api calls using spring boot.
I want to know how spring boot loads all the configuration step by step.
like what would be the process while loading configuration,what and how the order are getting followed. Spring security,db configuration and bean initialization in term of all How these are getting loaded.
And how the spring boot find the priority (order) ,which should be loaded first and which should be next.
Beans are loaded based on the autoconfiguration mechanism in Spring Boot, there is a spring.factories file in the META-INF folder of the jar file containing the the fully qualified name of the Configuration classes to load.
When Spring Boot finds a file like that, it will load the configuration as a bean, and the configuration usually loads other beans.
Other way configuration could be loaded, by just simply #Importing them, some work like that, example #EnableMetrics.
Jar files containing this autoconfiguration mechanism usually called starters.
There are some special beans as well that will be picked up by Spring, example FilterDefinitionBean that will be turned into a filter, or CommandLineRunner, that will be executed after startup.
Usually you do not need to worry too much about the order of the starters, since Spring Boot automatically detects the correct order to initialize these based on the dependencies, beans with #DependsOn annotations.
Spring Boot starter autoconfiguraions happen after your beans are already defined, so they can give you fallback beans, but only if you have not defined them.
But sometimes, especially when you have #ConditionalOnBean you have to explicitly define the order, this can be done by #AutoConfigureBefore and #AutoConfigureAfter annotations to define where this autoconfiguration has to be initialized.
Another annotation #AutoConfigureOrder is the Spring Boot equivalent of the #Order annotation could also be used to change the order (since #Order does not work for autoconfigurations).
If you check your favourite starter's source code, you will see these order changing annotations are often added to the class.
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
What is the Difference between Spring IOC and Spring AOP and their Importance ?
Have you searched the web for IoC and AOP? There are a lot of references to both.
In a nutshell, IoC allows an external force to determine what implementation will be used by code rather than the code determining the implementation. The "external force" might be a configuration file, a unit test, other different code, etc.
AOP allows cross-cutting concerns to be implemented outside of the code affected by those concerns.
The "purpose" of Spring includes IoC and AOP, but goes quite a ways beyond that in its scope.
For more details please check.
Inversion of Control Containers and the Dependency Injection pattern and
Aspect-oriented programming
Also check this
What is AOP, Dependency Injection and Inversion Of Control in Simple English
IoC, AOP and more
Spring IOC: In simple answer normally you create object with new operator and set yourself for getter and setter. So, yes we use new operator in Java to create object. There is no any bad in doing this. But, when your project size grows and lots of developers are working, and you want to achieve POJO-based programming, you can use DI. So then maybe your question arises - why I can not code it myself? Of course you can use the power of reflection, annotation, and XML. But, some other had already coded this then why not reuse the third party one? There are lots of options for you to choose; Spring can be the best one. It manages your object life cycle from object creation to its destruction. You use the objects created and set by Spring DI container but you do not create them yourself.
Spring AOP: It is related to cross cutting concern. What it mean is in large system the common functionality is scattered throughout different modules. So AOP provides an easiest way to take out a common implementation in the form of 'aspect'. You can also in this case write own implementation using proxy concept but you can reuse the code of proxy based that is implementation of APO alliance using Spring.
Objective of Spring IOC is to reduce explicit dependencies between components, while purpose of Spring AOP is to wire components together possibly by enforcing certain common behavior (read: NOT Interface)
Since purpose of Spring AOP is to enforce certain behavior across components.So, Spring IOC comes in handy to achieve this purpose