First, create the project
Project Name: spring100806
Ii. adding spring support to the project
1. Add the jar package in the Lib directory
Commons-logging.jar
Junit-4.10.jar
Log4j.jar
Spring-beans-3.2.0.release.jar
Spring-context-3.2.0.release.jar
Spring-core-3.2.0.release.jar
Spring-expression-3.2.0.release.jar
Spring-web-3.2.0.release.jar
Iii. Adding a configuration file to a project
1. Create the Conf directory in your project
/conf
2. Add a configuration file to the Conf directory
Configuration file name: Applicationcontext.xml
Configuration file Contents:
<?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:context= "Http://www.springframework.org/schema/context"
Xsi:schemalocation= "
Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
Http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd " >
</beans>
Iv. Adding configuration information to the Web. xml file
<!--Specify a profile path--
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!--configuration Listener--
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
Iv. creation of related beans
1. Create a bean in the SRC directory
Package Name: Cn.jbit.spring100806.domain
Class Name: Teacher.java
Contents of the class:
@Component ("Teacher")
public class Teacher {
public void SayHello () {
System.out.println ("Hello");
}
}
2. Configuring annotation scanning and annotation parsing in the configuration file
<!--Notice Spring parsing common annotations--
<context:annotation-config/>
<!--Notice that the Spring app annotation Bean is located--
<context:component-scan base-package= "Cn.jbit.spring100806.domain" ></context:component-scan>
V. Testing
Create a servlet under SRC to test
Package Name: Cn.jbit.spring100806.web.servlet
Servlet Name: Teacherservlet.java
Servlet content:
public class Teacherservlet extends HttpServlet {
public void doget (HttpServletRequest request, httpservletresponse response)
Throws Servletexception, IOException {
DoPost (request, response);
}
public void DoPost (HttpServletRequest request, httpservletresponse response)
Throws Servletexception, IOException {
1. Get Webapplicationcontext
/*
Webapplicationcontext Webapplicationcontext =
(Webapplicationcontext) Getservletcontext (). getattribute (Webapplicationcontext.root_web_application_context_ ATTRIBUTE);
*/
2. Get Webapplicationcontext
Webapplicationcontext Webapplicationcontext =
Webapplicationcontextutils.getwebapplicationcontext (Getservletcontext ());
Teacher Teacher = (Teacher) webapplicationcontext.getbean ("Teacher");
Teacher.sayhello ();
}
This article is from the "Yan" blog, please be sure to keep this source http://suyanzhu.blog.51cto.com/8050189/1561201
spring-using spring in Web applications