Spring Core Overview
Spring Core is the fundamental part of the Spring Framework. It provides the basic features and infrastructure needed for developing Java applications. This overview covers the key components and concepts of Spring Core, including dependency injection, inversion of control, and bean lifecycle management.
Key Concepts of Spring Core
- Dependency Injection (DI): A design pattern that allows objects to be injected into a class, rather than being created by the class itself.
- Inversion of Control (IoC): A principle where the control of object creation and lifecycle is transferred from the application to the IoC container.
- Bean: An object that is instantiated, assembled, and managed by the Spring IoC container.
- ApplicationContext: The central interface for providing configuration information to the Spring IoC container.
- BeanFactory: The root interface for accessing the Spring IoC container.
Setting Up Spring Core
To set up Spring Core, you need to add the necessary dependencies to your project and configure the Spring IoC container. Here is a basic setup:
pom.xml Configuration
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>
</dependencies>
Main Application Class
// SpringCoreApplication.java
package com.example.springcore;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class SpringCoreApplication {
public static void main(String[] args) {
ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
MyService myService = context.getBean(MyService.class);
myService.performTask();
}
}
Configuration Class
// AppConfig.java
package com.example.springcore;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan(basePackages = "com.example.springcore")
public class AppConfig {
@Bean
public MyService myService() {
return new MyService();
}
}
Defining Beans
Beans can be defined using annotations or XML configuration. Here is an example using annotations:
// MyService.java
package com.example.springcore;
import org.springframework.stereotype.Service;
@Service
public class MyService {
public void performTask() {
System.out.println("Performing a task in MyService.");
}
}
Dependency Injection
Spring Core supports different types of dependency injection: constructor injection, setter injection, and field injection. Here is an example of constructor injection:
// MyController.java
package com.example.springcore;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
@Controller
public class MyController {
private final MyService myService;
@Autowired
public MyController(MyService myService) {
this.myService = myService;
}
public void execute() {
myService.performTask();
}
}
Bean Lifecycle
Spring manages the lifecycle of beans, providing hooks for initialization and destruction callbacks. Here is an example using @PostConstruct and @PreDestroy annotations:
// MyService.java
package com.example.springcore;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import org.springframework.stereotype.Service;
@Service
public class MyService {
@PostConstruct
public void init() {
System.out.println("Initializing MyService.");
}
@PreDestroy
public void destroy() {
System.out.println("Destroying MyService.");
}
public void performTask() {
System.out.println("Performing a task in MyService.");
}
}
Application Contexts
The ApplicationContext is the central interface to the Spring IoC container. It is responsible for instantiating, configuring, and assembling the beans. Here is an example of using AnnotationConfigApplicationContext:
// SpringCoreApplication.java
package com.example.springcore;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class SpringCoreApplication {
public static void main(String[] args) {
ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
MyService myService = context.getBean(MyService.class);
myService.performTask();
}
}
Key Points
- Spring Core is the fundamental part of the Spring Framework, providing the basic features and infrastructure for developing Java applications.
- Key concepts include dependency injection (DI), inversion of control (IoC), beans, ApplicationContext, and BeanFactory.
- Dependency injection allows objects to be injected into a class, rather than being created by the class itself.
- The ApplicationContext is the central interface for providing configuration information to the Spring IoC container.
- Spring manages the lifecycle of beans, providing hooks for initialization and destruction callbacks.
Conclusion
Spring Core provides a powerful and flexible foundation for building Java applications. By leveraging its features and following best practices, developers can create robust, maintainable, and scalable applications. Happy coding!