Spring Framework Introduction: Core Concepts & Dependency Injection
Dependency Injection
- Decoupling the creation of object
- ability to replace dependencies without changing the class that uses it
- Promotes "Code to interface not to implementation" principle
- Ability to create and use mock dependency during test
Bean Factory
- Bean instantiation/wiring
Application Context
- Bean instantiation/wiring
- Automatic BeanPostProcessor registration
- Automatic BeanFactoryPostProcessor registration
- Convenient MessageSource access (for i18n)
- ApplicationEvent publication
- BeanFactory uses lazy initialization but ApplicationContext uses eager initialization. In BeanFactory, bean is created when getBeans() method is called, but bean is created upfront in case of ApplicationContext when the ApplicationContext object is created.
- BeanFactory does not support internationalization but ApplicationContext supports internationalization.
- With BeanFactory annotation based dependency injection is not supported but annotation based dependency injection is supported in ApplicationContext.
Spring Modules

- Core and Beans modules provide Dependency Injection features
- Context module provides way to access objects
- Expression Language module provides expression language for querying and manipulating an object graph at runtime
- JDBC module provides abstraction layer that removes the need to do tedious JDBC coding and parsing of database-vendor specific error codes.
- ORM module provides integration layers for popular object-relational mapping APIs, including JPA, JDO, Hibernate, and iBatis.
- OXM module provides an abstraction layer for using a number of Object/XML mapping implementations. Supported technologies include JAXB, Castor, XMLBeans, JiBX and XStream.
- JMS module provides support for the Java Messaging Service. It contains features for both producing and consuming messages.
- Transaction module provides a way to do programmatic as well as declarative transaction management, not only for classes implementing special interfaces, but for all your POJOs (plain old Java objects).
- Web module provides basic web-oriented integration features, such as multipart file-upload functionality, the initialization of the IoC container using servlet listeners and a web-oriented application context.
- Web-Servlet module provides Model-View-Controller (MVC) implementation for web-applications.
- Web-Portlet module provides the MVC implementation to be used in a portlet environment and mirrors what is provided in the Web-Servlet module.
Dependency Injection using XML Configuration
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="dependencyA" class="com.demo.DependencyA" />
<bean id="dependencyB" class="com.demo.DependencyB" />
<bean id="consumerA" class="com.demo.ConsumerA">
<constructor-arg ref="dependencyA" />
</bean>
<bean id="consumerB" class="com.demo.ConsumerB">
<property name="paramA" ref="dependencyB" />
</bean>
</beans>
package com.demo.service;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class SpringDependencyExample {
public static void main(String[] args) {
ApplicationContext context
= new ClassPathXmlApplicationContext("xmlFileA.xml");
ConsumerA consumer = (ConsumerA) context.getBean("consumerA");
consumer.process();
}
}
Dependency Injection using XML Configuration File and Annotations
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="com.dependencies, com.consumers" />
</beans>
package com.demo.service;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class SpringDependencyExample {
public static void main(String[] args) {
ApplicationContext context
= new ClassPathXmlApplicationContext("xmlFileA.xml");
ConsumerA consumer = (ConsumerA) context.getBean("consumerA");
consumer.process();
}
}
Dependency Injection Using Java Based Configuration
Step2: Add spring-context dependency in the the pom file.
package com.example.demo;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class ConfigClass {
@Bean
public ConsumerA getConsumerA(){
DependencyA dependencyA = new DependencyA();
return new ConsumerA(dependencyA);
}
}
package com.example.demo.service;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class SpringDependencyExample {
public static void main(String[] args) {
AnnotationConfigApplicationContext context
= new AnnotationConfigApplicationContext(ConfigClass.class);
ConsumerA consumer = (ConsumerA) context.getBean("consumerA");
consumer.process();
}
}
package com.example.demo;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;@Configuration
@ComponentScan(basePackages = "com.dependencies, com.consumers")
public class ConfigClass {
}
FAQ Section
1. What is Spring Framework used for?
Spring is used for building enterprise Java applications with features like dependency injection, transaction management, web MVC, and more.
2. What is Dependency Injection in Spring?
It’s the process of supplying a class’s dependencies externally instead of creating them internally, promoting loose coupling.
3. What is the difference between BeanFactory and ApplicationContext?
BeanFactory is basic and lazy-loading; ApplicationContext is more feature-rich and eagerly loads beans.
4. Can I use annotations instead of XML in Spring?
Yes. Spring supports annotation-based configuration using @Component
, @Autowired
, and @Configuration
.
5. How does Java-based configuration differ from XML?
Java-based configuration uses @Configuration
classes and @Bean
methods, providing type safety and IDE support.
6. What is a Spring module?
A Spring module is a logical component (like JDBC, AOP, Web, etc.) that provides a specific set of features.
7. Is Spring only for web applications?
No. Spring is suitable for all types of Java applications, including standalone apps, microservices, and web systems.
Leave a Comment