This story will explain the spring container and how its works
org.springframework.context packages are the basis for Spring Framework’s IoC container. The BeanFactory the interface provides an advanced configuration mechanism capable of managing any type of object. ApplicationContext is a sub-interface of
org.springframework.context.ApplicationContext the interface represents the Spring IoC container.
Instantiating a Container
The location path or paths supplied to an
ApplicationContext constructor are resource strings that let the container load configuration metadata from a variety of external resources, such as the local file system, the Java
CLASSPATH, and so on.
ApplicationContext context = new ClassPathXmlApplicationContext("services.xml", "daos.xml");
The Spring IoC container consumes a form of configuration metadata. This configuration metadata represents how you, as an application developer, tell the Spring container to instantiate, configure, and assemble the objects in your application.
- Annotation-based configuration: Spring 2.5 introduced support for annotation-based configuration metadata.
- Java-based configuration: Starting with Spring 3.0, many features provided by the Spring JavaConfig project became part of the core Spring Framework. Thus, you can define beans external to your application classes by using Java rather than XML files. To use these new features, see the
A Spring IoC container manages one or more beans. These beans are created with the configuration metadata that you supply to the container (for example, in the form of XML
How do you find the Spring context?
To get a reference to the ApplicationContext in a Spring application, it can easily be achieved by implementing the ApplicationContextAware interface. Spring will automatically detect this interface and inject a reference to the ApplicationContext.
What is @ContextConfiguration?
@ContextConfiguration defines class-level metadata that is used to determine how to load and configure an ApplicationContext for integration tests.
What is difference between IoC and DI in spring?
Inversion of control is a design principle that helps to invert the control of object creation. Dependency Injection is a design pattern that implements the IOC principle. … DI provides objects that an object needs.
@Configuration & @Bean Annotations
Annotating a class with the @Configuration indicates that the class can be used by the Spring IoC container as a source of bean definitions.
The @Bean annotation tells Spring that a method annotated with @Bean will return an object that should be registered as a bean in the Spring application context