9 Replies Latest reply: May 4, 2012 7:52 PM by Steven Boscarine RSS

    Spring2.5 Component Scan Not Working in JB 7AS

    Deepak S Newbie

      Hello Folks,

       

      I'm using JBoss 7 AS(jboss-as-7.1.0.Final) as my server.

       

      I have 2 questions...

       

      1.  I'm using Eclipse Helios SR2.  I'm not able to get the proper pluging to have JBOSS AS as my server in eclipse.

           I got several sites in net for the plugin, but when i tried to install it says conflicting, and it closes.

           Can i add Jboss 7 AS to my eclipse ?

       

       

      2.  I'm using spring 2.5, JSF and hibernate application. 

           The problem is i have used annotation for component and repository  but it is not getting initialized.   Basically  spring component scanner is not working.

          

           Please find the code snippets:

       

           Web.xml:    

      <?xml version="1.0" encoding="UTF-8"?>
      <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xmlns="http://java.sun.com/xml/ns/javaee" 
          xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
            http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
          version="2.5">
      
          <listener>
              <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
          </listener>
      
          <listener>
            <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
          </listener>
          
          <context-param>
              <param-name>contextConfigLocation</param-name>
              <param-value>/WEB-INF/applicationContext.xml</param-value>
          </context-param>
      

       

      applicationContext.xml

      <?xml version="1.0" encoding="UTF-8"?>
      <beans xmlns="http://www.springframework.org/schema/beans"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xmlns:context="http://www.springframework.org/schema/context"
             xsi:schemaLocation="http://www.springframework.org/schema/beans 
                 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
                 http://www.springframework.org/schema/context
                 http://www.springframework.org/schema/context/spring-context-3.0.xsd">
                     
          <context:component-scan base-package="main.controllerbeans" />
          <context:annotation-config />
          
          <bean id="propertyPlaceholderConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
              <property name="locations">
                  <list><value>/WEB-INF/configuration.properties</value></list>
              </property>
          </bean>
          
          <!-- IMPORTING HIBERNATE SETTINGS -->
          <import resource="/db-config.xml"/>
      

      Component scan is not working... it seems

       

       

      db-config.xml

      <?xml version="1.0" encoding="UTF-8"?>
      <beans xmlns="http://www.springframework.org/schema/beans"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
          xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
          xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
          http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
          http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
      
          <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
              <property name="driverClass">
                  <value>${jdbc.driver.className}</value>
              </property>
      
              <property name="jdbcUrl">
                  <value>${jdbc.url}</value>
              </property>
      
              <property name="user">
                  <value>${jdbc.username}</value>
              </property>
      
              <property name="password">
                  <value>${jdbc.password}</value>
              </property>
          </bean>
      
          <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
      
              <property name="dataSource">
                  <ref bean="dataSource" />
              </property>
      
              <!-- <property name="packagesToScan" value="main.dbentities" /> -->
              
              <property name="hibernateProperties">
                  <props>
                      <prop key="hibernate.dialect">${jdbc.hibernate.dialect}</prop>
                      <prop key="hibernate.show_sql">false</prop>
                  </props>
              </property>
              
              <property name="packagesToScan" value="org.adit.spring.hibernate.entity" />
          
               
          </bean>
      
          <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
      
              <property name="sessionFactory">
                  <ref bean="sessionFactory" />
              </property>
      
          </bean>
      
          <context:annotation-config />
          <tx:annotation-driven />
          
          
          
      
      </beans>
      

       

       

      faces-config.xml

      <?xml version="1.0"?>
      <faces-config xmlns="http://java.sun.com/xml/ns/javaee" 
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
            http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd"
         version="2.0">
      
          <application>
               <el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-resolver>
          </application>
      

       

      NotificationBean.java

      import java.io.Serializable;
      
      import javax.faces.bean.RequestScoped;
      
      import main.dao.NotificationDAO;
      import main.dbentities.Notification;
      
      import org.springframework.beans.factory.annotation.Autowired;
      import org.springframework.stereotype.Component;
      
      @Component("notificationBean")
      @RequestScoped
      public class NotificationBean implements Serializable {
          
          private static final long serialVersionUID = -3637354326738964505L;
      
          @Autowired
          Notification notification;
          
          @Autowired
          public NotificationDAO notificationDao;
          
          public NotificationBean() {
              notification = new Notification();
              //notificationDao = new NotificationDaoImpl();
          }
      
          public void persist(){
              System.out.println("Persist Method");
              notification.setUserId(111);
              notification.setPriorityId(1);
              notificationDao.save(notification);
          }
      

       

      In my xhtml page i have a  input text box:

      <h:inputText id="englishMessageTitle" value="#{notificationBean.notification.notificationTitleEng}" />

       

      And there is submit button:

      <h:commandButton class="btn_empty" id="submit" value="#{msg['CreateNotification.Submit']}" action="#{notificationBean.persist}" />

       

      I'm getting the follwoing exception:

       

      javax.el.PropertyNotFoundException: /xhtml/postmessage.xhtml @36,111 value="#{notificationBean.notification.notificationTitleEng}": Target Unreachable, identifier 'notificationBean' resolved to null
       at com.sun.faces.facelets.el.TagValueExpression.getType(TagValueExpression.java:100)
       at com.sun.faces.renderkit.html_basic.HtmlBasicInputRenderer.getConvertedValue(HtmlBasicInputRenderer.java:95)
       at javax.faces.component.UIInput.getConvertedValue(UIInput.java:1030)
       at javax.faces.component.UIInput.validate(UIInput.java:960)
       at javax.faces.component.UIInput.executeValidate(UIInput.java:1233)
       at javax.faces.component.UIInput.processValidators(UIInput.java:698)
      
      

       

      Its unable to resolve the bean notificationBean

       

      Following are the Jars that i have in my web-inf/lib:

      c3p0-0.9.1.2.jar

      commons-collections-3.2.1.jar

      commons-dbcp-1.4.jar

      dom4j-1.6.1.jar

      hibernate-3.2.1.ga.jar

      hibernate-annotations-3.4.0.GA.jar

      hibernate-commons-annotations-3.1.0.GA.jar

      hibernate-core-3.3.2.GA.jar

      jboss-el-2.0.1.GA.jar

      jsf-api.jar

      jsf-impl.jar

      jsp-2.1-6.0.2.jar

      jstl-api-1.2.jar

      jstl-impl-1.2.jar

      mysql-connector-java-3.0.17-ga-bin.jar

      org.springframework.context-2.5.6.SEC01.jar

      org.springframework.core-2.5.6.SEC01.jar

      org.springframework.transaction-2.5.6.A.jar

      persistence-api-1.0.jar

      spring-2.5.6.SEC01.jar

      spring-beans-2.5.6.SEC01.jar

       

       

      Please let me know what is the problem, and let me know if u need any other information.

        • 1. Re: Spring2.5 Component Scan Not Working in JB 7AS
          Deepak S Newbie

          No Reply even after 60+ views...  

           

          Help me out...

          • 2. Re: Spring2.5 Component Scan Not Working in JB 7AS
            jaikiran pai Master

            Deepak S wrote:

             

             

            1.  I'm using Eclipse Helios SR2.  I'm not able to get the proper pluging to have JBOSS AS as my server in eclipse.

                 I got several sites in net for the plugin, but when i tried to install it says conflicting, and it closes.

                 Can i add Jboss 7 AS to my eclipse ?

             

            You should ask that specific question here https://community.jboss.org/en/tools?view=discussions

            • 3. Re: Spring2.5 Component Scan Not Working in JB 7AS
              Steven Boscarine Apprentice

              Hello Deepak,

              I am porting a legacy app to JBoss AS7 and ran into the exact same issue.  Spring 3.1 works great, but you're right 2.5 doesn't work.  I even created an ultra-tiny demo with 1 class to rule out the rest of my app being a culprit in breaking it.

               

              I have no idea if the bug is because of Servlet 3.0 or AS7.  However, it definitely doesn't work. 

               

              I just upgraded my version of Spring...the POM became messy, but it seems to work well so far.   It hasn't gone through a full QA cycle, though.

               

              Thanks,
              Steven

               

              If anyone wants help reproducing this issue, create a project with the archetype and add:

               

              package foo;
              import java.util.Date;
              
              import javax.annotation.PreDestroy;
              
              import org.apache.commons.logging.Log;
              import org.apache.commons.logging.LogFactory;
              import org.springframework.stereotype.Component;
              /**
               * Tracing class designed to output when constructed.
               */
              @Component
              public class ChatterBox
              {
                private final Log logger = LogFactory.getLog(getClass());
                public ChatterBox()
                {
                  // we want this to be very visible and easy to read.
                  logger.fatal("\n\n\n\n\nSingleton was constructed at " + new Date() + " (you should only see me once)\n\n\n\n");
                }
              
                @PreDestroy
                protected void finalize2()
                {
                  logger.warn("\n\nI was destroyed by @PreDestory on " + new Date() + "\n\n");
                }
              }
              

              Create a Spring config:

               

              <?xml version="1.0" encoding="UTF-8"?>
              <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                  xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
                  xsi:schemaLocation="http://www.springframework.org/schema/beans 
                         http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
                         http://www.springframework.org/schema/context
                         http://www.springframework.org/schema/context/spring-context-2.5.xsd">
                  <!-- Activates annotation-based bean configuration? --I think this is optional, but better safe than sorry -->
                  <context:annotation-config />
                  <!-- Fails...never is ran in 2.5, but ran correctly in 3.1. -->
                  <context:component-scan base-package="foo" />
                  <!-- Works when uncommented. -->
              <!--     <bean class="foo.ChatterBox"/> -->
              </beans>
              

              Update web.xml:

               

              <web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
              
                  <description>Our canary in a coal mine - Can we get Spring working in JBoss AS7?</description>
                  <display-name>canary</display-name>
                  <context-param>
                      <param-name>contextConfigLocation</param-name>
                      <param-value>classpath:/applicationContext.xml</param-value>
                  </context-param>
                  <listener>
                      <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
                  </listener>
              </web-app>
              

              Add the following to the pom.xml:

              <dependency>
                  <groupId>org.springframework</groupId>
                  <artifactId>spring</artifactId>
                  <version>2.5.6</version>
              </dependency>
              <dependency>
                  <groupId>commons-logging</groupId>
                  <artifactId>commons-logging</artifactId>
                  <version>1.1</version>
              </dependency>
              <dependency>
                  <groupId>log4j</groupId>
                  <artifactId>log4j</artifactId>
                  <version>1.2.15</version>
              </dependency>
              

               

              At this point, you'll see output in the console that the spring bean is being registered.  This works in 3.1 and fails in 2.5.6.

              • 4. Re: Spring2.5 Component Scan Not Working in JB 7AS
                jaikiran pai Master

                I've no clue about Spring. So one of you will have to explain what exactly is not working. Is that class annotated with @Component not being scanned for that annotation or is it something else? And what does the application packaging look like?

                • 5. Re: Spring2.5 Component Scan Not Working in JB 7AS
                  Tomaz Cerar Master

                  Have you tried using snowdrop extension for as7?

                   

                  http://www.jboss.org/snowdrop

                   

                  anything more in detail probably snowdrop guys can answer.

                   

                   

                  --

                  tomaz

                  • 6. Re: Spring2.5 Component Scan Not Working in JB 7AS
                    Stephen Coy Master

                    Spring 2.5.x has known issues with JBoss VFS, which were resolved in Spring 3.0.

                     

                    Spring 2.5 + JBoss >= 5.0  => no go

                     

                    https://jira.springsource.org/browse/SPR-6146

                    https://jira.springsource.org/browse/SPR-5120

                     

                    Historically, Spring Framework's backward compatibility has been really excellent, so just upgrade it.

                    • 7. Re: Spring2.5 Component Scan Not Working in JB 7AS
                      Philippe Marschall Apprentice

                      For Spring 2.5 you need a VFS-enabled Application Context. This is no longer needed for Spring 3.0+.

                      • 8. Re: Spring2.5 Component Scan Not Working in JB 7AS
                        xsa lefter Newbie

                        @Steven,

                         

                        How do you make it works? I'm still got this issue even when use spring 3.1 with jboss as 7. Did you use snowdrop extension? My configucation is exactly same with described by Mariuus here.

                        • 9. Re: Spring2.5 Component Scan Not Working in JB 7AS
                          Steven Boscarine Apprentice

                          Hmm, simply changing Spring fixed the error, but I haven't yet completed the final step of the full wiring and proving the UI is viewing the component correctly.  We're not using MVC.  I can see through debugging that the container is initializing the beans correctly, simply by upgrading the library, though.  If I find that additional work needs to be done to get the UI to read the beans, I'll update this post.