Basic examples on cdi and beans validation

This commit is contained in:
2015-04-30 01:33:05 +02:00
commit 23bf95316c
31 changed files with 878 additions and 0 deletions

54
test1_cdi/Class.java Normal file
View File

@@ -0,0 +1,54 @@
package test1_cdi;
import javax.annotation.PostConstruct;
import javax.enterprise.event.Observes;
import javax.inject.Inject;
import java.util.logging.Logger;
import javax.enterprise.event.Event;
/**
* Created by Giovanni on 26/04/2015.
*/
@ClassI
@GeneralInterceptorA
public class Class implements ClassInterface {
@Inject
Logger l;
@Inject @StudentI(implementation = student_implementations.two)
Student s;
@Inject @NumberOfStudents
int number_of_students;
@Inject @Event1
Event<Object> event_shooter1;
@Inject @Event2
Event<Object> event_shooter2;
@PostConstruct
public void do_post_construct(){
l.info("XGLOG: Post Construct done");
}
public String printStudent (){
event_shooter1.fire(new Object());
event_shooter2.fire(new Object());
return (s.getinfo() +" " + number_of_students);
}
public void observe1 (@Observes @Event1 Object b){
l.info("XGLOG: Event1 captured!");
}
public void observe2 (@Observes @Event2 Object b){
l.info("XGLOG: Event2 captured!");
}
}

View File

@@ -0,0 +1,25 @@
package test1_cdi;
import javax.decorator.Decorator;
import javax.decorator.Delegate;
import javax.inject.Inject;
/**
* Created by Giovanni on 27/04/2015.
*/
@Decorator
public class ClassDecorator implements ClassInterface {
@Inject @Delegate @ClassI
private ClassInterface c;
public String printStudent() {
return c.printStudent() + " decorated ";
}
}

20
test1_cdi/ClassI.java Normal file
View File

@@ -0,0 +1,20 @@
package test1_cdi;
import javax.inject.Qualifier;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.ElementType.TYPE;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
/**
* Created by Giovanni on 26/04/2015.
*/
@Qualifier
@Retention(RUNTIME)
@Target({FIELD, TYPE, METHOD})
public @interface ClassI {
}

View File

@@ -0,0 +1,12 @@
package test1_cdi;
/**
* Created by Giovanni on 27/04/2015.
*/
public interface ClassInterface {
String printStudent ();
}

View File

@@ -0,0 +1,47 @@
package test1_cdi;
import javax.enterprise.inject.*;
import javax.inject.Inject;
import java.util.logging.Logger;
/**
* Created by Giovanni on 26/04/2015.
*/
public class ClassProducer {
@Inject
Logger l;
@Inject @StudentI(implementation = student_implementations.one)
Student s;
@Produces @NumberOfStudents
private int number_of_students(){
return 10;
}
@Produces
private int number_of_students_2(){
return 20;
}
private void clean (@Disposes int s) {
l.info("Do something to clean the producer");
}
private void clean2(@Disposes @NumberOfStudents int s) {
l.info("Do something to clean the producer");
}
}

19
test1_cdi/Event1.java Normal file
View File

@@ -0,0 +1,19 @@
package test1_cdi;
import javax.inject.Qualifier;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import static java.lang.annotation.ElementType.*;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
/**
* Created by Giovanni on 26/04/2015.
*/
@Qualifier
@Retention(RUNTIME)
@Target({FIELD, TYPE, METHOD, PARAMETER})
public @interface Event1 {}

19
test1_cdi/Event2.java Normal file
View File

@@ -0,0 +1,19 @@
package test1_cdi;
import javax.inject.Qualifier;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import static java.lang.annotation.ElementType.*;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
/**
* Created by Giovanni on 26/04/2015.
*/
@Qualifier
@Retention(RUNTIME)
@Target({FIELD, TYPE, METHOD, PARAMETER})
public @interface Event2 {}

View File

@@ -0,0 +1,58 @@
package test1_cdi;
/**
* Created by Giovanni on 27/04/2015.
*/
import javax.annotation.PostConstruct;
import javax.annotation.Priority;
import javax.inject.Inject;
import javax.interceptor.*;
import java.util.logging.Logger;
@Interceptor
@GeneralInterceptorA
@Priority(200)
public class GeneralInterceptor {
@Inject
private Logger logger;
@AroundConstruct
private void logConstruct(InvocationContext ic) throws Exception {
logger.info("XGLOG: Begin Constructor Interceptor");
try {
ic.proceed();
} finally {
logger.info("XGLOG: End Constructor Interceptor");
}
}
@PostConstruct
public void logPostConstruct (InvocationContext ic) throws Exception {
logger.info("XGLOG: Begin PostConstructor Interceptor");
try {
ic.proceed();
} finally {
logger.info("XGLOG: End PostConstructor Interceptor");
}
}
@AroundInvoke
public Object logMethod(InvocationContext ic) throws Exception {
logger.info("XGLOG: Begin " + ic.getTarget().toString() + " "+ ic.getMethod().getName()+ " Interceptor");
try {
return ic.proceed();
} finally {
logger.info("XGLOG: End " + ic.getTarget().toString() + " "+ ic.getMethod().getName() + " Interceptor");
}
}
}

View File

@@ -0,0 +1,19 @@
package test1_cdi;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import static java.lang.annotation.ElementType.*;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
import javax.interceptor.InterceptorBinding;
/**
* Created by Giovanni on 27/04/2015.
*/
@InterceptorBinding
@Target({METHOD, TYPE, CONSTRUCTOR})
@Retention(RUNTIME)
public @interface GeneralInterceptorA {
}

View File

@@ -0,0 +1,20 @@
package test1_cdi;
import javax.inject.Qualifier;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import static java.lang.annotation.ElementType.*;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
/**
* Created by Giovanni on 26/04/2015.
*/
@Qualifier
@Retention(RUNTIME)
@Target({FIELD, TYPE, METHOD,PARAMETER})
public @interface NumberOfStudents {
}

10
test1_cdi/Student.java Normal file
View File

@@ -0,0 +1,10 @@
package test1_cdi;
/**
* Created by Giovanni on 26/04/2015.
*/
public interface Student {
String getinfo ();
}

View File

@@ -0,0 +1,15 @@
package test1_cdi;
/**
* Created by Giovanni on 26/04/2015.
*/
import javax.enterprise.inject.Alternative;
@Alternative @StudentI(implementation = student_implementations.one)
public class StudentAlternative implements Student {
@Override
public String getinfo() {
return "i'm an alternative";
}
}

View File

@@ -0,0 +1,15 @@
package test1_cdi;
/**
* Created by Giovanni on 26/04/2015.
*/
import javax.enterprise.inject.Alternative;
@Alternative @StudentI(implementation = student_implementations.two)
public class StudentAlternative2 implements Student {
@Override
public String getinfo() {
return "i'm an alternative 2";
}
}

21
test1_cdi/StudentI.java Normal file
View File

@@ -0,0 +1,21 @@
package test1_cdi;
import javax.inject.Qualifier;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import static java.lang.annotation.ElementType.*;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
/**
* Created by Giovanni on 26/04/2015.
*/
@Qualifier
@Retention(RUNTIME)
@Target({FIELD, TYPE, METHOD})
public @interface StudentI {
student_implementations implementation();
}

View File

@@ -0,0 +1,14 @@
package test1_cdi;
/**
* Created by Giovanni on 26/04/2015.
*/
@StudentI(implementation = student_implementations.one)
public class StudentImplemented implements Student {
@Override
public String getinfo() {
return "null null";
}
}

View File

@@ -0,0 +1,14 @@
package test1_cdi;
/**
* Created by Giovanni on 26/04/2015.
*/
@StudentI(implementation = student_implementations.two)
public class StudentImplemented2 implements Student {
@Override
public String getinfo() {
return "null null 2";
}
}

View File

@@ -0,0 +1,3 @@
package test1_cdi;
public enum student_implementations {one,two}