Skip to content

Commit 29a5d00

Browse files
Merge pull request #300 from InsertKoinIO/feat_safety_check_rework
Merge features - Jsr330 compat, Scope Archetypes, Configuration & Application boostrap
2 parents 162933c + 706a5f6 commit 29a5d00

78 files changed

Lines changed: 2666 additions & 795 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

docs/reference/koin-annotations/definitions.md

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,3 +207,137 @@ public class ComponentWithProps(
207207
```
208208

209209
The generated DSL equivalent will be `factory { ComponentWithProps(getProperty("id", ComponentWithProps.DEFAAULT_ID)) }`
210+
211+
## JSR-330 Compatibility Annotations
212+
213+
Koin Annotations provides JSR-330 (Jakarta Inject) compatible annotations through the `koin-jsr330` module. These annotations are particularly useful for developers migrating from other JSR-330 compatible frameworks like Hilt, Dagger, or Guice.
214+
215+
### Setup
216+
217+
Add the `koin-jsr330` dependency to your project:
218+
219+
```kotlin
220+
dependencies {
221+
implementation "io.insert-koin:koin-jsr330:$koin_version"
222+
}
223+
```
224+
225+
### Available JSR-330 Annotations
226+
227+
#### @Singleton (jakarta.inject.Singleton)
228+
229+
JSR-330 standard singleton annotation, equivalent to Koin's `@Single`:
230+
231+
```kotlin
232+
import jakarta.inject.Singleton
233+
234+
@Singleton
235+
class DatabaseService
236+
```
237+
238+
This generates the same result as `@Single` - a singleton instance in Koin.
239+
240+
#### @Named (jakarta.inject.Named)
241+
242+
JSR-330 standard qualifier annotation for string-based qualifiers:
243+
244+
```kotlin
245+
import jakarta.inject.Named
246+
import jakarta.inject.Singleton
247+
248+
@Singleton
249+
@Named("inMemory")
250+
class InMemoryCache : Cache
251+
252+
@Singleton
253+
@Named("redis")
254+
class RedisCache : Cache
255+
```
256+
257+
#### @Inject (jakarta.inject.Inject)
258+
259+
JSR-330 standard injection annotation. While Koin Annotations doesn't require explicit constructor marking, `@Inject` can be used for JSR-330 compatibility:
260+
261+
```kotlin
262+
import jakarta.inject.Inject
263+
import jakarta.inject.Singleton
264+
265+
@Singleton
266+
class UserService @Inject constructor(
267+
private val repository: UserRepository
268+
)
269+
```
270+
271+
#### @Qualifier (jakarta.inject.Qualifier)
272+
273+
Meta-annotation for creating custom qualifier annotations:
274+
275+
```kotlin
276+
import jakarta.inject.Qualifier
277+
278+
@Qualifier
279+
annotation class Database
280+
281+
@Qualifier
282+
annotation class Cache
283+
284+
@Singleton
285+
@Database
286+
class DatabaseConfig
287+
288+
@Singleton
289+
@Cache
290+
class CacheConfig
291+
```
292+
293+
#### @Scope (jakarta.inject.Scope)
294+
295+
Meta-annotation for creating custom scope annotations:
296+
297+
```kotlin
298+
import jakarta.inject.Scope
299+
300+
@Scope
301+
annotation class RequestScoped
302+
303+
// Use with Koin's scope system
304+
@Scope(name = "request")
305+
@RequestScoped
306+
class RequestProcessor
307+
```
308+
309+
### Mixed Usage
310+
311+
You can freely mix JSR-330 annotations with Koin annotations in the same project:
312+
313+
```kotlin
314+
// JSR-330 style
315+
@Singleton
316+
@Named("primary")
317+
class PrimaryDatabase : Database
318+
319+
// Koin style
320+
@Single
321+
@Named("secondary")
322+
class SecondaryDatabase : Database
323+
324+
// Mixed in same class
325+
@Factory
326+
class DatabaseManager @Inject constructor(
327+
@Named("primary") private val primary: Database,
328+
@Named("secondary") private val secondary: Database
329+
)
330+
```
331+
332+
### Framework Migration Benefits
333+
334+
Using JSR-330 annotations provides several advantages for framework migration:
335+
336+
- **Familiar API**: Developers coming from Hilt, Dagger, or Guice can use known annotations
337+
- **Gradual Migration**: Existing JSR-330 annotated code works with minimal changes
338+
- **Standard Compliance**: Following JSR-330 ensures compatibility with dependency injection standards
339+
- **Team Onboarding**: Easier for teams familiar with other DI frameworks
340+
341+
:::info
342+
JSR-330 annotations in Koin generate the same underlying DSL as their Koin equivalents. The choice between JSR-330 and Koin annotations is purely stylistic and based on team preferences or migration requirements.
343+
:::

docs/reference/koin-annotations/modules.md

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,3 +122,141 @@ fun main() {
122122
}
123123
}
124124
```
125+
126+
## Application Bootstrap with @KoinApplication
127+
128+
To create a complete Koin application bootstrap, you can use the `@KoinApplication` annotation on an entry point class. This annotation helps generate Koin application bootstrap functions:
129+
130+
```kotlin
131+
@KoinApplication(
132+
configurations = ["default", "production"],
133+
modules = [MyModule::class]
134+
)
135+
class MyApp
136+
```
137+
138+
This generates **two** functions for starting your Koin application:
139+
140+
```kotlin
141+
// Use the generated application functions
142+
import org.koin.ksp.generated.*
143+
144+
fun main() {
145+
// Option 1: Start Koin directly
146+
MyApp().startKoin()
147+
148+
// Option 2: Get KoinApplication instance
149+
val koinApp = MyApp().koinApplication()
150+
}
151+
```
152+
153+
Both generated functions support custom configuration:
154+
155+
```kotlin
156+
fun main() {
157+
MyApp().startKoin {
158+
printLogger()
159+
// Add other Koin configuration
160+
}
161+
162+
// Or with koinApplication
163+
MyApp().koinApplication {
164+
printLogger()
165+
}
166+
}
167+
```
168+
169+
The `@KoinApplication` annotation supports:
170+
- `configurations`: Array of configuration names to scan and load
171+
- `modules`: Array of module classes to include directly (in addition to configurations)
172+
173+
When no configurations are specified, it automatically loads the "default" configuration.
174+
175+
## Configuration Management with @Configuration
176+
177+
The `@Configuration` annotation allows you to organize modules into different configurations (environments, flavors, etc.). This is useful for organizing modules by deployment environment or feature sets.
178+
179+
### Basic Configuration Usage
180+
181+
```kotlin
182+
// Default configuration - these are equivalent
183+
@Module
184+
@Configuration
185+
class CoreModule
186+
187+
@Module
188+
@Configuration("default")
189+
class CoreModule
190+
```
191+
192+
### Multiple Configuration Support
193+
194+
A module can be associated with multiple configurations:
195+
196+
```kotlin
197+
// This module is available in both "prod" and "test" configurations
198+
@Module
199+
@Configuration("prod", "test")
200+
class DatabaseModule {
201+
@Single
202+
fun database() = PostgreSQLDatabase()
203+
}
204+
205+
// This module is available in default, test, and development
206+
@Module
207+
@Configuration("default", "test", "development")
208+
class LoggingModule {
209+
@Single
210+
fun logger() = Logger()
211+
}
212+
```
213+
214+
### Environment-Specific Configurations
215+
216+
```kotlin
217+
// Development-only configuration
218+
@Module
219+
@Configuration("development")
220+
class DevDatabaseModule {
221+
@Single
222+
fun database() = InMemoryDatabase()
223+
}
224+
225+
// Production-only configuration
226+
@Module
227+
@Configuration("production")
228+
class ProdDatabaseModule {
229+
@Single
230+
fun database() = PostgreSQLDatabase()
231+
}
232+
233+
// Available in multiple environments
234+
@Module
235+
@Configuration("default", "production", "development")
236+
class CoreModule {
237+
@Single
238+
fun logger() = Logger()
239+
}
240+
```
241+
242+
### Using Configurations with @KoinApplication
243+
244+
Reference these configurations in your application bootstrap:
245+
246+
```kotlin
247+
@KoinApplication(configurations = ["default", "production"])
248+
class ProductionApp
249+
250+
@KoinApplication(configurations = ["default", "development"])
251+
class DevelopmentApp
252+
253+
// Load only default configuration (same as @KoinApplication with no parameters)
254+
@KoinApplication
255+
class SimpleApp
256+
```
257+
258+
:::info
259+
- Empty `@Configuration` is equivalent to `@Configuration("default")`
260+
- The "default" configuration is loaded automatically when no specific configurations are specified
261+
- Modules can belong to multiple configurations by listing them in the annotation
262+
:::

0 commit comments

Comments
 (0)