Announcement Announcement Module
Collapse
No announcement yet.
Problems whit injection using converters and getHibernateTemplate method Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Problems whit injection using converters and getHibernateTemplate method

    Hi peoples,
    I've some troubles with a custom converter using:
    Spring MVC 2.5.6
    Hibernate 3
    Spring WebFlow 2.0.8
    Tiles 2.0.7

    I overrided the toObject method from my class StringToDescriptions to retrieve the object from my database but I receive a java.lang.IllegalArgumentException: 'sessionFactory' or 'hibernateTemplate' is required exception.
    This is my StringToDescriptions class:

    Code:
    @Component
    public class StringToDescriptions extends StringToObject {
    
        @Autowired
        private DescriptionsManager descriptionsManager;
    
        public StringToDescriptions() {
            super(Descriptions.class);
    
        }
        
        public StringToDescriptions(DescriptionsManager descriptionsManager) {
            super(Descriptions.class);
            this.descriptionsManager = descriptionsManager;      
        }
        
        
    
        @Override
        protected Object toObject(String string, Class targetClass) throws Exception {        
            return descriptionsManager.retrieveFromString(string);
        }
    
        @Override
        protected String toString(Object object) throws Exception {
            Descriptions descriptions = (Descriptions) object;
            return descriptions.getCode() + " - " + descriptions.getName();
        }
    }
    According to this thread, I defined my ConversionService like this:

    Code:
    @Component("conversionService")
    public class ApplicationConversionService extends DefaultConversionService {     
    
        public ApplicationConversionService() {
        }
    
        public ApplicationConversionService(DescriptionsManager descriptionsManager) {
            addConverter(new StringToDescriptions(descriptionsManager));
        }
    }
    DescriptionsManager is a service class for retrieve objects from my database:

    Code:
    public class DescriptionsManager extends HibernateDaoSupport implements java.io.Serializable {    
    
        public Descriptions retrieveFromString(String string) {        
            int index = string.indexOf("-");
            String code = string.substring(0, index);                     
            DetachedCriteria criteria = DetachedCriteria.forClass(Descriptions.class);        
            criteria.add(Restrictions.eq("code", code));        
            return (Descriptions) DataAccessUtils.uniqueResult(getHibernateTemplate().findByCriteria(criteria));
        }
    }
    finally this is my flow:

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <flow xmlns="http://www.springframework.org/schema/webflow"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://www.springframework.org/schema/webflow http://www.springframework.org/schema/webflow/spring-webflow-2.0.xsd">
            
           <persistence-context />
    
           <var name="descriptionsManager" class="model.DescriptionsManager" />       
    
           <view-state id="create" model="ecg" view="/WEB-INF/jsp/ecg_form.jsp">
               <binder>               
                   <binding property="ritmo" converter="descriptionsToString(descriptionsManager)" required="true" />
                </binder>          
               <transition on="confirm" to="ecgConfirmed">
                   <evaluate expression="ecgManager.saveECG(ecg)"/>
               </transition>
               <transition on="cancel" to="cancel" bind="false" />
           </view-state>
    
           <end-state id="cancel" />
    
           <end-state id="ecgConfirmed" commit="true" view="/WEB-INF/jsp/success.jsp" />
    
    </flow>
    I think I've an injection problem in the
    Code:
    <var name="descriptionsManager" class="model.DescriptionsManager" />
    line, cause when i call the jsp page, spring give me the java.lang.IllegalArgumentException: 'sessionFactory' or 'hibernateTemplate' is required exception...

    Any idea??
    Last edited by MarcoS; Dec 8th, 2009, 01:28 PM.

  • #2
    I include my configurations files:

    applicationContext.xml

    Code:
    <?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:aop="http://www.springframework.org/schema/aop"
           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/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
        
    
        <bean id="dataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">
            <property name="driverClassName" value="org.postgresql.Driver" />
    	<property name="url" value="jdbc:postgresql://localhost:5432/mydb" />
    	<property name="username" value="postgres" />
    	<property name="password" value="postgres" />
        </bean>
    
        <!-- ADD PERSISTENCE SUPPORT HERE (jpa, hibernate, etc) -->
    
        <bean id="sessionFactory"
        class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
            <property name="dataSource" ref="dataSource"></property>
            <property name="mappingResources">
                <list>
                    <value>model/Descriptions.hbm.xml</value>
                </list>
            </property>
    	<property name="hibernateProperties">
                <props>
                    <prop key="hibernate.dialect">
                        org.hibernate.dialect.PostgreSQLDialect
                    </prop>
                    <prop key="show_sql">true</prop>                
                </props>
    	</property>
        </bean>    
    
        <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
                <property name="sessionFactory" ref="sessionFactory" />
        </bean>    
    
    </beans>
    and dispatcher-servlet.xml

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:context="http://www.springframework.org/schema/context"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:p="http://www.springframework.org/schema/p"
           xmlns:webflow="http://www.springframework.org/schema/webflow-config"
           xmlns:aop="http://www.springframework.org/schema/aop"
           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/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
           http://www.springframework.org/schema/webflow-config http://www.springframework.org/schema/webflow-config/spring-webflow-config-2.0.xsd
           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">
    
        <context:component-scan base-package="controller"/>
        <context:component-scan base-package="converter"/>
    
        <!-- Maps request paths to flows in the flowRegistry; e.g. a path of /hotels/booking looks for a flow with id "hotels/booking" -->
        <bean class="org.springframework.webflow.mvc.servlet.FlowHandlerMapping">
            <property name="flowRegistry" ref="flowRegistry" />
            <property name="defaultHandler">
                <!-- If no flow match, map path to a view to render; e.g. the "/intro" path would map to the view named "intro" -->
                <bean class="org.springframework.web.servlet.mvc.UrlFilenameViewController" />
            </property>
        </bean>
    
    
        <bean id="tilesViewResolver" class="org.springframework.js.ajax.AjaxUrlBasedViewResolver">
            <property name="viewClass" value="org.springframework.webflow.mvc.view.FlowAjaxTilesView"/>
        </bean>
    
        <bean id="flowController" class="org.springframework.webflow.mvc.servlet.FlowController">
            <property name="flowExecutor" ref="flowExecutor"/>
        </bean>
    
        <!-- Registers the web flows that can be executed -->
        <webflow:flow-registry id="flowRegistry" flow-builder-services="flowBuilderServices">
            <webflow:flow-location-pattern value="/WEB-INF/flows/**/*.xml" />
        </webflow:flow-registry>
    
        <!-- Configures the engine that executes web flows in this application -->
        <webflow:flow-executor id="flowExecutor" flow-registry="flowRegistry" />
        
        <!-- Custom converters -->
        <webflow:flow-builder-services id="flowBuilderServices" conversion-service="conversionService" />
    
        <!--
        If you provide a custom HandlerAdapter (such as the AnnotationMethodHandlerAdapter),
        you will lose the standard ones (which are listed in DispatcherServlet.properties).
        So the solution is to add the SimpleControllerHandlerAdapter to the dispatcher-servlet.xml
        -->
        <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter" />
    
        <!-- Enables web flows; responsible for calling the Spring Web Flow system to execute a flow when one is mapped. -->
        <bean class="org.springframework.webflow.mvc.servlet.FlowHandlerAdapter">
            <property name="flowExecutor" ref="flowExecutor" />
        </bean>
    
        <bean id="tilesConfigurer" class="org.springframework.web.servlet.view.tiles2.TilesConfigurer">
            <property name="definitions">
                <list>
                    <value>/WEB-INF/tiles-def.xml</value>
                </list>
            </property>
        </bean>    
        
        <!--
        Most controllers will use the ControllerClassNameHandlerMapping above, but
        for the index controller we are using ParameterizableViewController, so we must
        define an explicit mapping for it.
        -->
        <bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
            <property name="mappings">
                <value>
                    index.htm=indexController                
                    /**/*=flowController
                </value>
            </property>
        </bean>
    
        <bean id="viewResolver"
              class="org.springframework.web.servlet.view.InternalResourceViewResolver"
              p:prefix="/WEB-INF/jsp/"
              p:suffix=".jsp" />   
     
        
        <!--
        The index controller.
        -->
        <bean name="indexController"
              class="org.springframework.web.servlet.mvc.ParameterizableViewController"
              p:viewName="index" />
    
        <!-- MODEL -->
    
            <!-- MANAGERS -->    
    
        <bean id="descriptionsManager" class="model.DescriptionsManager">
            <property name="sessionFactory" ref="sessionFactory" />
        </bean>           
            
            <!-- BEANS -->    
    
        <bean id="descriptions" class="model.Descriptions" />
            
           
        <bean id="conversionService" class="controller.ApplicationConversionService" />     
       
    </beans>

    Comment


    • #3
      Ok, I solved it.

      No need to specify a

      Code:
       
      <var name="descriptionsManager" class="model.DescriptionsManager" />
      in flow.xml and just call the conversation like this:

      Code:
      <binder>
          <binding property="ritmo" converter="descriptionsToString" required="true" />
      </binder>
      without any extra parameter

      Comment

      Working...
      X