Unlock the Power of Spring: 30 Core Concepts Every Java Developer Must Master
This comprehensive guide explores the fundamentals of the Spring framework—including its core modules, dependency injection, IoC, bean scopes, configuration styles, annotation-driven setup, event handling, and common design patterns—providing Java developers with practical examples and code snippets to master Spring's capabilities.
1. What is the Spring framework and its main modules
Spring is a comprehensive Java platform that provides extensive foundational support for Java application development, helping developers focus on business logic by handling core infrastructure concerns. It follows well‑designed patterns, allowing seamless integration without worrying about internal workings.
2. Benefits of using Spring
Key advantages include:
Dependency Injection (DI) makes dependencies clear in constructors and bean property files.
IoC containers are lightweight compared to EJB containers, suitable for limited memory and CPU resources.
Spring leverages existing technologies such as ORM, logging, J2EE, Quartz, and JDK Timer.
Modular organization lets you pick only the needed modules.
Testing is straightforward because test utilities are bundled, and POJOs can be injected with test data.
Spring MVC offers an alternative to mainstream or overly complex web frameworks.
Convenient transaction management supports both local and distributed transactions.
3. What are Inversion of Control (IoC) and Dependency Injection
IoC is a design technique where object relationships are assembled at runtime by a container rather than being hard‑coded. Dependency Injection is the mechanism that performs this assembly, allowing the container to instantiate and wire components.
4. Ways to perform Dependency Injection in Java
Constructor injection.
Setter method injection.
Interface injection.
5. Difference between BeanFactory and ApplicationContext
BeanFactory is a factory that holds bean definitions and creates bean instances on demand, managing lifecycle callbacks. ApplicationContext extends BeanFactory with additional features such as internationalization, unified resource loading, and event propagation. Common implementations include ClassPathXmlApplicationContext, FileSystemXmlApplicationContext, and XmlWebApplicationContext.
ApplicationContext context = new ClassPathXmlApplicationContext("application.xml");6. Configuration methods for metadata
Spring supports three metadata configuration styles:
XML‑based configuration.
Annotation‑based configuration.
Java‑based configuration.
7. Using XML configuration for Spring
Beans and their dependencies are defined in XML using Spring’s namespaces (context, beans, jdbc, tx, aop, mvc, etc.). Example:
<beans>
<bean id="viewResolver" class="org.springframework.web.servlet.view.BeanNameViewResolver"/>
</beans>A minimal web.xml can configure only the DispatcherServlet to bootstrap the application.
8. Annotation‑based configuration
Since Spring 2.5, annotations can replace XML bean definitions. Enable annotation processing with <context:annotation-config/> or by registering AutowiredAnnotationBeanPostProcessor. Common annotations include @Required, @Autowired, @Qualifier, and JSR‑250 annotations ( @Resource, @PostConstruct, @PreDestroy).
9. Spring Bean lifecycle
Bean initialization and destruction callbacks are managed by the BeanFactory. Lifecycle hooks include implementing InitializingBean and DisposableBean, using custom init/destroy methods, or annotating with @PostConstruct and @PreDestroy.
<bean id="demoBean" class="com.gupaoedu.task.DemoBean" init-method="customInit" destroy-method="customDestroy"/>10. Bean scopes
Spring defines five scopes:
singleton – one instance per container (default).
prototype – a new instance for each request.
request – one instance per HTTP request.
session – one instance per HTTP session.
global-session – for Portlet applications.
11. Inner beans
When a bean is used only by another bean, it can be defined as an inner bean within the property element, reducing external visibility.
<bean id="CustomerBean" class="com.gupaoedu.common.Customer">
<property name="person">
<bean class="com.gupaoedu.common.Person">
<property name="name" value="lokesh"/>
<property name="address" value="India"/>
<property name="age" value="34"/>
</bean>
</property>
</bean>12. Thread safety of singleton beans
Spring does not provide built‑in thread safety for singleton beans. Developers must ensure thread safety for mutable state, often by using prototype scope for stateful beans.
13. Injecting Java collections
Spring can inject List, Set, Map, and Properties using <list>, <set>, <map>, and <props> elements respectively.
<property name="customList">
<list>
<value>INDIA</value>
<value>Pakistan</value>
<value>USA</value>
<value>UK</value>
</list>
</property>14. Using @Qualifier
@Qualifierdisambiguates autowiring when multiple beans of the same type exist.
15. Constructor vs. setter injection
Constructor injection guarantees fully initialized objects and avoids circular dependencies, while setter injection offers flexibility but may leave beans partially configured.
16. Spring events
ApplicationContext publishes events such as ContextRefreshedEvent, ContextStartedEvent, ContextStoppedEvent, ContextClosedEvent, and RequestHandledEvent. Custom events can be created by extending ApplicationEvent and listeners implement ApplicationListener.
public class CustomApplicationEvent extends ApplicationEvent {
public CustomApplicationEvent(Object source, String msg) {
super(source);
System.out.println("Created a Custom event");
}
}17. FileSystemResource vs. ClassPathResource
FileSystemResourceloads resources using an absolute or relative file system path, while ClassPathResource searches the classpath, allowing simple names when the file resides in src or other classpath locations.
18. Design patterns used in Spring
Spring employs many patterns, including Proxy (AOP), Singleton (default bean scope), Template (JdbcTemplate, RestTemplate), Delegation (DispatcherServlet), Factory (BeanFactory, ApplicationContext), and others.
19. Effective JDBC usage
Spring’s JdbcTemplate simplifies resource management and error handling, allowing developers to focus on SQL statements.
20. The IoC container
The core of Spring’s IoC container consists of the org.springframework.beans and org.springframework.context packages. BeanFactory provides basic bean creation, while ApplicationContext adds internationalization, event propagation, and web integration.
21. Injecting null or empty strings
Spring permits injection of null values or empty strings into bean properties.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Programmer DD
A tinkering programmer and author of "Spring Cloud Microservices in Action"
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
