0

this is my first time using a framework, so i got struggling here, i was following several tutorials on web but still get same error.. this is my code

HelloWorld.java

package com.andi.latihanspring;

/**
*
* @author andi
*/
public class HelloWorld {
private String message;

public void getMessage() {
    System.out.println("Your message : "+message);
}

public void setMessage(String message) {
    this.message = message;
}

}

MainApp.java

package com.andi.latihanspring;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 *
 * @author andi
 */
 public class MainApp {
    public static void main(String []args){
        ApplicationContext context=new      ClassPathXmlApplicationContext("classpath*:context.xml");
    HelloWorld obj=(HelloWorld)context.getBean("helloWorld");
    obj.getMessage();
}
}

context.xml

<?xml version='1.0' encoding='UTF-8' ?>
<!-- was: <?xml version="1.0" encoding="UTF-8"?> -->
<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:p="http://www.springframework.org/schema/p"
   xmlns:aop="http://www.springframework.org/schema/aop"
   xmlns:tx="http://www.springframework.org/schema/tx"
   xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
   http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
   http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">


<bean id="helloWorld" class="com.andi.latihanspring.HelloWorld">
    <property name="message" value="Hello Spring!"/>
</bean>

and this is the output

INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@68de145: startup date [Wed Aug 16 04:34:07 WIB 2017]; root of context hierarchy
Exception in thread "main" org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'helloWorld' is defined
at     org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:638)
at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1159)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:282)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:195)
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:973)
at com.andi.latihanspring.MainApp.main(MainApp.java:18)

i'm using netbeans and spring 4.0.1. i can't continue to next chapter cause this error, thanks a lot

FelixSFD
  • 6,052
  • 10
  • 43
  • 117
andi putra
  • 65
  • 10

0 Answers0