Examples of using Injekt (#5519)
This commit is contained in:
		
							parent
							
								
									c028c40079
								
							
						
					
					
						commit
						d7bbfb353d
					
				| @ -61,6 +61,11 @@ | ||||
|             <version>3.3.0</version> | ||||
|             <type>pom</type> | ||||
|         </dependency> | ||||
|         <dependency> | ||||
|             <groupId>uy.kohesive.injekt</groupId> | ||||
|             <artifactId>injekt-core</artifactId> | ||||
|             <version>1.16.1</version> | ||||
|         </dependency> | ||||
|     </dependencies> | ||||
| 
 | ||||
|     <properties> | ||||
|  | ||||
| @ -0,0 +1,60 @@ | ||||
| package com.baeldung.injekt | ||||
| 
 | ||||
| import org.slf4j.LoggerFactory | ||||
| import uy.kohesive.injekt.* | ||||
| import uy.kohesive.injekt.api.* | ||||
| import java.util.* | ||||
| 
 | ||||
| class DelegateInjectionApplication { | ||||
|     companion object : InjektMain() { | ||||
|         private val LOG = LoggerFactory.getLogger(DelegateInjectionApplication::class.java) | ||||
|         @JvmStatic fun main(args: Array<String>) { | ||||
|             DelegateInjectionApplication().run() | ||||
|         } | ||||
| 
 | ||||
|         override fun InjektRegistrar.registerInjectables() { | ||||
|             addFactory { | ||||
|                 val value = FactoryInstance("Factory" + UUID.randomUUID().toString()) | ||||
|                 LOG.info("Constructing instance: {}", value) | ||||
|                 value | ||||
|             } | ||||
| 
 | ||||
|             addSingletonFactory { | ||||
|                 val value = SingletonInstance("Singleton" + UUID.randomUUID().toString()) | ||||
|                 LOG.info("Constructing singleton instance: {}", value) | ||||
|                 value | ||||
|             } | ||||
| 
 | ||||
|             addSingletonFactory { App() } | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
|     data class FactoryInstance(val value: String) | ||||
|     data class SingletonInstance(val value: String) | ||||
| 
 | ||||
|     class App { | ||||
|         private val instance: FactoryInstance by injectValue() | ||||
|         private val lazyInstance: FactoryInstance by injectLazy() | ||||
|         private val singleton: SingletonInstance by injectValue() | ||||
|         private val lazySingleton: SingletonInstance by injectLazy() | ||||
| 
 | ||||
|         fun run() { | ||||
|             for (i in 1..5) { | ||||
|                 LOG.info("Instance {}: {}", i, instance) | ||||
|             } | ||||
|             for (i in 1..5) { | ||||
|                 LOG.info("Lazy Instance {}: {}", i, lazyInstance) | ||||
|             } | ||||
|             for (i in 1..5) { | ||||
|                 LOG.info("Singleton {}: {}", i, singleton) | ||||
|             } | ||||
|             for (i in 1..5) { | ||||
|                 LOG.info("Lazy Singleton {}: {}", i, lazySingleton) | ||||
|             } | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
|     fun run() { | ||||
|         Injekt.get<App>().run() | ||||
|     } | ||||
| } | ||||
| @ -0,0 +1,37 @@ | ||||
| package com.baeldung.injekt | ||||
| 
 | ||||
| import org.slf4j.LoggerFactory | ||||
| import uy.kohesive.injekt.* | ||||
| import uy.kohesive.injekt.api.* | ||||
| 
 | ||||
| class KeyedApplication { | ||||
|     companion object : InjektMain() { | ||||
|         private val LOG = LoggerFactory.getLogger(KeyedApplication::class.java) | ||||
|         @JvmStatic fun main(args: Array<String>) { | ||||
|             KeyedApplication().run() | ||||
|         } | ||||
| 
 | ||||
|         override fun InjektRegistrar.registerInjectables() { | ||||
|             val configs = mapOf( | ||||
|                     "google" to Config("googleClientId", "googleClientSecret"), | ||||
|                     "twitter" to Config("twitterClientId", "twitterClientSecret") | ||||
|             ) | ||||
|             addPerKeyFactory<Config, String> {key -> configs[key]!! } | ||||
| 
 | ||||
|             addSingletonFactory { App() } | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
|     data class Config(val clientId: String, val clientSecret: String) | ||||
| 
 | ||||
|     class App { | ||||
|         fun run() { | ||||
|             LOG.info("Google config: {}", Injekt.get<Config>("google")) | ||||
|             LOG.info("Twitter config: {}", Injekt.get<Config>("twitter")) | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
|     fun run() { | ||||
|         Injekt.get<App>().run() | ||||
|     } | ||||
| } | ||||
| @ -0,0 +1,46 @@ | ||||
| package com.baeldung.injekt | ||||
| 
 | ||||
| import org.slf4j.LoggerFactory | ||||
| import uy.kohesive.injekt.* | ||||
| import uy.kohesive.injekt.api.* | ||||
| 
 | ||||
| class ModularApplication { | ||||
|     class ConfigModule(private val port: Int) : InjektModule { | ||||
|         override fun InjektRegistrar.registerInjectables() { | ||||
|             addSingleton(Config(port)) | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
|     object ServerModule : InjektModule { | ||||
|         override fun InjektRegistrar.registerInjectables() { | ||||
|             addSingletonFactory { Server(Injekt.get()) } | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
|     companion object : InjektMain() { | ||||
|         private val LOG = LoggerFactory.getLogger(Server::class.java) | ||||
|         @JvmStatic fun main(args: Array<String>) { | ||||
|             ModularApplication().run() | ||||
|         } | ||||
| 
 | ||||
|         override fun InjektRegistrar.registerInjectables() { | ||||
|             importModule(ConfigModule(12345)) | ||||
|             importModule(ServerModule) | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
|     data class Config( | ||||
|             val port: Int | ||||
|     ) | ||||
| 
 | ||||
|     class Server(private val config: Config) { | ||||
| 
 | ||||
|         fun start() { | ||||
|             LOG.info("Starting server on ${config.port}") | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
|     fun run() { | ||||
|         Injekt.get<Server>().start() | ||||
|     } | ||||
| } | ||||
| @ -0,0 +1,48 @@ | ||||
| package com.baeldung.injekt | ||||
| 
 | ||||
| import org.slf4j.LoggerFactory | ||||
| import uy.kohesive.injekt.* | ||||
| import uy.kohesive.injekt.api.* | ||||
| import java.util.* | ||||
| import java.util.concurrent.Executors | ||||
| import java.util.concurrent.TimeUnit | ||||
| 
 | ||||
| class PerThreadApplication { | ||||
|     companion object : InjektMain() { | ||||
|         private val LOG = LoggerFactory.getLogger(PerThreadApplication::class.java) | ||||
|         @JvmStatic fun main(args: Array<String>) { | ||||
|             PerThreadApplication().run() | ||||
|         } | ||||
| 
 | ||||
|         override fun InjektRegistrar.registerInjectables() { | ||||
|             addPerThreadFactory { | ||||
|                 val value = FactoryInstance(UUID.randomUUID().toString()) | ||||
|                 LOG.info("Constructing instance: {}", value) | ||||
|                 value | ||||
|             } | ||||
| 
 | ||||
|             addSingletonFactory { App() } | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
|     data class FactoryInstance(val value: String) | ||||
| 
 | ||||
|     class App { | ||||
|         fun run() { | ||||
|             val threadPool = Executors.newFixedThreadPool(5) | ||||
| 
 | ||||
|             for (i in 1..20) { | ||||
|                 threadPool.submit { | ||||
|                     val instance = Injekt.get<FactoryInstance>() | ||||
|                     LOG.info("Value for thread {}: {}", Thread.currentThread().id, instance) | ||||
|                     TimeUnit.MILLISECONDS.sleep(100) | ||||
|                 } | ||||
|             } | ||||
|             threadPool.awaitTermination(10, TimeUnit.SECONDS) | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
|     fun run() { | ||||
|         Injekt.get<App>().run() | ||||
|     } | ||||
| } | ||||
| @ -0,0 +1,34 @@ | ||||
| package com.baeldung.injekt | ||||
| 
 | ||||
| import org.slf4j.LoggerFactory | ||||
| import uy.kohesive.injekt.* | ||||
| import uy.kohesive.injekt.api.* | ||||
| 
 | ||||
| class SimpleApplication { | ||||
|     companion object : InjektMain() { | ||||
|         private val LOG = LoggerFactory.getLogger(Server::class.java) | ||||
|         @JvmStatic fun main(args: Array<String>) { | ||||
|             SimpleApplication().run() | ||||
|         } | ||||
| 
 | ||||
|         override fun InjektRegistrar.registerInjectables() { | ||||
|             addSingleton(Config(12345)) | ||||
|             addSingletonFactory { Server(Injekt.get()) } | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
|     data class Config( | ||||
|             val port: Int | ||||
|     ) | ||||
| 
 | ||||
|     class Server(private val config: Config) { | ||||
| 
 | ||||
|         fun start() { | ||||
|             LOG.info("Starting server on ${config.port}") | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
|     fun run() { | ||||
|         Injekt.get<Server>().start() | ||||
|     } | ||||
| } | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user