Results 1 to 6 of 6

Thread: How do you create a .class bean (not an instance)

  1. #1
    Join Date
    Sep 2004
    Posts
    602

    Default How do you create a .class bean (not an instance)

    I want to create an instance of org.springframework.jdbc.core.BeanPropertyRowMappe r that takes a class as its constructor

    e.g. new BeanPropertyRowMapper(Address.class)

    How do I do this in a Spring config ?

    I'm a bit stumped on how to create a bean containing Address.class, as I am so used to creating singletons of instances of classes.

    Code:
    <bean id="addressRowMapper"
    		class="org.springframework.jdbc.core.BeanPropertyRowMapper">
    		<constructor-arg >
    ????? What goes here ???????
    		</constructor-arg >
    
    	</bean>
    Last edited by Paul Newport; Jul 14th, 2008 at 06:04 AM. Reason: added in code

  2. #2
    Join Date
    Sep 2004
    Posts
    602

    Default

    PS this works but I was hoping there is a more graceful way of doing it:

    Code:
    <bean id="address"
    		class="net.targetgroup.batch.validator.Address" />
    
    	<bean id="addressClass" factory-bean="address"
    		factory-method="getClass" />

  3. #3
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,624

    Default

    It will work out of the box. Spring will do the conversion.

    Code:
    <bean id="addressRowMapper" class="org.springframework.jdbc.core.BeanPropertyRowMapper">
      <constructor-arg value="net.targetgroup.batch.validator.Address" />
    </bean>
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  4. #4
    Join Date
    Sep 2004
    Posts
    602

    Default

    Quote Originally Posted by Marten Deinum View Post
    It will work out of the box. Spring will do the conversion.

    Code:
    <bean id="addressRowMapper" class="org.springframework.jdbc.core.BeanPropertyRowMapper">
      <constructor-arg value="net.targetgroup.batch.validator.Address" />
    </bean>
    Oh, that is annoyingly magic ;-)

    Thanks !

    PS can you point me in the direction of the documentation that explains how the magic works ?

  5. #5
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,624

    Default

    Spring uses PropertyEditors for that (just like the Spring MVC stuff). It inspects the target type (in your case a Class) and tries to find a PropertyEditor for that. Spring has several shipped out of the box.

    Like converting a String to an Array or List or Set. A Properties object into a Map and vice versa and also one for a String to Class conversion.

    That is explained in the reference guide Chapter 3.3.2.1.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  6. #6
    Join Date
    Sep 2004
    Posts
    602

    Default

    Quote Originally Posted by Marten Deinum View Post
    Spring uses PropertyEditors
    I guessed it might be proprty editors; that is most cool. Thanks again.

Posting Permissions

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