Results 1 to 6 of 6

Thread: Injecting objects of type class

  1. #1
    Join Date
    Oct 2009
    Posts
    21

    Default Injecting objects of type class

    How do I inject a class into an object? If the message signature was:

    Code:
    public void setFooBar(Class class)
    then spring would resolve it correctly from the following config:

    Code:
    <property name="fooBar" value="com.foo.bar.Cow"/>
    In my scenario the method signature is:

    Code:
    public void setFooBar(Object obj)
    what would the corresponding configuration be?

  2. #2
    Join Date
    Jan 2006
    Location
    Seattle, Washington
    Posts
    467

    Default

    I'm not sure what you're asking. You asked how to inject a class object, and then you showed how that is done, then you seemed to ask how to inject a normal instance of a class. If you're trying to inject a value for "FooBar", then just declare a bean of a particular class and use the "ref" attribute of the "property" element to refer to that bean.

  3. #3
    Join Date
    Oct 2009
    Posts
    21

    Default

    Quote Originally Posted by dkarr View Post
    I'm not sure what you're asking. You asked how to inject a class object, and then you showed how that is done, then you seemed to ask how to inject a normal instance of a class. If you're trying to inject a value for "FooBar", then just declare a bean of a particular class and use the "ref" attribute of the "property" element to refer to that bean.
    I want to inject Cow.class, not an instance of type 'Cow'

    In the 1st example spring can determine that the value 'com.foo.bar.Cow' should be converted to an object to type 'Class' since the parameter to the method fooBar is defined as type 'Class'.

    In the 2nd example the parameter is defined as type 'Object', so spring doesn't know what type the value 'com.foo.bar.Cow' should be converted to - I assume spring just converts it to a String.

    How can I force spring to convert 'com.foo.bar.Cow' to the type Class instead of a String? The only mechanism I can think of is to configure Spring to invoke the static method 'Class.forName(...)' and pass the result of that to the fooBar method - but that is extremely messy and makes the configuration unreadable

  4. #4
    Join Date
    Jan 2006
    Location
    Seattle, Washington
    Posts
    467

    Default

    Ah, ok. I don't know if there's a better way to do this, but perhaps you should define a bean with a single property of type class, with that given value. We'll call the bean "classHolder" and the property "clazz".

    Then, you have the other bean with the property of type Object. The value of the property will be "#{classHolder.clazz}". This uses the Spring Expression Language. I have not tried this, but I've reasoned through what the doc says, and this seems logical.

    This obviously requires a little more than you wanted. I wouldn't be surprised if there's a simpler way to do this.

  5. #5
    Join Date
    Oct 2009
    Posts
    21

    Default

    Haven't looked at the expression language yet.

    Below is another possible soln using the proxy factory mechanism - but does anyone know of a solution out of the box?

    Code:
    class BeanClassFactory implements FactoryBean
    {
              Class c;
    
               public void setClazz(Class c) {
                     this.c = c;
              }
    
               public Object getObject() {
                     return c;
               }
    
               public Class getObjectType() {
                     return Class.class;
               }
    }

  6. #6
    Join Date
    Oct 2009
    Posts
    21

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
  •