IOC Spring Container Spring Beans - AyushiDamele/Spring GitHub Wiki

Inversion of Control :

The approach of outsourcing the construction and management of objects

Spring Container :

Primary functions

  1. Create and Manage objects(Inversion of Control)
  2. Inject object's dependencies(dependency injection)

Configuring Spring Container

  1. XML configuration file(legacy, but most legacy apps still use this)
  2. Java Annotations(modern)
  3. Java Source Code(modern)

What is a Spring Bean?

A "Spring Bean" is simply a Java object.
When Java objects are created by the Spring Container, then Spring refers to them as "Spring Beans".

Spring Beans are created from normal Java classes .... just like Java objects.

Spring Development Process

  1. Configure your spring beans
  2. Create a spring Container
  3. Retrieve Beans from spring container

Configure your spring beans : applicationContext.xml

<bean id="myCoach"
	class="com.ayushi.spring.BaseballCoach"
</bean>

Spring Container is generically known as ApplicationContext Specialized implementations are :

  1. ClassPathXmlApplicationContext
  2. AnnotationConfigApplicationContext
  3. GenericWebApplicationContext
  4. others...

Using ClassPathXmlApplicationContext:

ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");

Retrieve bean from spring container :

Coach theCoach = context.getBean("myCoach", Coach.class);

Syntax:

context.getBean(beanId, interfaceName that BaseballCoach class implements)
⚠️ **GitHub.com Fallback** ⚠️