Last active
August 29, 2015 14:08
-
-
Save joshlong/3ceb8d326c7c048670f7 to your computer and use it in GitHub Desktop.
This demonstrates using JSR 330 annotations and JSR 330's `@Qualifier` (on Spring)
This file contains 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
package jsr330; | |
import org.springframework.context.annotation.AnnotationConfigApplicationContext; | |
import org.springframework.context.annotation.ComponentScan; | |
import org.springframework.context.annotation.Configuration; | |
import javax.annotation.PostConstruct; | |
import javax.inject.Inject; | |
import javax.inject.Named; | |
import java.lang.annotation.ElementType; | |
import java.lang.annotation.Retention; | |
import java.lang.annotation.RetentionPolicy; | |
import java.lang.annotation.Target; | |
import static jsr330.Jsr330.Platform; | |
@Configuration | |
@ComponentScan | |
public class Jsr330 { | |
public static void main(String[] args) { | |
new AnnotationConfigApplicationContext(Jsr330.class); | |
} | |
@Inject | |
@Platform(Platform.OperatingSystems.ANDROID) | |
private MarketPlace android; | |
@Inject | |
@Platform(Platform.OperatingSystems.IOS) | |
private MarketPlace ios; | |
@PostConstruct | |
public void qualifyTheTweets() { | |
System.out.println("ios:" + this.ios); | |
System.out.println("android:" + this.android); | |
} | |
// the type has to be public! | |
@Target({ElementType.FIELD, | |
ElementType.METHOD, | |
ElementType.TYPE, | |
ElementType.PARAMETER}) | |
@Retention(RetentionPolicy.RUNTIME) | |
@javax.inject.Qualifier | |
public static @interface Platform { | |
OperatingSystems value(); | |
public static enum OperatingSystems { | |
IOS, | |
ANDROID | |
} | |
} | |
} | |
interface MarketPlace { | |
} | |
@Named | |
@Platform(Platform.OperatingSystems.IOS) | |
class AppleMarketPlace implements MarketPlace { | |
@Override | |
public String toString() { | |
return "apple"; | |
} | |
} | |
@Named | |
@Platform(Platform.OperatingSystems.ANDROID) | |
class GoogleMarketPlace implements MarketPlace { | |
@Override | |
public String toString() { | |
return "android"; | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment