Spring syntax for setting a Class object?

后端 未结 4 1580
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-01 09:57

Is there a way to set a property in spring to, not an instance of a class, but the class object itself? i.e.

Rather than


   

        
相关标签:
4条回答
  • 2021-01-01 10:35

    No. With a bean tag you instruct Spring on how to instantiate a class.

    0 讨论(0)
  • 2021-01-01 10:44
    <bean>
       <property name="x">
          <value type="java.lang.Class">a.b.c.Foo</value>
       </property>
     </bean>
    

    That should work.

    0 讨论(0)
  • 2021-01-01 10:45

    You could certainly use the static factory method Class.forName(), if there's no more elegant syntax (and I don't believe there is):

    <property name="x">
       <bean class="java.lang.Class" factory-method="forName">
          <constructor-arg value="a.b.c.Foo"/>
       </bean>
    </property>
    
    0 讨论(0)
  • 2021-01-01 10:45

    Would <property name="x" class="a.b.c.Foo.class"> work? That should be an instance of a Class object...

    0 讨论(0)
提交回复
热议问题