Announcement Announcement Module
Collapse
No announcement yet.
Superclass has no null constructors but no arguments were given Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Superclass has no null constructors but no arguments were given

    Hi, I'm new in Spring, and when I tried one of the example in spring in action,I got the following error:

    Exception in thread "main" org.springframework.beans.factory.BeanCreationExce ption: Error creating bean with name 'knight': FactoryBean threw exception on object creation; nested exception is org.springframework.aop.framework.AopConfigExcepti on: Could not generate CGLIB subclass of class [class com.springinaction.chapter01.knight.KnightOfTheRou ndTable]: Common causes of this problem include using a final class or a non-visible class; nested exception is java.lang.IllegalArgumentException: Superclass has no null constructors but no arguments were given

    this is spring configuration xml file:

    <?xml version="1.0" encoding="UTF-8"?>
    <beans
    xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">

    <bean id="quest"
    class="com.springinaction.chapter01.knight.HolyGra ilQuest"/>
    <bean id="knightTarget"
    class="com.springinaction.chapter01.knight.KnightO fTheRoundTable">
    <constructor-arg>
    <value>Bedivere</value>
    </constructor-arg>
    <property name="quest">
    <ref bean="quest"/>
    </property>
    </bean>

    <bean id="minstrel" class="com.springinaction.chapter01.knight.Minstre lAdvice"></bean>

    <bean id="knightAdvisor"
    class="org.springframework.aop.support.NameMatchMe thodPointcutAdvisor">
    <property name="mappedName">
    <value>embarkOnQuest</value>
    </property>
    <property name="advice">
    <ref bean="minstrel" />
    </property>
    </bean>


    <bean id="knight" class="org.springframework.aop.framework.ProxyFact oryBean">
    <property name="proxyTargetClass" value="true"></property>
    <property name="proxyInterfaces">
    <list>
    <value>com.springinaction.chapter01.knight.Knigh t</value>

    </list>


    </property>
    <property name="interceptorNames">
    <list>
    <value>knightAdvisor</value>
    </list>
    </property>
    <property name="target">
    <ref bean="knightTarget"/>
    </property>
    </bean>

    </beans>

    This is my main file:

    public class KnightApp {

    public static void main(String[] args) throws QuestException {

    ApplicationContext ctx =
    new FileSystemXmlApplicationContext(
    "/src/applicationContext.xml");

    //Instantiate an object

    KnightOfTheRoundTable knight =
    (KnightOfTheRoundTable) ctx.getBean("knight");
    knight.embarkOnQuest();

    }

    }

    class KnightOfTheAroundTable implements interface Knight,
    can you please tell me what am I doing wrong?


    thanks for any help!

  • #2
    Try removing the following from your ProxyFactoryBean configuration:

    Code:
    <property name="proxyTargetClass" value="true"></property>
    This setting tells Spring to use CGLIB to create a subclass of the target object to apply your aspect, instead of creating dynamic proxy(ies) for the interface(s). There are some limitations to CGLIB - for example, the target object must have a default (i.e., no-arg) constructor, which is the cause of your error. Generally, the dynamic proxy approach (the default when you don't specify the proxyTargetClass property) is recommended when you have interface(s).

    Comment


    • #3
      Thanks for response.
      when I remove this property: <property name="proxyTargetClass" value="true"></property>.
      Another error happened:

      Exception in thread "main" java.lang.ClassCastException: $Proxy0 cannot be cast to com.springinaction.chapter01.knight.KnightOfTheRou ndTable
      at com.springinaction.chapter01.knight.KnightApp.main (KnightApp.java:30).

      There are two interfaces , Knight and Quest. if I use the property, there is no way to create default no-arg Construtor in interface. If I remove the property, the error above will be happened. How should I solve the problem?
      Thanks for your help!!!

      Comment


      • #4
        here is class implements interface Knight

        Code:
        package com.springinaction.chapter01.knight;
        
        
        public class KnightOfTheRoundTable implements Knight {
        
        	private Quest quest;
        	private Minstrel minstrel;
        	
        	public KnightOfTheRoundTable(){}
        	/**
        	 * @return the minstrel
        	 */
        	public Minstrel getMinstrel() {
        		return minstrel;
        	}
        	/**
        	 * @param minstrel the minstrel to set
        	 */
        	public void setMinstrel(Minstrel minstrel) {
        		this.minstrel = minstrel;
        	}
        	public KnightOfTheRoundTable(String name) {
        	quest = new HolyGrailQuest();
        	
        	}
        	public Object embarkOnQuest() throws QuestException {
        		minstrel = new Minstrel();
        		minstrel.compose(this.getClass().getName(), "embark on a quest");
        	return quest.embark();
        	}
        	/**
        	 * @return the quest
        	 */
        	public Quest getQuest() {
        		return quest;
        	}
        	/**
        	 * @param quest the quest to set
        	 */
        	public void setQuest(Quest quest) {
        		this.quest = quest;
        	}
        
        }

        Comment


        • #5
          I understood, and solved the problem. if use dynamic proxy, cannot cast to KnightOfTheRoundTable, only can use Knight; if use CGLIB, i need to switch to setter injection, and need a no-arg constructor in target object. Thanks again.

          Comment

          Working...
          X