Announcement Announcement Module
Collapse
No announcement yet.
Access service or DAO beans from a GenericConverter Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Access service or DAO beans from a GenericConverter

    I want to make a converter similar to IdToEntityConverter, but instead of calling a find method from entity class, I want it to call find method from a specific DAO (annotated with @Repository) or service class (annotated with @Service). I created my converter that extends ConditionalGenericConverter, and registered it with a default ConversionService.
    But the problem is that I don't know how to get DAO or service beans from my converter implementation ? I tried autowireing the ApplicationContext and calling the getBean method but those beans are not in that context. Any suggestions ?

  • #2
    I just autowire my service into my converters and it works fine.

    Comment


    • #3
      Originally posted by MartyJones View Post
      I just autowire my service into my converters and it works fine.
      Yes, that works, but I don't know which service I need - it's a universal converter, so I need to find a service bean by name.

      Comment


      • #4
        Try making the generic converter implement the ApplicationContextAware interface. You will then get a reference to the application context. You should be able to get your service bean by using applicationContext.getBean("");

        Comment


        • #5
          Originally posted by MartyJones View Post
          Try making the generic converter implement the ApplicationContextAware interface. You will then get a reference to the application context. You should be able to get your service bean by using applicationContext.getBean("");
          I tried that, but it the same as autowiring the ApplicationContext. getBean(name) can't find those beans ...

          Comment


          • #6
            Can you give me a brief description of how you have your configuration setup? Do you have a application-context.xml and dispatcher-servlet.xml? It may be easier to just post your xml configuration files.

            Marty

            Comment


            • #7
              It's configured pretty much in the same way like all roo-generated projects ... So it's based on Spring 3. Entities have @Entity, DAOs @Repository and service classes @Service. Here are the parts of config files:

              applicationContext.xml

              Code:
              <context:property-placeholder location="classpath*:META-INF/spring/*.properties"/>
              
              <context:spring-configured/>
              
              <context:component-scan base-package="com.somepackage">
                      <context:exclude-filter expression="org.springframework.stereotype.Controller" type="annotation"/>
              </context:component-scan>
              
              ... data source, transaction manager etc.

              webmvc-config.xml

              Code:
              <context:component-scan base-package="rs.wm.appstore.server" use-default-filters="false">
              		<context:include-filter expression="org.springframework.stereotype.Controller" type="annotation"/>
              </context:component-scan>
              
              <mvc:annotation-driven conversion-service="conversionService" />
              	
              <bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean">
                  	<property name="converters">
                      	<list>
                          	<bean class="rs.wm.appstore.server.core.IdToEntityConverterWithRegistrySupport" />
                      	</list>
                  	</property>
              </bean>
              
              <mvc:default-servlet-handler/>
              
              ... mvc interceptors, error handler, viewresolver, tilesview, etc.
              And I'm trying to access the beans from IdToEntityConverterWithRegistrySupport class.

              Comment


              • #8
                It looks like you are loading your controller classes twice by the look of you configuration:

                Change the application-context.xml section:

                Code:
                <context:component-scan base-package="com.somepackage">
                    <context:exclude-filter expression="org.springframework.stereotype.Controller" type="annotation"/>
                </context:component-scan>
                to

                Code:
                <context:component-scan base-package="com.somepackage">
                    <context:include-filter type="annotation" expression="org.springframework.stereotype.Repository"/>
                    <context:include-filter type="annotation" expression="org.springframework.stereotype.Service"/>
                </context:component-scan>
                This way you are loading your dao and service classes in the application context and not the dispatcher servlet context.

                You should be able to get access to the service beans then.

                Comment


                • #9
                  They are not loading twice because in one configuration there is an exclude filter and in another it's not loading the defaults, only Controllers.

                  I found a solution:

                  In application context I define my converter that uses it's own conversion service:

                  Code:
                  <bean id="genericConversionService" class="org.springframework.context.support.ConversionServiceFactoryBean" />
                  	
                  <bean id="idToEntityConverterWithRegistrySupport" class="rs.wm.appstore.server.core.spring.IdToEntityConverterWithRegistrySupport">
                  	<constructor-arg ref="genericConversionService" />
                  </bean>
                  And in web context the converter is added to converters of conversion service that is used by Spring:

                  Code:
                  <mvc:annotation-driven conversion-service="conversionService" />
                  	
                  <bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean">
                  	<property name="converters">
                  		<list>
                  			<ref bean="idToEntityConverterWithRegistrySupport" />
                  			</list>
                  	</property>
                  </bean>

                  Comment


                  • #10
                    Hello joshefin,

                    then how do you use this converter? How do you assign the correct DAO to it?

                    Probably some source-code could be useful?

                    R,

                    triplem

                    Comment

                    Working...
                    X