[jboss-user] [JBoss Seam] - Re: Entity withiout Injection --- How do construct a efficie

alessandrolt do-not-reply at jboss.com
Sat Nov 3 14:33:53 EDT 2007


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




More information about the jboss-user mailing list