1. @Scope:
@Scope ("prototype") or @scope ("singleton")
is equivalent to the applicationcontext.xml inside the Bean's property scope
<bean id= "StuDao1" class= " Com.mth.impl.StuDaoImpl" scope= "Singleton" ></bean>
2. @PostConstruct = Init-method; @PreDestroy = Destroy-method;
is equivalent to the applicationcontext.xml inside the Bean's properties
<bean id= "StuDao1" class= "Com.mth.impl.StuDaoImpl" "init-method=" "></bean>
Studaoimpl Code:
Package Com.mth.impl;
Import Org.springframework.context.annotation.Scope;
Import org.springframework.stereotype.Component;
Import com.mth.bean.Student;
Import Com.mth.dao.IStuDao;
/**
* *
@ClassName: Studaoimpl
* @Description: @Component ("Studao") a name called Studao
* @author mth 75100313@qq.com
* @date 2014-1-19 PM 10:13:23
* * */
//bean properties inside the annotation representation of the scope, parameter is singleton (one) or prototype (create a new one each time)
@Scope ("prototype")
@Component ("Studao") Public
class Studaoimpl implements Istudao {
@Override public
void Savestu (Student stu) {
System.out.println (save student);
}
Service Code:
Package com.mth.service;
Import javax.annotation.PostConstruct;
Import Javax.annotation.PreDestroy;
Import Javax.annotation.Resource;
Import org.springframework.stereotype.Component;
Import com.mth.bean.Student;
Import Com.mth.dao.IStuDao;
@Component ("Stuser") public
class Service {
private Istudao dao;
The method executed before service initialization is equivalent to the Init-method property in the configuration file
@PostConstruct public
void init () {
System.out.println ( "Init");
}
Public Istudao Getdao () {return
dao;
}
Specifies that the resource currently required to be injected matches the value in the component
@Resource (name = "Studao") public
void Setdao (Istudao dao) {
This.dao = DAO;
}
public void Savestu (Student Student) {
dao.savestu (Student);
}
Equivalent to the Destroy-method property configured in the Bean
@PreDestroy public
void Destory () {
System.out.println ("Destory");
}
}
applicationcontext.xml Configuration:
<?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-3.0.xsd
Http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd ">
<context:component-scan Base-package= "com.mth" ></context:component-scan>
</beans>
Test Code:
Package com.mth.test;
Import Org.springframework.context.support.ClassPathXmlApplicationContext;
Import com.mth.bean.Student;
Import Com.mth.service.Service;
public class Test {
/**
* @Title: Main
* @Description: Test spring annotation
* @param @param args Set file
* @return void return type
* @throws
*
/public static void main (string[] args) {
classpathxmlapplicationcontext context = new Classpathxmlapplicationcontext (
"Applicationcontext.xml");
Getbean parameter must be the service class above @component ("Stuser") inside the parameter
service service = (service) Context.getbean ("Stuser");
Service.savestu (New Student ());
Context.destroy ();
}