Announcement Announcement Module
Collapse
No announcement yet.
bean of type java.lang.Class Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

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

    Comment


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

      Comment


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

        Comment


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

          Comment


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

            Comment

            Working...
            X