Created
December 29, 2014 16:57
-
-
Save sjednac/479c0f513338d1b3611f to your computer and use it in GitHub Desktop.
Optional Spring bean injection using a @Profile
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name := "spring-optional-beans" | |
version := "0.1-SNAPSHOT" | |
scalaVersion := "2.11.2" | |
resolvers ++= Seq( | |
"Sonatype Snapshots" at "https://oss.sonatype.org/content/repositories/snapshots/", | |
"Sonatype Releases" at "http://oss.sonatype.org/content/repositories/releases", | |
"Typesafe Repository" at "http://repo.typesafe.com/typesafe/releases/" | |
) | |
unmanagedClasspath in Runtime += file(".") | |
libraryDependencies ++= { | |
val springVersion = "4.0.3.RELEASE" | |
val slf4jVersion = "1.7.7" | |
Seq( | |
"org.springframework" % "spring-core" % springVersion, | |
"org.springframework" % "spring-context" % springVersion, | |
"javax.inject" % "javax.inject" % "1", | |
"org.slf4j" % "slf4j-api" % slf4jVersion, | |
"org.slf4j" % "slf4j-simple" % slf4jVersion | |
) | |
} | |
mainClass := Some("OptionalSpringBeanInjection") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import collection.JavaConversions._ | |
import org.springframework.beans.factory.annotation._ | |
import org.springframework.context.ApplicationContext | |
import org.springframework.context.annotation._ | |
import org.springframework.stereotype._ | |
package com.example.core { | |
@ComponentScan(basePackages = Array("com.example.core")) | |
@Configuration | |
class CoreConfig {} | |
@Service | |
class MyApplicationService { | |
@Autowired(required = false) | |
val newsProvider: com.example.news.NewsProvider = null | |
def greet(name: String): String = { | |
if (newsProvider != null) | |
newsProvider.headlineForToday | |
else | |
s"Hello, ${name}" | |
} | |
} | |
} | |
package com.example.news { | |
@Profile(Array("newsModule")) | |
@ComponentScan(basePackages = Array("com.example.news")) | |
@Configuration | |
class NewsConfig {} | |
trait NewsProvider { | |
def headlineForToday: String | |
} | |
@Service | |
class StaticNewsProvider extends NewsProvider { | |
def headlineForToday: String = "Evidence of life found on Mars!" | |
} | |
} | |
object OptionalSpringBeanInjection extends App { | |
val ctx = new AnnotationConfigApplicationContext(classOf[com.example.core.CoreConfig], classOf[com.example.news.NewsConfig]) | |
val service = ctx.getBean(classOf[com.example.core.MyApplicationService]) | |
val result = service.greet("John Doe") | |
println("*"*80) | |
println(result) | |
if (!sys.props.get("spring.profiles.active").getOrElse("").split(',').exists( p => p == "newsModule" )) { | |
println("Run 'sbt -Dspring.profiles.active=newsModule run' to enable the \"optional\" service.") | |
} | |
println("*"*80) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment