Knowing IoC containers in general
IoC container is generally used for dependency injection, initializing objects in the memory, hold objects in the memory. so it has 3 works to do: create objects, hold them in memory and inject them into another object as required. Till the object is in the memory for the whole life cycle it is maintained by the containers.
Spring IoC container generally takes two values to perform the above actions:
or we can say Spring IoC container is made up of two things:
- Beans (POJO– a plain old Java object is an ordinary Java object, not bound by any special restriction)
- Configuration file bean factory are the ready objects for injection and working configuration xml file tells the function and working of these bean factories inside the spring containers , its more like inside a kitchen, the raw food material which is ready to be cooked by the chefs and served to customers ././ the ready raw food material is your bean factory
chef-> configurationXML file and customer is your application code.
Types of IoC containers
Now, the Spring IoC container that we use daily is your ApplicationContext, earlier it was BeansFactor but it has some incomplete functions, so application context was made with extra features, applications and beans included in it. It is basically an interface.
ApplicationContext implements beanFactory{}
well, ApplicationContext is an interface class so, it cannot directly have its objects. So it has different classes for object creation, that is:
- ClasspathXMLApplicationContext (searches for XML configration in java class path)
- AnnotationConfigApplicationCongfig (searches for Beans which used annotations)
- FileSystemXMLApplicationContext (searches for XML files in the file system)
Ways for injecting Dependencies
There are ways of injecting dependencies:
- setter injection
- constructor injection
Obvious from the names that how it is going to work in the setter, every member variable of the class will be injected by IoC container using set methods/functions. In the constructor, it will be done by the constructor of the class.
Now, these all procedures are written in a class called beans which are then specified under the configuration file, which tells the Intelligent boy IoC container to create objects and inject by himself.
Types of datas it can take:
primitive: byte,boolean,short ,char,long, double, float
collections: lists,map,get.hashmaps
non primitive/reference/user-defined: class-objects
If you have any doubts you can ask the questions directly by commenting down below. Also, don’t forget to read our 5th article on the OOPs and first article of Spring Framework to understand it completely:
Software Dev Phase 5: Introduction to Spring Framework
Get the latest tech news and updates, ethical hacking tutorials and cybersecurity tips and tricks. Check out MeuSec for more.
Sometimes we include links to online retail stores and/or online campaigns. If you click on one and make a purchase we may receive a small commission.
Comments: