Brief introduction about Spring IoC and scope of the spring bean
Posted By : Nanda Ram | 16-Dec-2018
Spring IoC Container
Spring Inversion of Control (IoC) is the mechanism to get loose-coupling between Objects dependencies. to get a loose coupling and dynamic binding of the object at runtime, object dependency is injected by another assembler object. Spring Inversion of Control (IoC) container is the program that injects dependency into an associated object and makes it ready for our use.
Spring Inversion of Control (IoC) container class is part of org.springframework.beans, and org.springframework.context package. Spring Inversion of Control (IoC) container provide us with alternative ways to decouple the object dependency.
BeanFactory is the root interface of Spring Inversion of Control (IoC) container. ApplicationContext is the child interface of BeanFactory interface that has Spring Aspect Oriented Programming (AOP) option.
Some of the useful child-interface of ApplicationContext are ConfigurableApplicationContext and WebApplicationContext. Spring Framework provides a variety of useful ApplicationContext implementation class that we are able to use to get the spring context and the Spring Bean.
Some of the useful ApplicationContext implementation that we have to use:
AnnotationConfigApplicationContext: If we are using Spring in standalone Java application and using annotation for Configuration, then we are able to use this to initialize the container and find the bean object.
ClassPathXmlApplicationContext: If we've got spring bean configuration in XML file standalone application, then we are able to use this class to loads the file and find the container objects.
FileSystemXmlApplicationContext: This is almost like ClassPathXmlApplicationContext except that the XML configuration file can be loaded from anyplace within the file systems.
AnnotationConfigWebApplicationContext and XmlWebApplicationContext for net application.
Usually, if you're working on Spring MVC application and your application is designed to use Spring Framework, Spring Inversion of Control (IoC) container gets initialized once the appliance starts and once a bean is requested, the dependencies injected automatically.
However, for a standalone application, you wish to initialize the container somewhere within the application and so use it to get the spring beans.
There are 5 scopes defined for Spring Beans.
singleton – Just one instance of the bean is going to be created for every container. singleton scope is the default scope for the spring beans. whereas using this scope, to check that bean doesn’t have shared instance variables otherwise it would cause information data problems.
prototype – A new instance is going to be created every time the bean is requested.
request – This can be the same as the prototype scope, but it’s meant to be used for web application. a new instance of the bean is going to be created for every HTTP request.
session – A new bean is going to be created for every HTTP session by the container.
global-session – This can be accustomed to produce global session beans for a Portlet application.
Spring Framework is extendable and that we will produce our own scopes too. However, most of the times we have a good with the scopes provided by the framework.
Spring Bean Configuration
Spring Framework provides 3 ways to set up beans to be used in the application.
Annotation-based Configuration – By using @Service or @Component annotations. Scope details may be given @Scope annotation.
XML based Configuration – By making Spring Configuration XML file to set up the beans. If you're using Spring MVC framework, then XML based configuration may be loaded automatically by writing some boilerplate code in a web.xml file.
Java based Configuration – Starting from Spring 3.0, we will set up Spring beans using java programs. Some necessary annotations used for java based configuration are @Configuration, @ComponentScan and @Bean.
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Nanda Ram
Nanda Ram is a Java Developer, he keep conscientious about his task to complete it in a effective and efficient manner .