7 Replies Latest reply on Sep 15, 2011 10:46 AM by ben_utzer

    Newbee question concerning injection

    ben_utzer

      Hi guys,


      I'm having some trouble getting my first simple example app to do what I want and I hope someone can help.
      The problem is that #{person.nachname} is null after I entered some text and submitted the form. What am I doing wrong?


      It's running with the lates AS 7 Snapshot.


      Here's the code:


      pom:




      <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
           <modelVersion>4.0.0</modelVersion>
           <groupId>org.example.seam</groupId>
           <artifactId>seam-sandbox</artifactId>
           <version>0.0.1-SNAPSHOT</version>
           <packaging>war</packaging>
           <properties>
                <seam.bom.version>3.1.0.Beta2</seam.bom.version>
                <jboss.javaee6.version>2.0.0.Final</jboss.javaee6.version>
                <jodatime.version>1.6.2</jodatime.version>
                <arquillian.version>1.0.0.CR2</arquillian.version>
                <arquillian.jbossas7.version>7.0.0.Final</arquillian.jbossas7.version>
                <richfaces.version>4.1.0.20110805-M1</richfaces.version>
                <prettyfaces.version>3.3.0</prettyfaces.version>
                <junit.version>4.8.2</junit.version>
                <jbossas.7.version>7.0.1.Final</jbossas.7.version>
                <primefaces.version>3.0.RC1-SNAPSHOT</primefaces.version>
      
           </properties>
           <!-- Use the Seam 3 BOM to get the correct versions for most modules -->
           <dependencyManagement>
                <dependencies>
                     <dependency>
                          <groupId>org.jboss.seam</groupId>
                          <artifactId>seam-bom</artifactId>
                          <version>${seam.bom.version}</version>
                          <type>pom</type>
                          <scope>import</scope>
                     </dependency>
                     <dependency>
                          <groupId>org.richfaces</groupId>
                          <artifactId>richfaces-bom</artifactId>
                          <version>${richfaces.version}</version>
                          <type>pom</type>
                          <scope>import</scope>
                     </dependency>
                </dependencies>
           </dependencyManagement>
           <dependencies>
                <dependency>
                     <groupId>com.sun.faces</groupId>
                     <artifactId>jsf-api</artifactId>
                     <version>2.1.2</version>
                </dependency>
                <dependency>
                     <groupId>com.sun.faces</groupId>
                     <artifactId>jsf-impl</artifactId>
                     <version>2.1.2</version>
                </dependency>
                <dependency>
                     <groupId>org.jboss.spec</groupId>
                     <artifactId>jboss-javaee-6.0</artifactId>
                     <version>${jboss.javaee6.version}</version>
                     <type>pom</type>
                     <scope>provided</scope>
                </dependency>
                <dependency>
                     <groupId>org.jboss.seam.faces</groupId>
                     <artifactId>seam-faces</artifactId>
                </dependency>
                <dependency>
                     <groupId>org.jboss.seam.international</groupId>
                     <artifactId>seam-international</artifactId>
                </dependency>
                <dependency>
                     <groupId>org.jboss.seam.catch</groupId>
                     <artifactId>seam-catch</artifactId>
                </dependency>
                <dependency>
                     <groupId>org.jboss.seam.security</groupId>
                     <artifactId>seam-security</artifactId>
                </dependency>
                <dependency>
                     <groupId>org.jboss.seam.config</groupId>
                     <artifactId>seam-config-xml</artifactId>
                </dependency>
                <dependency>
                     <groupId>org.jboss.seam.persistence</groupId>
                     <artifactId>seam-persistence</artifactId>
                </dependency>
                <dependency>
                     <groupId>org.jboss.seam.validation</groupId>
                     <artifactId>seam-validation</artifactId>
                </dependency>
                <!-- This and setting the build to 1.6 enable the annotation processor 
                     to be used for logging -->
                <dependency>
                     <groupId>org.jboss.seam.solder</groupId>
                     <artifactId>seam-solder-tooling</artifactId>
                     <scope>provided</scope>
                </dependency>
                <dependency>
                     <groupId>org.jboss.seam.solder</groupId>
                     <artifactId>seam-solder-logging</artifactId>
                     <version>3.1.0.Beta2</version>
                </dependency>
                <dependency>
                     <groupId>joda-time</groupId>
                     <artifactId>joda-time</artifactId>
                     <version>${jodatime.version}</version>
                </dependency>
                <dependency>
                     <groupId>org.richfaces.ui</groupId>
                     <artifactId>richfaces-components-ui</artifactId>
                </dependency>
                <dependency>
                     <groupId>org.richfaces.core</groupId>
                     <artifactId>richfaces-core-impl</artifactId>
                </dependency>
                
                <dependency>
                     <groupId>org.primefaces</groupId>
                     <artifactId>primefaces</artifactId>
                     <version>${primefaces.version}</version>
                </dependency>
      
                <!-- Runtime dependencies -->
                <dependency>
                     <groupId>com.ocpsoft</groupId>
                     <artifactId>prettyfaces-jsf2</artifactId>
                     <version>${prettyfaces.version}</version>
                     <scope>runtime</scope>
                </dependency>
                <dependency>
                     <groupId>org.jboss.seam.transaction</groupId>
                     <artifactId>seam-transaction</artifactId>
                </dependency>
      
                <dependency>
                     <groupId>org.jboss.arquillian.junit</groupId>
                     <artifactId>arquillian-junit-container</artifactId>
                     <version>${arquillian.version}</version>
                     <scope>test</scope>
                </dependency>
                <dependency>
                     <groupId>junit</groupId>
                     <artifactId>junit</artifactId>
                     <version>${junit.version}</version>
                     <scope>test</scope>
                </dependency>
                <dependency>
                     <groupId>org.jboss.shrinkwrap.resolver</groupId>
                     <artifactId>shrinkwrap-resolver-impl-maven</artifactId>
                     <version>1.0.0-beta-3</version>
                     <scope>test</scope>
                </dependency>
      
                <dependency>
                     <groupId>org.jboss.logging</groupId>
                     <artifactId>jboss-logging</artifactId>
                     <version>3.0.1.GA</version>
                </dependency>
                <dependency>
                     <groupId>org.jboss.logmanager</groupId>
                     <artifactId>jboss-logmanager</artifactId>
                     <version>1.2.0.GA</version>
                </dependency>
      
           </dependencies>
      
           <build>
                <finalName>${project.artifactId}</finalName>
                <plugins>
                     <!-- This plugin config is used to set the Dependencies entry in META-INF/MANIFEST.MF 
                          so that in AS7 both JBoss Logging and the JBoss Log manager will be available 
                          in the classpath -->
                     <plugin>
                          <groupId>org.apache.maven.plugins</groupId>
                          <artifactId>maven-war-plugin</artifactId>
                          <configuration>
                               <archive>
                                    <manifestEntries>
                                         <Dependencies>org.jboss.logging,org.jboss.logmanager,org.apache.log4j</Dependencies>
                                    </manifestEntries>
                               </archive>
                          </configuration>
                     </plugin>
                     <plugin>
                          <artifactId>maven-compiler-plugin</artifactId>
                          <configuration>
                               <source>1.6</source>
                               <target>1.6</target>
                          </configuration>
                     </plugin>
                     <plugin>
                          <artifactId>maven-surefire-plugin</artifactId>
                          <version>2.9</version>
                          <executions>
                               <execution>
                                    <id>default-test</id>
                                    <phase>test</phase>
                                    <goals>
                                         <goal>test</goal>
                                    </goals>
                                    <configuration>
                                         <excludes>
                                              <exclude>**/integration/**/*.java</exclude>
                                         </excludes>
                                    </configuration>
                               </execution>
                          </executions>
                     </plugin>
                </plugins>
           </build>
      
           <profiles>
                <profile>
                     <id>jboss-public-repository</id>
                     <activation>
                          <property>
                               <name>jboss-public-repository</name>
                               <value>!false</value>
                          </property>
                     </activation>
                     <repositories>
                          <repository>
                               <id>jboss-public-repository-group</id>
                               <name>JBoss Public Maven Repository Group</name>
                               <url>http://repository.jboss.org/nexus/content/groups/public</url>
                               <releases>
                                    <enabled>true</enabled>
                                    <updatePolicy>never</updatePolicy>
                               </releases>
                               <snapshots>
                                    <enabled>true</enabled>
                                    <updatePolicy>never</updatePolicy>
                               </snapshots>
                          </repository>
                     </repositories>
                     <pluginRepositories>
                          <pluginRepository>
                               <id>jboss-public-repository-group</id>
                               <name>JBoss Public Maven Repository Group</name>
                               <url>http://repository.jboss.org/nexus/content/groups/public</url>
                               <releases>
                                    <enabled>true</enabled>
                                    <updatePolicy>never</updatePolicy>
                               </releases>
                               <snapshots>
                                    <enabled>true</enabled>
                                    <updatePolicy>never</updatePolicy>
                               </snapshots>
                          </pluginRepository>
                     </pluginRepositories>
                </profile>
                <profile>
                     <id>arq-jboss-as-managed</id>
                     <activation>
                          <property>
                               <name>integration</name>
                          </property>
                     </activation>
                     <dependencies>
                          <dependency>
                               <groupId>org.jboss.as</groupId>
                               <artifactId>jboss-as-arquillian-container-managed</artifactId>
                               <version>${arquillian.jbossas7.version}</version>
                               <scope>test</scope>
                          </dependency>
                     </dependencies>
                     <build>
                          <plugins>
      
                               <plugin>
                                    <groupId>org.apache.maven.plugins</groupId>
                                    <artifactId>maven-dependency-plugin</artifactId>
                                    <executions>
                                         <execution>
                                              <id>unpack-as7</id>
                                              <phase>generate-test-sources</phase>
                                              <!-- So run before testing -->
                                              <goals>
                                                   <goal>unpack</goal>
                                              </goals>
                                              <configuration>
                                                   <artifactItems>
                                                        <artifactItem>
                                                             <groupId>org.jboss.as</groupId>
                                                             <artifactId>jboss-as-dist</artifactId>
                                                             <version>${jbossas.7.version}</version>
                                                             <type>zip</type>
                                                             <overWrite>false</overWrite>
                                                             <outputDirectory>${project.build.directory}</outputDirectory>
                                                        </artifactItem>
                                                   </artifactItems>
                                              </configuration>
                                         </execution>
                                    </executions>
                               </plugin>
                               <plugin>
                                    <artifactId>maven-surefire-plugin</artifactId>
                                    <version>2.9</version>
                                    <executions>
                                         <execution>
                                              <id>integration-tests</id>
                                              <phase>integration-test</phase>
                                              <goals>
                                                   <goal>test</goal>
                                              </goals>
                                              <configuration>
                                                   <skip>false</skip>
                                                   <includes>
                                                        <include>**/integration/**/*.java</include>
                                                   </includes>
                                                   <redirectTestOutputToFile>true</redirectTestOutputToFile>
                                                   <trimStackTrace>false</trimStackTrace>
                                                   <printSummary>true</printSummary>
                                                   <forkMode>once</forkMode>
                                              </configuration>
                                         </execution>
                                    </executions>
                               </plugin>
                          </plugins>
                     </build>
                </profile>
           </profiles>
      </project>
      



      Bean Test:




      package beans;
      
      import java.io.Serializable;
      
      import javax.annotation.PostConstruct;
      import javax.enterprise.context.SessionScoped;
      import javax.enterprise.inject.Produces;
      import javax.inject.Inject;
      import javax.inject.Named;
      
      import domain.Person;
      
      @SessionScoped
      @Named
      public class Test implements Serializable {
      
           @Inject
           private Person person;
      
           @PostConstruct
           void initialize() {
                person = new Person();
           }
      
           @Named
           @Produces
           @SessionScoped
           public Person getPerson() {
                return person;
           }
      
           public void setPerson(Person person) {
                this.person = person;
           }
      
           public String zeigeAusgewaehltePerson() {
                System.out.println(this.person);
                return null;
           }
      
      }
      



      Bean Person:




      package domain;
      
      import java.io.Serializable;
      
      import javax.persistence.Entity;
      import javax.persistence.GeneratedValue;
      import javax.persistence.Id;
      
      import org.jboss.seam.solder.core.Veto;
      
      @Veto
      //@Entity
      public class Person implements Serializable {
      
      //     @Id
      //     @GeneratedValue
           private Long id;
           private String nachname;
           private String vorname;
      
           public void setNachname(String nachname) {
                this.nachname = nachname;
      
           }
      
           public String getNachname() {
                return nachname;
           }
      
           public void setVorname(String vorname) {
                // TODO Auto-generated method stub
                this.vorname = vorname;
           }
      
           public String getVorname() {
                return vorname;
           }
      
           @Override
           public String toString() {
                return "Person [id=" + id + ", nachname=" + nachname + ", vorname="
                          + vorname + "]";
           }
           
           
      
      }
      



      index.xhtml



      <?xml version="1.0" encoding="UTF-8"?>
      <!DOCTYPE html
          PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
          "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
      <html xmlns="http://www.w3.org/1999/xhtml"
           xmlns:a4j="http://richfaces.org/a4j"
           xmlns:ui="http://java.sun.com/jsf/facelets"
           xmlns:h="http://java.sun.com/jsf/html"
           xmlns:f="http://java.sun.com/jsf/core"
           xmlns:p="http://primefaces.org/ui"
           xmlns:rich="http://richfaces.org/rich">
      <h:head>
      </h:head>
      <h:body>
           <h:form>
                <h:inputText value="#{person.nachname}" />
                <h:commandButton action="#{test.zeigeAusgewaehltePerson}"
                     value="Klick">
                </h:commandButton>
                <h:panelGroup id="out">
                     <h:outputText value="#{person.nachname}" />
                </h:panelGroup>
           </h:form>
      </h:body>
      </html>



        • 1. Re: Newbee question concerning injection
          ssachtleben.ssachtleben.gmail.com

          Remove injection here:


          //@Inject
          private Person person;



          If it doesn't work try to set break point on getter and setter for person on test bean.

          • 2. Re: Newbee question concerning injection
            ben_utzer

            Unfortunately Eclipse complains that it is unable to set the breakpoints in the weld proxy of the test bean due to missing line number information.


            The output for nachname is still null.

            • 3. Re: Newbee question concerning injection
              ben_utzer

              When I change




              @PostConstruct
                   void initialize() {
                        person = new Person();
                   }



              to




              @PostConstruct
                   void initialize() {
                        person = new Person();
                              person.setNachname("Mustermann");
                              person.setVorname("Max");
                   }




              I get




              Person [id=null, nachname=, vorname=Max]

              • 4. Re: Newbee question concerning injection
                ssachtleben.ssachtleben.gmail.com

                Something seems really messed up. Never had this error message while debugging in JEE6.


                Maybe upload your project as zip somewhere and I will have a look at it.

                • 5. Re: Newbee question concerning injection
                  ben_utzer

                  Of course, here you go. Thanks for your effort. I tested on both AS 7.0.1.Final and 7.1.0.Alpha1-SNAPSHOT.

                  • 6. Re: Newbee question concerning injection
                    ssachtleben.ssachtleben.gmail.com

                    Please follow these steps:


                    1) Goto project properties


                    2) Goto and enable project facets


                    3) Tick Dynamic Web Module, CDI, Java, JavaServer Faces


                    4) Switch on the right side to runtimes and tick Jboss AS 7 Runtime


                    5) Go back to project properties


                    6) Goto deployment assembly


                    7) Add: Java Build Path Entries - Maven Dependencies (if not exists)


                    8) Add: src/main/webapp path (if not exists)


                    9) Now you can drag and drop the project to your Jboss AS 7 on server view


                    10) Check once if all exported properly to deployments path of JBoss Runtime


                    ------


                    Now its properly set up. If you start your application you should see this:


                    16:31:05,141 INFO  [org.jboss.weld] (MSC service thread 1-9) Processing CDI deployment: seam-sandbox.war
                    16:31:05,232 INFO  [org.jboss.weld] (MSC service thread 1-3) Starting Services for CDI deployment: seam-sandbox.war
                    16:31:05,237 INFO  [org.jboss.weld] (MSC service thread 1-3) Starting weld service



                    and finally I got:


                    16:32:15,329 INFO  [stdout] (http--127.0.0.1-8080-1) Person [id=null, nachname=Tester, vorname=null]
                    




                    Good luck :)


                    • 7. Re: Newbee question concerning injection
                      ben_utzer

                      Perfect! Thanks a lot. Though I'm still uncertain about what exactly caused the problem. I didn't have cdi and jsf facet enabled the rest was correctly setup. what magic does adding those facets do?