3 Replies Latest reply on Nov 13, 2009 7:48 AM by Nicklas Karlsson

    examining a concrete Weld example from se.simple.example - Helloworld

    Arbi Sookazian Master
      @ApplicationScoped
      public class CommandLineArgsValidator
      {
      
          @Inject
          private @Parameters List<String> validParams;
          private List<String> errors = new ArrayList<String>();
      
          public CommandLineArgsValidator()
          {
          }
      
          @Inject
          public void checkParameters()
          {
              if (validParams.size() != 1)
              {
                  errors.add( "Please supply just one parameter: your first name" );
                  validParams = Collections.EMPTY_LIST;
              }
          }
      
         public boolean hasErrors()
         {
            return !this.errors.isEmpty();
         }
      
         public List<String> getErrors()
         {
            return errors;
         }
      
         public List<String> getValidParameters()
         {
            return validParams;
         }
      }
      



      1) so validParams is injected from where?


      2) how does method-level injection work? referring to checkParameters(), what is being injected from where?


      IIRC GKing stated there are no contextual variables in CDI, so this is a very basic concept everybody needs to understand if you're coming from a Seam perspective...

        • 1. Re: examining a concrete Weld example from se.simple.example - Helloworld
          Arbi Sookazian Master

          So how does this method get invoked exactly?


          org.jboss.weld.environment.se.StartMain.main()



          I'm imagining it's trace back to this cmd line command:

          mvn -Drun -Dname=Pete



          aha! found it in pom.xml:


          <profiles>
                <profile>
                   <id>run</id>
                   <activation>
                      <property>
                         <name>run</name>
                      </property>
                   </activation>
                   <build>
                      <plugins>
                         <plugin>
                            <executions>
                               <execution>
                                  <id>run</id>
                                  <phase>package</phase>
                                  <goals>
                                     <goal>java</goal>
                                  </goals>
                               </execution>
                            </executions>
                            <groupId>org.codehaus.mojo</groupId>
                            <artifactId>exec-maven-plugin</artifactId>
                            <configuration>
                               <mainClass>org.jboss.weld.environment.se.StartMain</mainClass>
                               <arguments>
                                  <argument>${name}</argument>
                               </arguments>
                            </configuration>
                         </plugin>
                      </plugins>
                   </build>
                </profile>
             </profiles>



          and then this method in org.jboss.weld.environment.se.example.simple.HelloWorld is invoked:

          public void printHello( @Observes ContainerInitialized init )



          b/c of the presence of the @Observes annotation.  In this case the container is the JVM or what as this is a simple Java app that is not deployed in a servlet container...


          Is this legal?


          @Observes
          public void printHello( ContainerInitialized init )

          • 2. Re: examining a concrete Weld example from se.simple.example - Helloworld
            Nicklas Karlsson Master

            1. Somewhere, accessible to the manager, you need to have something resolvable with the qualifier @Parameters and the type List<String> (most probably a producer in this case)

            2. Nothing is being injected in this case. If the method would have had parameters, they would have been resolved and injected.


            • 3. Re: examining a concrete Weld example from se.simple.example - Helloworld
              Nicklas Karlsson Master

              No. Arbi, you promised us you would read the specs ;-)