Using aspect I kept the annotation @In in Entities, may inject any seam 
component.

The Aspect intercepting the criation of the Entity and search the annotation 
@In. After it inject by Component.getInstance("componentName").

Below that the code written in AspectJ:

Using aspect I kept the annotation @In in Entities, may inject any seam 
component.

The Aspect intercepting the criation of the Entity and search the annotation 
@In. After it inject by Component.getInstance("componentName").

Below that the code written in AspectJ:

  | public aspect InjectEntity{
  |     
  |     //intercept the package of Entities
  |     pointcut inject() : initialization(public 
br.com.siq.pesquisa.entidade.*.new (..));
  |      
  |     after():inject(){
  |             Object obj = thisJoinPoint.getThis();
  |             String valueOfContextVariable = null;
  |             Boolean createContextVariable=false;
  | 
  |             for(Field field:obj.getClass().getDeclaredFields()){
  |                     if(field.isAnnotationPresent(In.class)){
  | 
  |                             In in = field.getAnnotation(In.class);
  |                             
  |                             if(in.value().equals("")){
  |                                     valueOfContextVariable = 
field.getName();
  |                             }else{
  |                                     valueOfContextVariable = in.value();
  |                             }       
  |                             
  |                             if(in.create())
  |                                     createContextVariable = true;
  | 
  |                             field.setAccessible(true);
  |                             
  |                             try {
  |                                     field.set(obj, 
Component.getInstance(valueOfContextVariable, createContextVariable));
  |                             } catch (IllegalStateException e) {
  |                                     log.error("Don´t have context active");
  |                             }catch(Exception e){
  |                                     log.error(e);
  |                             }
  |                     }
  |             }
  |     }
  |     
  | }
  | 
  | 

The code of Entity now can be written with:


  | @Entity
  | public class User{
  | 
  |     @In
  |     private UserRepository userRepository;
  |    
  |    //more code
  |     (...)
  | 
  | }
  | 

... the Entity user isn't necessarily a Seam component, but works ;)

----------------------------- 
[]´s 
Alessandro Lazarotti

View the original post : 
http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4101541#4101541

Reply to the post : 
http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=4101541

_______________________________________________
jboss-user mailing list
[email protected]
https://lists.jboss.org/mailman/listinfo/jboss-user

Reply via email to