Skip to content

Instantly share code, notes, and snippets.

@mathieuancelin
Created April 13, 2012 12:25
Show Gist options
  • Save mathieuancelin/2376591 to your computer and use it in GitHub Desktop.
Save mathieuancelin/2376591 to your computer and use it in GitHub Desktop.
public class MyBean extends Application {
@Inject HelloService service;
public void displayHello() {
display( service.hello();
}
}
public class MyBean extends Application {
private HelloService service;
@Inject
public MyBean(HelloService service) {
this.service = service;
}
public void displayHello() {
display( service.hello();
}
}
public class MyBean extends Application {
private HelloService service;
@Inject
public void setService(HelloService service) {
this.service = service;
}
public void displayHello() {
display( service.hello();
}
}
public class HelloService {
public String hello() {
return "Hello World!";
}
}
public interface HelloService {
public String hello();
}
public class FrenchHelloService {
public String hello() {
return "Bonjour tout le monde!";
}
}
@French
public class FrenchHelloService {
public String hello() {
return "Bonjour tout le monde!";
}
}
@English
public class EnglishHelloService {
public String hello() {
return "Hello World!";
}
}
@Language(FRENCH)
public class FrenchHelloService {
public String hello() {
return "Bonjour tout le monde!";
}
}
@Language(ENGLISH)
public class EnglishHelloService {
public String hello() {
return "Hello World!";
}
}
@Qualifier
@Retention(RUNTIME)
@Target({FIELD, TYPE, METHOD, PARAMETER})
public @interface French {}
@Qualifier
@Retention(RUNTIME)
@Target({FIELD, TYPE, METHOD, PARAMETER})
public @interface English {}
@Qualifier
@Retention(RUNTIME)
@Target({FIELD, TYPE, METHOD, PARAMETER})
public @interface Language {
Languages lang();
public enum Languages {
FRENCH, ENGLISH
}
}
public class MyBean extends Application {
@Inject @French HelloService service;
public void displayHello() {
display( service.hello();
}
}
public class MyBean extends Application {
@Inject @English HelloService service;
public void displayHello() {
display( service.hello();
}
}
public class MyBean extends Application {
@Inject Language(ENGLISH) HelloService service;
public void displayHello() {
display( service.hello();
}
}
public class MyBean extends Application {
@Inject Language(FRENCH) HelloService service;
public void displayHello() {
display( service.hello();
}
}
public class MyBean extends Application {
@Inject Instance<HelloService> service;
public void displayHello() {
display( service.get().hello() );
}
}
public class MyBean extends Application {
@Inject Instance<HelloService> service;
public void displayHello() {
if (!service.isUnsatisfied()) {
display( service.get().hello() );
}
}
}
public class MyBean extends Application {
@Inject Instance<HelloService> services;
public void displayHello() {
for (HelloService service : services) {
display( service.hello() );
}
}
}
public class MyBean extends Application {
@Inject Instance<HelloService> services;
public void displayHello() {
display(
service.select(new AnnotationLiteral()<French> {})
.get() );
}
}
@InterceptorBinding
@Target({METHOD, TYPE})
@Retention(RUNTIME)
public @interface Loggable {}
@Loggable @Interceptor
public class LoggingInterceptor {
@AroundInvoke
public Object logMethod(InvocationContext ic) throws Exception {
System.out.println("Entering " + ic.getMethod().getName());
try {
return ic.proceed();
} finally {
System.out.println("Existing " + ic.getMethod().getName());
}
}
}
@Loggable
public class MyBean extends Application {
@Inject HelloService service;
public void displayHello() {
display( service.hello();
}
}
@Decorator
public class HelloDecorator implements HelloService {
@Inject @Delegate HelloService service;
public String hello() {
return service.hello() + "-decorated";
}
}
@Inject Event<String> evt;
...
evt.fire("Bonjour");
@Qualifier
@Retention(RUNTIME)
@Target({FIELD, TYPE, METHOD, PARAMETER})
public @interface English {}
@Inject @English Event<String> evt;
...
evt.fire("Hello");
public void receiveEvt(@Observes String evt) {
System.out.println("Reçut : " + evt);
}
public void receiveEvt(@Observes @English String evt) {
System.out.println("Received : " + evt);
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/beans_1_0.xsd">
<decorators>
<class>foo.bar.HelloDecorator</class>
</decorators>
</beans>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/beans_1_0.xsd">
<interceptors>
<class>foo.bar.LogInterceptor</class>
</interceptors>
</beans>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment