期待更好更稳定的开源FrameWork的出现,让我们一起努力吧!  
日历
<2008年1月>
303112345
6789101112
13141516171819
20212223242526
272829303112
3456789
统计
  • 随笔 - 78
  • 文章 - 1
  • 评论 - 29
  • 引用 - 0

导航

常用链接

留言簿(1)

随笔分类

随笔档案(42)

文章档案(37)

相册

搜索

  •  

积分与排名

  • 积分 - 44787
  • 排名 - 1067

最新随笔

最新评论

阅读排行榜

评论排行榜

 

SPRING ... A JUMP START
-----------------------
by Farihah Noushene B.E.,
-------------------------

                         

PART-II
-----------------

 

(PUBLISHED IN DEVELOPER IQ - September2005)
The Spring Framework comes in the form of ZIP file with the necessary jars, examples etc., The Spring framework can be downloaded from http://www.springframework.org. There will be two zip files one with dependencies and other without. The spring framework with dependencies is larger and includes all dependent libraries. Download Spring framework 1.2 without dependency and unzip on the hard disk as spring12

Inside the folder spring12 we can find 7 folders. The name and the contents of all the folders are given below,
1. dist: It contains various Spring distribution jar files.
2. docs: It contains general documentation and API javadocs.
3. mock: It contains mock JNDI contexts and a set of servlet API mock objects.
4. samples: It contains demo applications and skeletons.
5. src: It contains the Java source files for the framework.
6. test: It contains the Java source files for Spring's test suite.
7. tiger: It contains JDK1.5 examples and test suite.

Inside the "dist" directory, we can find all the jar files necessary for compiling and executing the program. The jar files and its contents are listed below:
1.  spring.jar : It contains the entire Spring framework including everything in the other JAR files also.
2.  spring-core.jar : It contains the core Spring container and its utilities.
3.  spring-beans.jar : It contains the bean Spring container and JavaBeans support utilities.
4.  spring-aop.jar : It contains Spring's AOP framework, source-level metadata support, AOP Alliance interfaces etc.,
5.  spring-context.jar : It contains application context, validation framework, JNDI, templating support and scheduling.
6.  spring-dao.jar : It contains DAO support and transaction infrastructure.
7.  spring-jdbc.jar : It contains the JDBC support.
8.  spring-support.jar : It contains JMX support, JCA support, scheduling support, mail support and caching support.
9.  spring-web.jar : It contains the web application context, multipart resolver, Struts support, JSF support and web utilities.
10. spring-webmvc.jar : It contains the framework servlets, web MVC framework, web controllers and web views.
11. spring-remoting.jar :It contains remoting support, EJB support and JMS support.
12. spring-orm.jar : It contains iBATIS SQL Maps support, Apache OJB support, TopLink support and JDO support.
13. spring-hibernate.jar : It contains Hibernate 2.1 support, Hibernate 3.x support.
14. spring-mock.jar : It contains JNDI mocks, Servlet API mocks and JUnit support.

--------------------------------------------

Now we will run a greeter example in Spring.

f:\>md springdemo
f:\>cd springdemo

As the entire Spring Framework is included in spring.jar. We use that to run our examples.
Copy spring.jar from spring12\dist folder to the working folder. Also copy commons-logging.jar from tomcat41\server\lib to the working directory.

f:\springdemo>set path=c:\windows\command;g:\jdk1.5\bin
(* Set path for jdk1.5 or jdk1.4.2 only. )
f:\springdemo>set classpath=f:\springdemo;
f:\springdemo\spring.jar;

 f:\springdemo\commons-logging.jar

  For a typical Spring Application we need the following files
1. An interface that defines the functions.
2. An Implementation that contains properties, its setter and getter methods, functions etc.,
3. A XML file called Spring configuration file.
4. Client program that uses the function.

Edit
1. hello.java
2. helloimpl.java
3. hello.xml
4. helloclient.java

  //f:\springdemo\hello.java

public interface hello
{
   public String sayhello(String a);
}

--------------------------------------------

//f:\springdemo\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;

      }

}

--------------------------------------------

//f:\springdemo\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>

--------------------------------------------

//f:\springdemo\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("please Wait.");
  
      Resource     res = new  ClassPathResource("hello.xml");
  
      BeanFactory  factory = new  XmlBeanFactory(res);
  
      hello bean1 = (hello)factory.getBean("hello");
  
     String s = bean1.sayhello(args[0]);
  
     System.out.println(s);

      }

      catch(Exception e1)

            { System.out.println(""+e1); }

   }

}

-------------------------------------------

To run:
f:\springdemo>javac hello.java
f:\springdemo>javac helloimpl.java
f:\springdemo>javac helloclient.java
f:\springdemo>java helloclient "sam"

We will get the output as follows:
please wait..
Aug 5, 2002 2:10:56 AM org.springframework. 
beans.factory.xml.XmlBeanDefinitionReader
loadBeanDefinitions

INFO: Loading XML bean definitions from 
class path resource [hello.xml]
Aug 5, 2002 2:10:57 AM org.springframework.
beans.factory.support.
AbstractBeanFactorygetBean

INFO: Creating shared instance of singleton
bean 'hello'
Good Morning!...sam

------------------------------------

Thus we have run our first Spring program. Here helloimpl implements the hello interface. Although it is not necessary to hide the implementation behind an interface, it is recommended as a way to seperate implementation from interface. helloimpl has a single property greeting. This property can be set in two different ways: by property's setter method or  by the constructor. The XML file hello.xml declares the instance of helloimpl.java in the spring container and configures its property greeting with the value 'Good Morning!...'

The root of the hello.xml file is the <beans> element, which is the root element
of any Spring configuration file. The <bean> element is used to tell the Spring container about the class and how it should be configured. Here, the id attribute takes the interface name and the class attribute specifies the bean抯 fully qualified class name.

Within the <bean> element, the <property> element is used to set the property, in this case the greeting property. By using <property>, we抮e telling the Spring container to call setGreeting() while setting the property. The value of the greeting is defined within the <value> element. Here we have given 'Good Morning!...' as the value.

The container instantiates the 'helloimpl' based on the XML definition as,

helloimpl hello = new helloimpl();
helloimpl.setGreeting("Good Morning!...");

Similarly, greeting property may be set through the single argument constructor of 'helloimpl' as,
<bean id="hello"  class="helloimpl">
<constructor-arg>
<value>Good Morning!...</value>
</constructor-arg>
</bean>

Now the container instantiates the 'helloimpl' based on the XML definition as,

helloimpl
hello = new
helloimpl("Good Morning...");

In the client program, 'BeanFactory' class is used which is the Spring container. Then the 'getBean()' method is called to get the reference to the 'hello'. With this reference, sayhello() method is called.

------------------------------------------

We can also use a frame client. The code is very much similar to console client except the GUI code.

//f:\springdemo\helloframe.java

import java.io.*;

import java.awt.*;

import org.springframework.beans.factory.*;

import org.springframework.beans.factory.xml.*;

import org.springframework.core.io.*;

public class helloframe  extends Frame

{

TextField     text1;

TextArea      area1;

Label         label1;

Button        button1;

   public static void main(String args[])

   {

      helloframe   app = new helloframe();

      app.setSize(700,500);

      app.setVisible(true);

   }

   helloframe()

   {

      setLayout(new FlowLayout());

      setBackground(Color.green);

      label1=new Label("Type Name: ");

      text1=new TextField(25);

      area1=new TextArea(10,50);

      button1=new Button("Exit");

      button1.setBackground(Color.red);

      add(label1);

      add(text1);         

      add(area1);         

      add(button1);

   }

   public boolean action (Event e,Object c)

   {

      if(e.target==text1)

      {

         try

         {

            area1.append("Please Wait..\n");

            Resource  res = new  ClassPathResource("hello.xml");                

            BeanFactory  factory = new  XmlBeanFactory(res);

            hello bean1 = (hello)factory.getBean("hello");

            String s = bean1.sayhello(text1.getText());

            area1.append(s);

         }

         catch(Exception e1)

                    {area1.append(""+e1);}

      }

      if(e.target==button1)

      {

         System.exit(0);

      }

      return true;

   }    

Then compile and run the frame client program. We will a textbox, a exit button and a text area. Type a name (say 'tom') in text area and press enter. 'Good Morning!... tom' will appear in the text area

--------------------------------------------

    Next we shall see how to run this program as a servlet. Consider Tomcat-5 is installed in g drive.
   
First copy g:\spring12\spring.jar to g:\tomcat5\common\lib and start tomcat server.
    Then set classpath as shown below and edit the servletclient.java.

     f:\springdemo>set classpath=f:\springdemo;

    f:\springdemo\spring.jar;

    f:\springdemo\commons-logging.jar;

    g:\tomcat5\common\lib\servlet-api.jar

//f:\springdemo\servletclient.java

import java.io.*;

import org.springframework.beans.factory.*;

import org.springframework.beans.factory.xml.*;

import org.springframework.core.io.*;

import javax.servlet.*;

import javax.servlet.http.*;

public class servletclient extends HttpServlet

{

   public void doPost(HttpServletRequest req,HttpServletResponse resp)

        throws ServletException,IOException

 

   {

      resp.setContentType("text/html");

      PrintWriter out =resp.getWriter();

 

      String a = req.getParameter("text1");

      try

      {

         System.out.println("Please wait.");

 

         Resource  res = new  ClassPathResource("hello.xml");

         System.out.println("Resource ok");

 

         BeanFactory  factory = new  XmlBeanFactory(res);

         System.out.println("BeanFactory ok");

 

         hello bean1 = (hello)factory.getBean("hello");

 

         String s = bean1.sayhello(a);

         out.println(s);

      }

      catch(Exception e1)

              {System.out.println(""+e1);}

   }

}

--------------------------------------------

//f:\springdemo\servletclient.htm

 

<html>

<body>

<form    method=post 

         action="http://localhost:8080/ 

                     servlet/servletclient">

   <input type=text name="text1">

   <input type=submit>

</form>

</body>

</html>

--------------------------------------------

Then compile the servlet and copy all the class files ie., hello.class, helloimpl.class,  servletclient.class and the xml file hello.xml  to g:\tomcat5\webapps\root\web-inf\classes. Copy html file servletclient.htm to g:\tomcat5\webapps\root. Add entry to web.xml file.
Restart Tomcat server and open browser and type url as http://localhost:8080/servletclient.htm. We will get a text box and a button. Type a name (say 'tom') and click the 'submit' button.

Good Morning!... tom will appear

In the next article, we shall see how to contact the database from Spring.

-------------------------------------------

 

                         



posted on 2007-06-07 21:14 BlueSky_itwangxinli 阅读(738) 评论(1)  编辑  收藏
评论:

只有注册用户登录后才能发表评论。


网站导航:
 
 
Copyright © BlueSky_itwangxinli Powered by: 博客园 模板提供:沪江博客