Spring3.0 Configuration

Source: Internet
Author: User

Original post address: http://blog.csdn.net/wzl002/article/details/5969469
This time, I started a web project from scratch and found that there are not many instructions on Spring 3.0 on the Internet. So I would like to take this opportunity to record the process of configuring a basic web project, mainly spring 3, in fact, most of them are similar to 2.

Step 1: Create and deploy a WEB Project (skip)
1. Here I would like to briefly describe the WEB Project. In fact, the most basic of a wen project is to look at only three places:
Under the root directory (this directory is usually called webcontext or webroot ),
1. webroot/WEB-INF/Web. xml boot guide File
2. webroot/WEB-INF/classes/compiled class file, will create a sub path based on Package
3. webroot/WEB-INF/lib/jar package (note that the subdirectory cannot be created under Lib)
This is the structure of commonly used web projects. with these three structures, and then tell the absolute path of webroot to a Web Container such as Tomcat, it can be started (of course there must be something in it ).
Therefore, although the general project structure is: projectname/src, projectname/webroot, it is actually in SRC. java source code is completely irrelevant to Tomcat, and its parsing starts from webroot ,. class is what it knows.

The following is an initial WEB. xml. file, which is equivalent to the project startup guide file. The Web Container first finds this file and then interprets and starts these classes one by one.

View plain
  1. <? XML version = "1.0" encoding = "UTF-8"?>
  2. <Web-app xmlns = "http://java.sun.com/xml/ns/javaee"
  3. Xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance"
  4. Xsi: schemalocation = "http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
  5. Version = "2.5">
  6. <Servlet>
  7. <Servlet-Name> testservlet </servlet-Name>
  8. <Display-Name> test servlet </display-Name>
  9. <Servlet-class> test. testservlet </servlet-class>
  10. </Servlet>
  11. <Servlet-mapping>
  12. <Servlet-Name> testservlet </servlet-Name>
  13. <URL-pattern>/test/* </url-pattern>
  14. </Servlet-mapping>
  15. <Welcome-file-List>
  16. <Welcome-File> index. jsp </welcome-File>
  17. </Welcome-file-List>
  18. </Web-app>

 

2. Deploy to Tomcat:
There are many ways to deploy tomcat. My favorite deployment method is Apache-Tomcat/CONF/Catalina/localhost/and put it in the projectname. xml configuration file,

Content is

View plain
  1. <Context Path = "projectname" reloadable = "true" docbase = "D:/workspace/projectname/webcontent"> </context>

The project has been deployed. Simple.
In eclipse, there is no need to configure any server or runtime environment. install a tomcat plug-in and specify the Tomcat path. the preceding XML file can also be automatically generated by configuring the eclipse-Tomcat plug-in.

 

Step 2: Introduce the jar package of spring. This is not nonsense. anyone who has seen spring3.0 knows that spring3 can use spring 10 thousand. jar is split into 20 small jar files. of course, you can also easily introduce 20 of them. No problem.
However, if you don't want to do this, here is a jar package that can easily implement the most basic dependency injection on the Web:
Org. springframework. asm-3.0.5.RELEASE.jar
Org. springframework. beans-3.0.5.RELEASE.jar
Org. springframework. context-3.0.5.RELEASE.jar
Org. springframework. core-3.0.5.RELEASE.jar
Org. springframework. expression-3.0.5.RELEASE.jar
Org. springframework. web-3.0.5.RELEASE.jar
Commons-logging-1.1.1.jar
The commons-logging-1.1.1.jar is the dependency package of spring.

 

The above is a collection without spring-JDBC database management. If you want to use spring's persistent layer, you also need:
Org. springframework. jdbc-3.0.5.RELEASE.jar
Org. springframework. transaction-3.0.5.RELEASE.jar
Commons-dbcp-1.3.jar
Commons pool-1.5.3.jar
Similarly, the next two are also spring dependent packages.
The configuration of spring persistent layer is introduced in the next section.

 

Step 3: Configure spring, which is basically the same as spring 2.
1. Add in Web. xml
 

View plain
  1. <Context-param>
  2. <Param-Name> contextconfiglocation </param-Name>
  3. <Param-value>/WEB-INF/spring *. xml </param-value>
  4. </Context-param>
  5. <Listener>
  6. <Listener-class> org. springframework. Web. Context. contextloaderlistener </listener-class>
  7. </Listener>


In the past, we have seen Spring servlet configuration, which is used to correspond to some earlier web containers. I will not talk about it here.

2. Create a WEB-INF under the spring-conf.xml/(name and path are casual, with the first step of the configuration corresponding to the line. But for security considerations or do not put outside the WEB-INF .)

View plain
  1. <? XML version = "1.0" encoding = "UTF-8"?>
  2. <Beans xmlns = "http://www.springframework.org/schema/beans"
  3. Xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance"
  4. Xsi: schemalocation = "http://www.springframework.org/schema/beans
  5. Http://www.springframework.org/schema/beans/spring-beans-3.0.xsd>
  6. <Bean id = "testbo"
  7. Class = "test. testbo">
  8. </Bean>
  9. <Bean id = "testaction" class = "test. testaction">
  10. <Property name = "testbo" ref = "testbo"/>
  11. </Bean>
  12. </Beans>

Here, the spring configuration is complete, and the test class can be implemented to test the effect. Basically, besides the jar package, it is consistent with spring2.

The following is testservlet. Other testactions and testbo are written at will. Access

Http: // 127.0.0.1: 8080/projectname/test/ABC.

View plain
  1. Package test;
  2. Import java. Io. ioexception;
  3. Import javax. servlet. servletconfig;
  4. Import javax. servlet. servletexception;
  5. Import javax. servlet. http. httpservlet;
  6. Import javax. servlet. http. httpservletrequest;
  7. Import javax. servlet. http. httpservletresponse;
  8. Import org. springframework. Context. applicationcontext;
  9. Import org. springframework. Web. Context. Support. webapplicationcontextutils;
  10. Public class testservlet extends httpservlet {
  11. Testaction ta;
  12. Public testservlet ()
  13. {
  14. }
  15. Public void Init (servletconfig)
  16. Throws servletexception {
  17. Servletconfig. getservletcontext ();
  18. Applicationcontext CTX = webapplicationcontextutils. getwebapplicationcontext (servletconfig. getservletcontext ());
  19. This. Ta = CTX. getbean (testaction. Class );
  20. This. Ta. sayhello ();
  21. System. Out. println ("testservlet init ");
  22. // Testaction TA = new testaction ();
  23. // Ta. sayhello ();
  24. }
  25. Public void doget (httpservletrequest req, httpservletresponse resp)
  26. Throws ioexception, servletexception
  27. {
  28. Dopost (req, resp );
  29. }
  30. Public void dopost (httpservletrequest request, httpservletresponse response)
  31. Throws ioexception, servletexception
  32. {
  33. This. Ta. sayhello ();
  34. }
  35. Public void destroy ()
  36. {
  37. }
  38. }

.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.