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
Spring-test-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
1. Create the test catalog in the project
/test
2. Creating a test package under the test directory
Package Name: Cn.jbit.spring100806.domain
3. Create a test class under a package
Class Name: Teachertest.java
Content:
@RunWith (Springjunit4classrunner.class)
@ContextConfiguration ("Classpath:applicationContext.xml")
public class Teachertest {
Private Teacher Teacher;
@Test
public void Testsayhello () {
Teacher.sayhello ();
}
@Resource (name= "Teacher")
public void Setteacher (Teacher Teacher) {
This.teacher = teacher;
}
Public Teacher Getteacher () {
return teacher;
}
}
This article is from the "Yan" blog, please be sure to keep this source http://suyanzhu.blog.51cto.com/8050189/1561202
spring-using spring-test and JUnit integration in Web applications