Spring Inversion of Control (IoC) also known as
Dependency Injection (DI) is a process by which
objects define their dependencies with collaborating objects.
The attribute init-method allows us to specify a no-argument method as
an initialization callback method. Instead of specifying bean level initialization callbacks
Spring provides a way to define it for all beans defined in bean configuration file using
default-init-method attribute.
System.out.println("Initializing ApplicationContext");
ApplicationContext factory = new ClassPathXmlApplicationContext("default_init.xml");
System.out.println("ApplicationContext Initialized");
System.out.println("Accessing first bean");
SimpleInitBean2 first = (SimpleInitBean2) factory.getBean("first");
System.out.println(first);
System.out.println("Accessing second bean");
SimpleInitBean2 second = (SimpleInitBean2) factory.getBean("second");
System.out.println(second);
}
}
It gives the following output,
Initializing ApplicationContext
ApplicationContext Initialized
Accessing first bean
In custom init callback : FIRST
In custom init callback : FIRST@callback
SimpleInitBean2[FIRST@callback]
Accessing second bean
In custom init callback : SECOND
In custom init callback : SECOND@callback
SimpleInitBean2[SECOND@callback]