Results 1 to 6 of 6

Thread: bean of type java.lang.Class

  1. #1
    Join Date
    Feb 2010
    Posts
    15

    Default bean of type java.lang.Class

    I wanted to migrate a lot of our controller classes to mostly annotations, so i've been migrating a lot of the properties and arguments to id'd beans that the controllers can autowire. however, i'm not sure how to make a java.lang.Class bean in xml. any ideas?

  2. #2

    Default

    I've never tried it before but I would expect it to work like normal:

    Code:
    <bean name="someClass" class="java.lang.Class" />
    Do you get an error when you try that?

    Chris

  3. #3
    Join Date
    Feb 2010
    Posts
    15

    Default

    Code:
    Caused by: java.lang.SecurityException: Can not make a java.lang.Class constructor accessible
    	at java.lang.reflect.AccessibleObject.setAccessible0(AccessibleObject.java:118)
    	at java.lang.reflect.AccessibleObject.setAccessible(AccessibleObject.java:108)
    	at org.springframework.util.ReflectionUtils.makeAccessible(ReflectionUtils.java:428)
    	at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:99)
    	at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:61)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:8

  4. #4

    Default

    Hmm from the Javadocs:

    Code:
    Class has no public constructor. Instead Class  objects are constructed automatically by the Java Virtual Machine as classes are loaded and by calls to the defineClass method in the class loader.
    So I guess Class is a special case. I'm sure it would be redundant to ask if creating a bean of type Class is really what you're after? Can you subtype it to something more specific to your needs?

    Chris

  5. #5
    Join Date
    Feb 2010
    Posts
    15

    Default

    well, i want it to specifically be the class object for a command class. normally when setting this via the property tag, you can just type
    Code:
    <property name="commandClass" value="some.class.Here" />
    and it knows that you mean the some.class.Here.class

  6. #6
    Join Date
    Feb 2010
    Posts
    15

    Default

    for anyone that cares, this is how i ended up doing it:

    Code:
    <bean id="myClassObjectBean" class="java.lang.Class" factory-method="forName">
      <constructor-arg value="the.class.i.want.as.a.Bean"/>
    </bean>

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
  •