Announcement Announcement Module
Collapse
No announcement yet.
Injecting objects of type class Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

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

    Comment


    • #3
      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

      Comment


      • #4
        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.

        Comment


        • #5
          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;
                     }
          }

          Comment


          • #6
            Found the answer:

            http://forum.springsource.org/showth...ht=Class.class

            Comment

            Working...
            X