Announcement Announcement Module
Collapse
No announcement yet.
Simple bean example Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Simple bean example

    I have two classes:

    Code:
    package pachet;
    
    import org.springframework.beans.factory.BeanFactory;
    import org.springframework.beans.factory.xml.XmlBeanFactory;
    import org.springframework.core.io.FileSystemResource;
    
    public class DrawingApp {
        public static void main(String[] args){
            BeanFactory factory = new XmlBeanFactory(new FileSystemResource("spring.xml"));
            Triangle triangle = (Triangle) factory.getBean("tri");
            triangle.draw();
        }
    }
    Code:
    package pachet;
    
    public class Triangle {
        public void draw(){
            System.out.println("Am...desenat un triunghi (sa zicem:)");
        }
    }
    and the xml:

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd">
            
    <beans>
        <bean id="tri" class="pachet.Triangle"/>
    </beans>
    what did I done wrong?

  • #2
    I also tried:

    Code:
    package pachet;
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class DrawingApp {
        public static void main(String[] args){
            ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
            Triangle triangle = (Triangle) context.getBean("tri");
            triangle.draw();
        }
    }
    with the spring.xml:

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd">
    
    <beans>
        <bean id="tri" class="pachet.Triangle"/>
    </beans>
    but it still gives me:
    Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory
    at org.springframework.util.ClassUtils.<clinit>(Class Utils.java:73)
    at org.springframework.core.io.DefaultResourceLoader. <init>(DefaultResourceLoader.java:52)
    at org.springframework.context.support.AbstractApplic ationContext.<init>(AbstractApplicationContext.jav a:199)
    at org.springframework.context.support.AbstractRefres hableApplicationContext.<init>(AbstractRefreshable ApplicationContext.java:84)
    at org.springframework.context.support.AbstractRefres hableConfigApplicationContext.<init>(AbstractRefre shableConfigApplicationContext.java:59)
    at org.springframework.context.support.AbstractXmlApp licationContext.<init>(AbstractXmlApplicationConte xt.java:58)
    at org.springframework.context.support.ClassPathXmlAp plicationContext.<init>(ClassPathXmlApplicationCon text.java:136)
    at org.springframework.context.support.ClassPathXmlAp plicationContext.<init>(ClassPathXmlApplicationCon text.java:83)
    at pachet.DrawingApp.main(DrawingApp.java:10)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Native MethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(De legatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at com.intellij.rt.execution.application.AppMain.main (AppMain.java:120)

    Process finished with exit code 1

    Comment


    • #3
      You are missing commons-logging in your classpath.

      Comment


      • #4
        Thanks! It's ok now!

        Comment

        Working...
        X