spring-using annotations for bean initialization and destruction

Source: Internet
Author: User

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

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.