0

I am making a simple program to test the inner bean but getting exception. Here is the code i have write.

TextEditor Class:

public class TextEditor {

private SpellChecker spellChecker;

public SpellChecker getSpellChecker() {
    return spellChecker;
}

public void setSpellChecker(SpellChecker spellChecker) {
    this.spellChecker = spellChecker;
}

public void spellCheck(){
    spellChecker.spellChecking();
}
}

SpellChecker Class:

public class SpellChecker {

public SpellChecker() {
    System.out.println("Spell Checker Constructor");
}

public void spellChecking(){
    System.out.println("Spell Checking");
}
}

Controller Class:

public class Controller {

public static void main(String[] args) {
    ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
    TextEditor editor = (TextEditor)context.getBean("editor");
    editor.spellCheck();
}
}

spring.xml:

<?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-3.0.xsd">

 <bean id="editor" class="com.data.TextEditor">
    <property name="spellChecker">
        <bean id="spellChecker" name="com.data.SpellChecker"/>
    </property>
</bean>     

Stack Trace:

WARNING: Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'editor' defined in class path resource [spring.xml]: Cannot create inner bean 'spellChecker' while setting bean property 'spellChecker'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'spellChecker' defined in class path resource [spring.xml]: Instantiation of bean failed; nested exception is java.lang.IllegalStateException: No bean class specified on bean definition
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'editor' defined in class path resource [spring.xml]: Cannot create inner bean 'spellChecker' while setting bean property 'spellChecker'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'spellChecker' defined in class path resource [spring.xml]: Instantiation of bean failed; nested exception is java.lang.IllegalStateException: No bean class specified on bean definition
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveInnerBean(BeanDefinitionValueResolver.java:313)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:122)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1481)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1226)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:543)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:772)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:839)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:538)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
at com.controller.Controller.main(Controller.java:17)
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'spellChecker' defined in class path resource [spring.xml]: Instantiation of bean failed; nested exception is java.lang.IllegalStateException: No bean class specified on bean definition
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1105)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1050)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:510)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveInnerBean(BeanDefinitionValueResolver.java:299)
... 15 more
Caused by: java.lang.IllegalStateException: No bean class specified on bean definition
at org.springframework.beans.factory.support.AbstractBeanDefinition.getBeanClass(AbstractBeanDefinition.java:356)
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:66)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1098)
... 19 more

This is all the code i have written can any body tell me what is the mistake i am doing here. I shall be thankful.

4 Answers 4

1
 <bean id="spellChecker" name="com.data.SpellChecker"/>

Change attribute name to attribute class

Sign up to request clarification or add additional context in comments.

Comments

1
<?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-3.0.xsd">

 <bean id="editor" class="com.data.TextEditor">
    <property name="spellChecker">
        <bean class="com.data.SpellChecker"/>
    </property>
</bean> 

Comments

1

Change attribute name to attribute class

this will solve your problem

<!-- Definition for textEditor bean using inner bean -->
  <bean id="textEditor" class="com.tutorialspoint.TextEditor">
    <property name="spellChecker">
       <bean id="spellChecker" class="com.tutorialspoint.SpellChecker"/>
    </property>
  </bean>

Comments

0

Bean definition is missing the class attribute. Add it:

<bean id="spellChecker" name ="com.data.SpellChecker" class="com.data.SpellChecker"/>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.