Java/Spring/ClassPathResource

Материал из Java эксперт
Перейти к: навигация, поиск

Spring ClassPathResource Demo

   <source lang="java">
      

File: hello.xml <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans>

  <bean id="hello"
        class="helloimpl">
  <property name="greeting">
      <value>Good Morning!...</value>
  </property>
  </bean>

</beans>

File: helloclient.java import java.io.*; import org.springframework.beans.factory.*; import org.springframework.beans.factory.xml.*; import org.springframework.core.io.*; public class helloclient {

   public static void main(String args[]) throws Exception
   {
       try
       {
       System.out.println("point1");
       Resource  res = new ClassPathResource("hello.xml");
       System.out.println("point2");
       BeanFactory  factory = new XmlBeanFactory(res);
       System.out.println("point3");
       hello bean1 = (hello)factory.getBean("hello");
       String s = bean1.sayhello("Joe");
       System.out.println(s);
       }catch(Exception e1){System.out.println(""+e1);}
   }

}

File: helloimpl.java public class helloimpl implements hello {

     private String greeting;
     public helloimpl()
     {
     }
     public helloimpl(String a)
     {
         greeting=a;
     } 
     public String sayhello(String s)
     {
          return greeting+s;
     }
     public void setGreeting(String a)
     {
          greeting=a;
     }

}

File: hello.java public interface hello {

     public String sayhello(String a);

}


      </source>