4 Replies Latest reply on Nov 13, 2013 2:13 AM by Romain LE BARO

    Annotation not proceeds by Weld in somes beans!

    Romain LE BARO Newbie

      Hi,  First I use Weld 2.1.0 Final on a dynamic project without JSF with tomcat 7.

       

      i have a Front Controller that dispatches all requests on to Java classes.

      All beans that are injected in the Front Controller, works fine, can used @Inject and are proceeds by weld later.

      But in one of this beans ( Router) . I instantiate dynamicaly java classes. This Classes are not proceeds by weld. The Annotations like @PostContruct and @Inject are not resolved.

      I don't know why.

       

      This is my code :

       

      @Named

      @RequestScoped

      public class LoginBean implements Serializable {

             /**

             *

              */

             private static final long serialVersionUID = -1059918784879936041L;

             private final static Logger log = Logger.getLogger(LoginBean.class);

          

             @Inject private PropertyLoader prop;

       

             public String Index(){

                   log.info("Envoi du formulaire de connexion");

                   return "connexion";

         }

          

             public LoginBean() {

                   log.info("login bean");

         }

          

             @PostConstruct

             private void init(){

                   prop.setProp("supervision.default.layout", "nothing");

                   log.info(prop.getProperty("layout"));

                   log.info("TOTOTOTOTOTOTOTOTOT");

         }

      }

       

      @Named

      @Singleton

      public class Router {

             private final static Logger log = Logger.getLogger(Router.class);

             @Inject private Parser parser;

             @Inject private ClassLoader cl;

          

             public Router(){

                

         }

          

             public String forward(String uri) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, ClassNotFoundException, InstantiationException{

                   log.info("Uri forward");

                   parser.parse(uri);

                

                   log.debug("Liste des managed Bean disponible = " + cl.getClasses());

                   log.debug("Liste des managed Bean disponible = " + parser.getController());

                   String classFullName = cl.getClasses().get(parser.getController());

                   if(classFullName == null){

                          throw new ClassNotFoundException("Class name " + parser.getController() + " don't exist!");

                   }

                   Class<?> c = Class.forName(classFullName);

                   Object o = c.newInstance();

                   Method[] methods = c.getMethods();

                   for (Method method : methods) {

                          if(method.getName().toLowerCase().equals(parser.getAction().toLowerCase())){

                                 Class<?>[] paramTypes = method.getParameterTypes();

                              

                                 if(parser.getParams().size() >= paramTypes.length){

                                        Object[] parameters = new Object [paramTypes.length];

                                        int i = 0;

                                        for (Class<?> paramType : paramTypes) {

                                              if(paramType == String.class){

                                                     parameters[i] = String.valueOf(parser.getParams().get(String.valueOf(i+1)));

                                              } else if(paramType == Integer.class || paramType == int.class){

                                                     parameters[i] = Integer.valueOf(parser.getParams().get(String.valueOf(i+1))).intValue();

                                              } else if(paramType == Long.class || paramType == long.class){

                                                     parameters[i] = Long.valueOf(parser.getParams().get(String.valueOf(i+1))).longValue();

                                              } else if(paramType == Double.class || paramType == double.class){

                                                     parameters[i] = Double.valueOf(parser.getParams().get(String.valueOf(i+1))).doubleValue();

                                              } else if(paramType == Float.class || paramType == float.class){

                                                     parameters[i] = Float.valueOf(parser.getParams().get(String.valueOf(i+1))).floatValue();

                                              } else if(paramType == Boolean.class || paramType == boolean.class){

                                                     parameters[i] = Boolean.valueOf(parser.getParams().get(String.valueOf(i+1))).booleanValue();

                                              } else if(paramType == Character.class || paramType == char.class){

                                                     parameters[i] = String.valueOf(parser.getParams().get(String.valueOf(i+1))).charAt(0);

                                              }

                                        }

                                        return (String) method.invoke(o, parameters);

                                 } else {

                                        throw new IllegalArgumentException("Method " + method.getName() + " in bean " + c.getName() +

                                                     " need " + paramTypes.length + " argument(s). Only " + parser.getParams().size() + " provide");

                                 }

                          }

                   }

                   throw new ClassNotFoundException();

             }

      }

       

       

      Any solution? PropertyLoader in LoginBean is not injected, and @PostContruct not called.