用于设置Class对象的Spring语法? [英] Spring syntax for setting a Class object?

查看:70
本文介绍了用于设置Class对象的Spring语法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法在spring中设置属性,而不是类的实例,而是类对象本身?即

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

而不是

<bean>
   <property name="prototype" class="a.b.c.Foo">...

给你一个Foo的实例,类似于:

giving you an instance of "Foo", something like:

<bean>
  <property name="prototype" class="java.lang.Class" value="a.b.c.Foo.class"...

编辑:
最佳(工作)解决方案到目前为止 - 使用正常的实例化并在setter中派生类。在解决方案方面,我认为我们将其描述为作弊:

edit: best (working) solution so far - use the normal instantiation and derive the class in the setter. In terms of solutions I think this we'd describe this as "cheating":

<bean class="Bar">
   <property name="prototype" class="a.b.c.Foo">...


public class Bar{
        public void setPrototype(Object o){
                this.prototypeClass=o.getClass();

编辑:
dtsazza的方法也适用。

edit: dtsazza's method works as well.

编辑:
pedromarce的方法也适用。

edit: pedromarce's method works as well.

推荐答案

<bean>
   <property name="x">
      <value type="java.lang.Class">a.b.c.Foo</value>
   </property>
 </bean>

这应该有效。

这篇关于用于设置Class对象的Spring语法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆