Spring Framework implements the Inversion of Control (IoC) principle. It’s also known as Dependency Injection (DI). It’s a process to define your beans and their injected dependencies.
org.springframework.context packages are the basis for Spring Framework’s IoC container. The
BeanFactory is the lowest implementation of the IoC container; but it is recommended to use an
ApplicationContext for your application. The
ApplicationContext is a subclass of the
BeanFactory interface so it has all the functionality a
BeanFactory has and more.
There’s a couple of ways to implement ApplicationContext. We can use ClassPathXmlApplicationContext which is a very popular one.
ApplicationContext context = new ClassPathXmlApplicationContext("/info/java/tips/config/spring-bean.xml");
Or annotation type
AnnotationConfigApplicationContext context= new AnnotationConfigApplicationContext();
Dependency Injection exists in two major variants:
- Constructor-based dependency injection
- Setter-based dependency injection.
Spring beans in IoC container can be any plain old java object (POJO). To create spring bean; you simply:
- Create a POJO
- Declare a bean element in a Spring Bean Configuration File or annotate the POJO
Navigate to the next chapter for step by step examples.