First, create the project
Project Name: spring100804
Ii. adding spring support to the project
1. Create a Lib directory in your project
/lib
2. 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
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. creation of related beans
1. Create DAO in src directory
Package Name: Cn.jbit.spring100801.domain
Class Name: Student.java
Contents of the class:
Equivalent to declaring a Bean,bean ID called student.
@Component ("Student")
public class Student {
/**
* Initialization method
*/
@PostConstruct
public void init () {
System.out.println ("Init");
}
/**
* Method of Destruction
*/
@PreDestroy
public void Destory () {
System.out.println ("Destory");
}
}
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.spring100801.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.spring100801.domain
3. Create a test class under a package
Class Name: Studenttest.java
Content:
/**
* Test initialization and destruction classes
* @author Administrator
*
*/
public class Studenttest {
@Test
public void Testinitanddestory () {
Classpathxmlapplicationcontext context = new Classpathxmlapplicationcontext ("Classpath:applicationContext.xml");
Student Student = (Student) context.getbean ("Student");
Context.close ();
}
}
This article is from the "Yan" blog, please be sure to keep this source http://suyanzhu.blog.51cto.com/8050189/1561200
spring-using annotations for bean initialization and destruction