Results 1 to 10 of 10

Thread: Access service or DAO beans from a GenericConverter

  1. #1
    Join Date
    Nov 2010
    Posts
    10

    Default 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. #2
    Join Date
    Oct 2005
    Location
    Mobile, AL
    Posts
    345

    Default

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

  3. #3
    Join Date
    Nov 2010
    Posts
    10

    Default

    Quote 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.

  4. #4
    Join Date
    Oct 2005
    Location
    Mobile, AL
    Posts
    345

    Default

    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("");

  5. #5
    Join Date
    Nov 2010
    Posts
    10

    Default

    Quote 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 ...

  6. #6
    Join Date
    Oct 2005
    Location
    Mobile, AL
    Posts
    345

    Default

    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

  7. #7
    Join Date
    Nov 2010
    Posts
    10

    Default

    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.

  8. #8
    Join Date
    Oct 2005
    Location
    Mobile, AL
    Posts
    345

    Default

    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.

  9. #9
    Join Date
    Nov 2010
    Posts
    10

    Default

    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>

  10. #10
    Join Date
    Aug 2008
    Posts
    14

    Default

    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

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •