Skip to content

Commit 9612f16

Browse files
committed
🚀 feat: Add comprehensive dependency injection system (v0.2)
- Spring-like DI with @component, @nAmed, @primary annotations - Zero-configuration automatic component discovery - Constructor injection with dependency resolution - Lifecycle management (@PostConstruct, @PreDestroy) - Scope management (@singleton, @prototype) - Comprehensive documentation and examples - 137 tests covering all DI features - Full backward compatibility with 0.1
1 parent ecff272 commit 9612f16

28 files changed

+4403
-124
lines changed

README.md

Lines changed: 141 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,56 @@
11
# Jazzy Framework
22

3-
Jazzy is a lightweight web framework for Java. It provides a minimal and easy-to-understand API for developing fast web applications with a structure inspired by Laravel.
3+
Jazzy is a lightweight web framework for Java. It provides a minimal and easy-to-understand API for developing fast web applications with a structure inspired by Laravel and Spring Boot.
4+
5+
## 🚀 Latest Updates (v0.2.0)
6+
7+
**NEW: Enterprise-Level Dependency Injection System!**
8+
9+
Jazzy Framework 0.2 introduces a comprehensive Spring-like dependency injection system with zero configuration:
10+
11+
- 🔧 **Zero Configuration DI**: Automatic component discovery
12+
- 📦 **Spring-like Annotations**: @Component, @Named, @Primary, @PostConstruct, @PreDestroy
13+
- 🔌 **Constructor Injection**: Clean, testable dependency injection
14+
- ⚖️ **Multiple Implementations**: Handle conflicts with @Named and @Primary
15+
- 🔄 **Lifecycle Management**: Proper initialization and cleanup
16+
- 📊 **Scope Management**: @Singleton and @Prototype support
17+
- 🔗 **Framework Integration**: Seamless integration with routing and controllers
18+
19+
## Version History
20+
21+
| Version | Release Date | Key Features |
22+
|---------|-------------|--------------|
23+
| **0.2.0** | 2025 | 🆕 **Dependency Injection System**, Spring-like annotations, automatic component discovery, lifecycle management |
24+
| **0.1.0** | 2025 | Core framework with routing, request/response handling, JSON utilities, validation system, metrics |
25+
26+
### 🔮 Upcoming Features (Roadmap)
27+
28+
| Planned Version | Features |
29+
|----------------|----------|
30+
| **0.3.0** | 🗄️ **Database Integration** - jOOQ integration, connection pooling, transaction management |
431

532
## Features
633

34+
### Core Framework (v0.1+)
735
- Simple and intuitive API
836
- Routing system with HTTP method support (GET, POST, PUT, DELETE, PATCH)
937
- URL path parameter support
1038
- Request validation with comprehensive rules
1139
- JSON response generation with fluent API
1240
- Metrics collection and reporting
1341

42+
### Dependency Injection (v0.2+)
43+
- **Zero Configuration**: Automatic component discovery with no setup
44+
- **Constructor Injection**: Dependencies injected via constructor parameters
45+
- **Named Injection**: Multiple implementations of same interface with @Named
46+
- **Primary Bean Selection**: Conflict resolution with @Primary annotation
47+
- **Lifecycle Management**: @PostConstruct and @PreDestroy callbacks
48+
- **Scope Management**: @Singleton (default) and @Prototype scopes
49+
- **Framework Integration**: DI works seamlessly with controllers and routing
50+
1451
## Quick Start
1552

16-
To develop a web application with Jazzy, you can follow this example code:
53+
### Basic Application (v0.1 style)
1754

1855
```java
1956
// App.java
@@ -45,80 +82,98 @@ public class App
4582
server.start(config.getServerPort());
4683
}
4784
}
85+
```
4886

49-
// UserController.java
50-
package examples.basic;
87+
### With Dependency Injection (v0.2 style)
5188

52-
import static jazzyframework.http.ResponseFactory.response;
53-
import jazzyframework.http.Request;
54-
import jazzyframework.http.Response;
55-
import jazzyframework.http.validation.ValidationResult;
56-
import java.util.Map;
89+
```java
90+
// Repository Component
91+
@Component
92+
public class UserRepository {
93+
private final List<User> users = new ArrayList<>();
94+
95+
@PostConstruct
96+
public void init() {
97+
System.out.println("UserRepository initialized");
98+
}
99+
100+
public List<User> findAll() {
101+
return new ArrayList<>(users);
102+
}
103+
}
57104

105+
// Service Component
106+
@Component
107+
public class UserService {
108+
private final UserRepository repository;
109+
110+
// Constructor injection - DI container automatically injects UserRepository
111+
public UserService(UserRepository repository) {
112+
this.repository = repository;
113+
}
114+
115+
public List<User> getAllUsers() {
116+
return repository.findAll();
117+
}
118+
}
119+
120+
// Controller Component
121+
@Component
58122
public class UserController {
123+
private final UserService userService;
59124

60-
public Response getUserById(Request request) {
61-
String id = request.path("id");
62-
63-
return response().json(
64-
"id", id,
65-
"name", "Fletcher Davidson",
66-
"email", "[email protected]"
67-
);
125+
// Constructor injection - DI container automatically injects UserService
126+
public UserController(UserService userService) {
127+
this.userService = userService;
68128
}
69129

70-
public Response getAllUsers(Request request) {
71-
int page = request.queryInt("page", 1);
72-
int limit = request.queryInt("limit", 10);
73-
74-
return response().json(
75-
"users", JSON.array(
76-
JSON.of("id", "user-1", "name", "Fletcher Davidson"),
77-
JSON.of("id", "user-2", "name", "Jane Smith")
78-
),
79-
"page", page,
80-
"limit", limit,
81-
"total", 2
82-
);
130+
public Response getUsers(Request request) {
131+
return Response.json(userService.getAllUsers());
83132
}
133+
}
84134

85-
public Response createUser(Request request) {
86-
// Validate the request
87-
ValidationResult result = request.validator()
88-
.field("name").required().minLength(3).maxLength(50)
89-
.field("email").required().email()
90-
.field("password").required().minLength(8)
91-
.validate();
92-
93-
if (!result.isValid()) {
94-
return response().json(
95-
"status", "error",
96-
"message", "Validation failed",
97-
"errors", result.getAllErrors()
98-
).status(400);
99-
}
100-
101-
Map<String, Object> userData = request.parseJson();
135+
// Application - DI works automatically!
136+
public class App {
137+
public static void main(String[] args) {
138+
Config config = new Config();
139+
Router router = new Router();
102140

103-
// Generate a new ID
104-
String newId = "user-" + System.currentTimeMillis();
105-
userData.put("id", newId);
141+
// Define routes - controllers will be created with DI
142+
router.GET("/users", "getUsers", UserController.class);
106143

107-
return response().success("User created successfully", userData).status(201);
144+
// DI is automatically enabled and configured
145+
Server server = new Server(router, config);
146+
server.start(8080);
108147
}
109-
110-
// Additional methods for update and delete operations
111148
}
112149
```
113150

114-
For a more detailed example, check the `src/main/java/examples/basic` directory.
151+
That's it! The DI container automatically:
152+
- Discovers all `@Component` classes
153+
- Resolves dependencies between them
154+
- Creates instances with proper injection
155+
- Manages lifecycle callbacks
115156

116157
## Documentation
117158

118159
Complete documentation for Jazzy Framework is available on our GitHub Pages site:
119160

120161
[Jazzy Framework Documentation](https://canermastan.github.io/jazzy-framework/)
121162

163+
### Documentation Sections
164+
165+
**Core Framework:**
166+
- [Getting Started Guide](https://canermastan.github.io/jazzy-framework/getting-started)
167+
- [Routing](https://canermastan.github.io/jazzy-framework/routing)
168+
- [HTTP Requests](https://canermastan.github.io/jazzy-framework/requests)
169+
- [HTTP Responses](https://canermastan.github.io/jazzy-framework/responses)
170+
- [JSON Operations](https://canermastan.github.io/jazzy-framework/json)
171+
- [Validation](https://canermastan.github.io/jazzy-framework/validation)
172+
173+
**Dependency Injection (v0.2+):**
174+
- [Dependency Injection Guide](https://canermastan.github.io/jazzy-framework/dependency-injection)
175+
- [DI Examples](https://canermastan.github.io/jazzy-framework/di-examples)
176+
122177
## Development
123178

124179
Jazzy is developed with Maven. After cloning the project, you can use the following commands:
@@ -130,63 +185,72 @@ mvn clean install
130185
# Run tests
131186
mvn test
132187

133-
# Run the example application
188+
# Run the basic example application
134189
mvn exec:java -Dexec.mainClass="examples.basic.App"
190+
191+
# Run the DI example application (v0.2+)
192+
mvn exec:java -Dexec.mainClass="examples.di.App"
135193
```
136194

137195
## Project Structure
138196

139197
- `core/`: Core framework components
140-
- `Server.java`: HTTP server
141-
- `RequestHandler.java`: HTTP request processor
198+
- `Server.java`: HTTP server with automatic DI initialization
199+
- `RequestHandler.java`: HTTP request processor with DI integration
142200
- `Config.java`: Configuration management
143201
- `Metrics.java`: Performance metrics
144202
- `routing/`: Routing system
145-
- `Router.java`: Route management
203+
- `Router.java`: Route management with DI container support
146204
- `Route.java`: Route data structure
147205
- `http/`: HTTP handling
148206
- `Request.java`: Request handling
149207
- `Response.java`: Response building
150208
- `ResponseFactory.java`: Factory for creating responses
151209
- `JSON.java`: JSON creation utilities
152210
- `validation/`: Validation system
211+
- `di/`: Dependency injection system (v0.2+)
212+
- `DIContainer.java`: Main DI container with automatic discovery
213+
- `ComponentScanner.java`: Automatic component scanning
214+
- `BeanDefinition.java`: Bean metadata and lifecycle management
215+
- `annotations/`: DI annotations (@Component, @Named, @Primary, etc.)
153216
- `controllers/`: System controllers
154217
- `MetricsController.java`: Metrics reporting
155218
- `examples/`: Example applications
156-
- `basic/`: A simple web API example
219+
- `basic/`: A simple web API example (v0.1 style)
220+
- `di/`: Dependency injection example (v0.2 style)
157221

158222
## Tests
159223

160-
Unit tests have been written to ensure the reliability of the framework. Test coverage includes:
224+
Comprehensive unit tests ensure the reliability of the framework. Test coverage includes:
161225

226+
**Core Framework Tests:**
162227
- `RouterTest`: Tests for adding routes, finding routes, and path parameter operations
163228
- `RouteTest`: Tests for the route data structure
164229
- `MetricsTest`: Tests for metric counters and calculations
165230
- `ValidationTest`: Tests for the validation system
166231
- `ResponseFactoryTest`: Tests for response generation
167232

168-
When adding new features or modifying existing code, it's important to update existing tests or add new tests to maintain the stability of the framework.
233+
**Dependency Injection Tests (v0.2+):**
234+
- `DIContainerTest`: Tests for DI container functionality and lifecycle
235+
- `ComponentScannerTest`: Tests for automatic component discovery
236+
- `BeanDefinitionTest`: Tests for bean metadata extraction
237+
- `DIIntegrationTest`: Tests for real-world DI scenarios
238+
- `AnnotationTest`: Tests for all DI annotations
239+
240+
**137 total tests** covering all framework features with comprehensive edge case testing.
169241

170-
## Roadmap
242+
## Migration Guide
171243

172-
Jazzy is actively being developed with the following features planned for upcoming releases:
244+
### From 0.1 to 0.2
173245

174-
### Upcoming Features
246+
**Good news: Zero breaking changes!** All existing 0.1 code continues to work without modification.
175247

176-
- **Middleware System**: Support for request/response middleware chains
177-
- **Database Integration**: jOOQ integration for type-safe SQL queries
178-
- **Dependency Injection**: Custom DI container (PococContainer) with `@Named` and `@Qualified` annotations
179-
- **Security Framework**: Authentication and authorization system
180-
- **Caching System**: Redis integration for high-performance caching
181-
- **API Documentation**: Swagger/OpenAPI integration
182-
- **WebSocket Support**: Real-time bidirectional communication
183-
- **Task Scheduling**: Cron-style job scheduling
184-
- **Monitoring Tools**: Health checks and system monitoring
185-
- **File Storage**: Cloud storage integrations (S3, etc)
186-
- **Event System**: Pub/sub event handling
187-
- **CLI Tools**: Command line tools for code generation
188-
- **And More...**: Stay tuned for additional features!
248+
**To use new DI features:**
249+
1. Add `@Component` annotation to classes you want managed by DI
250+
2. Use constructor injection for dependencies
251+
3. Optionally use `@Named`, `@Primary`, `@PostConstruct`, `@PreDestroy` for advanced features
189252

253+
You can gradually migrate - mix DI and manual instantiation in the same application.
190254

191255
## Contributing
192256

0 commit comments

Comments
 (0)