Comprehensive application framework and inversion of control container for the Java platform providing dependency injection, AOP, data access, transaction management, and web framework capabilities
npx @tessl/cli install tessl/maven-org-springframework--spring@5.3.0The Spring Framework is a comprehensive application framework and inversion of control container for the Java platform. It provides a complete programming and configuration model for modern Java-based enterprise applications, with extensive infrastructure support for developing Java applications.
Maven Coordinates:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.39</version>
</dependency>Gradle:
implementation("org.springframework:spring-context:5.3.39")Java Version: Requires Java 8 or higher
Key Dependencies:
// Core Application Context
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
// Configuration and Component Scanning
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Bean;
// Dependency Injection Annotations
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.stereotype.Repository;
// Value Injection and Profiles
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Profile;import org.springframework.core.io.Resource;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.env.Environment;
import org.springframework.context.annotation.PropertySource;@Configuration
@ComponentScan(basePackages = "com.example")
public class AppConfig {
@Bean
public DataService dataService() {
return new DataServiceImpl();
}
}
@Component
public class BusinessService {
@Autowired
private DataService dataService;
public String processData(String input) {
return dataService.transform(input);
}
}
// Application startup
public class Application {
public static void main(String[] args) {
ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
BusinessService service = context.getBean(BusinessService.class);
String result = service.processData("hello");
System.out.println(result);
}
}@Configuration
@PropertySource("classpath:application.properties")
public class DatabaseConfig {
@Value("${db.url}")
private String dbUrl;
@Value("${db.username}")
private String dbUsername;
@Bean
public DataSource dataSource() {
BasicDataSource dataSource = new BasicDataSource();
dataSource.setUrl(dbUrl);
dataSource.setUsername(dbUsername);
return dataSource;
}
}Spring Framework is built on several foundational layers:
// Container interfaces
public interface BeanFactory {
Object getBean(String name) throws BeansException;
<T> T getBean(String name, Class<T> requiredType) throws BeansException;
<T> T getBean(Class<T> requiredType) throws BeansException;
boolean containsBean(String name);
boolean isSingleton(String name) throws NoSuchBeanDefinitionException;
}
public interface ApplicationContext extends BeanFactory, MessageSource,
ApplicationEventPublisher, ResourceLoader {
String getId();
String getApplicationName();
String getDisplayName();
long getStartupDate();
ApplicationContext getParent();
AutowireCapableBeanFactory getAutowireCapableBeanFactory() throws IllegalStateException;
}The Spring IoC container manages object creation, dependency injection, and lifecycle. It supports various injection methods including constructor injection, setter injection, and field injection.
Key APIs:
// Bean Definition and Configuration
public interface BeanDefinition extends AttributeAccessor, BeanMetadataElement {
String getBeanClassName();
void setBeanClassName(String beanClassName);
String getScope();
void setScope(String scope);
boolean isLazyInit();
String[] getDependsOn();
}
// Property Access and Type Conversion
public interface BeanWrapper extends ConfigurablePropertyAccessor {
Object getWrappedInstance();
Class<?> getWrappedClass();
PropertyDescriptor[] getPropertyDescriptors();
PropertyDescriptor getPropertyDescriptor(String propertyName)
throws InvalidPropertyException;
}For comprehensive details: Core Container & Dependency Injection
Spring AOP provides aspect-oriented programming implementation allowing you to define method interceptors and pointcuts to cleanly decouple functionality.
Key APIs:
// Core AOP interfaces
public interface MethodInterceptor extends Interceptor {
Object invoke(MethodInvocation invocation) throws Throwable;
}
public interface Pointcut {
ClassFilter getClassFilter();
MethodMatcher getMethodMatcher();
}
// Proxy creation
public class ProxyFactory extends AdvisedSupport {
public Object getProxy();
public Object getProxy(ClassLoader classLoader);
public static <T> T getProxy(Class<T> proxyInterface, Interceptor interceptor);
}For comprehensive details: Aspect-Oriented Programming
Spring provides abstraction over JDBC, transaction management, and integration with ORM frameworks like Hibernate and JPA.
Key APIs:
// JDBC Template
public class JdbcTemplate extends JdbcAccessor implements JdbcOperations {
public <T> T queryForObject(String sql, Class<T> requiredType, Object... args);
public <T> List<T> query(String sql, RowMapper<T> rowMapper, Object... args);
public int update(String sql, Object... args);
}
// Transaction Management
@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
public @interface Transactional {
Propagation propagation() default Propagation.REQUIRED;
Isolation isolation() default Isolation.DEFAULT;
int timeout() default TransactionDefinition.TIMEOUT_DEFAULT;
boolean readOnly() default false;
}For comprehensive details: Data Access & Transaction Management
Spring MVC provides Model-View-Controller architecture for building web applications with flexible request handling and view resolution.
Key APIs:
// Controller annotations
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface Controller {
String value() default "";
}
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Mapping
public @interface RequestMapping {
String[] value() default {};
RequestMethod[] method() default {};
String[] params() default {};
String[] headers() default {};
}For comprehensive details: Web Framework (Spring MVC)
Spring WebFlux provides reactive programming model for building non-blocking web applications using Reactor and reactive streams.
Key APIs:
// Functional routing
public interface RouterFunction<T extends ServerResponse> {
Mono<HandlerFunction<T>> route(ServerRequest request);
RouterFunction<T> and(RouterFunction<T> other);
RouterFunction<T> andRoute(RequestPredicate predicate, HandlerFunction<T> handlerFunction);
}
// Reactive request/response
public interface ServerRequest {
Mono<String> bodyToMono(Class<String> elementClass);
<T> Flux<T> bodyToFlux(Class<T> elementClass);
MultiValueMap<String, String> queryParams();
}For comprehensive details: Reactive Web Framework (WebFlux)
Spring Test provides comprehensive testing utilities including TestContext framework, MockMvc for web layer testing, and transaction rollback support.
Key APIs:
// Test annotations
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@BootstrapWith(SpringBootTestContextBootstrapper.class)
public @interface SpringBootTest {
Class<?>[] classes() default {};
String[] properties() default {};
WebEnvironment webEnvironment() default WebEnvironment.MOCK;
}
// MockMvc for web testing
public final class MockMvc {
public ResultActions perform(RequestBuilder requestBuilder) throws Exception;
}For comprehensive details: Testing Support
Spring provides abstractions for message-driven applications including support for JMS, AMQP, and custom messaging solutions.
Key APIs:
// Message abstraction
public interface Message<T> {
T getPayload();
MessageHeaders getHeaders();
}
// JMS Template
public class JmsTemplate extends JmsDestinationAccessor implements JmsOperations {
public void send(String destinationName, MessageCreator messageCreator) throws JmsException;
public Object receiveAndConvert(String destinationName) throws JmsException;
}For comprehensive details: Messaging & JMS
Additional Spring modules provide integration with various enterprise technologies and cross-cutting concerns.
Key Areas:
For comprehensive details: Integration & Support
// Java-based configuration
@Configuration
@EnableTransactionManagement
@ComponentScan(basePackages = {"com.example.service", "com.example.repository"})
public class AppConfig {
@Bean
@Profile("development")
public DataSource devDataSource() {
return new EmbeddedDatabaseBuilder()
.setType(EmbeddedDatabaseType.H2)
.build();
}
@Bean
@Profile("production")
public DataSource prodDataSource() {
HikariDataSource dataSource = new HikariDataSource();
dataSource.setJdbcUrl("jdbc:postgresql://localhost/mydb");
return dataSource;
}
}// Constructor injection (recommended)
@Service
public class OrderService {
private final PaymentService paymentService;
private final EmailService emailService;
public OrderService(PaymentService paymentService, EmailService emailService) {
this.paymentService = paymentService;
this.emailService = emailService;
}
}
// Qualifier for multiple implementations
@Service
public class NotificationService {
@Autowired
@Qualifier("emailNotifier")
private Notifier emailNotifier;
@Autowired
@Qualifier("smsNotifier")
private Notifier smsNotifier;
}// Resource loading
@Service
public class ConfigurationService {
@Autowired
private ResourceLoader resourceLoader;
public Properties loadConfiguration(String configPath) {
try {
Resource resource = resourceLoader.getResource("classpath:" + configPath);
Properties props = new Properties();
props.load(resource.getInputStream());
return props;
} catch (IOException e) {
throw new ConfigurationException("Failed to load configuration", e);
}
}
}Understanding Spring's modular architecture helps in selecting the right dependencies:
For a complete Spring application, typically you'll need spring-context (which includes core and beans) plus specific modules for your use case (web, data access, etc.).