IOC : Inversion of Control
DI :Dependency Injection
setter injection
< property name = " message " value = " Spring is simple. " />
</ bean >
Constructor injection
< constructor - arg value = " Hello Spring " />
</ bean >
< constructor - arg type = " java.lang.String " value = " Rakesh Pandey " />
< constructor - arg type = " int " value = " 101 " />
</ bean >
< constructor - arg index = " 0 " value = " Aakash Pandey " />
< constructor - arg index = " 1 " value = " 1001 " />
</ bean >
Reference injection
< constructor - arg value = " Spring is simple " />
</ bean >
< bean id = " message " class = " ReferentialBean " >
< property name = " message " ref = " simpleMessage " />
</ bean >
< bean id = " show " class = " ioc.FirstBean " >
< constructor - arg ref = " anotherBean " />
</ bean >
idref (可验证的)
< property name = " message " value = " Spring is simple. " />
</ bean >
< bean id = " another " class = " ioc.AnotherBean " >
< property name = " amessage " >
< idref bean = " first " />
</ property >
</ bean >
inner bean
< constructor - arg >
< bean class = "ioc .InnerBean " >
< property name = " name " value = " satya " />
< property name = " address " value = " patna " />
</ bean >
</ constructor - arg >
</ bean >
list
< property name = " lists " >
< list >
< value > 1 </ value >
< ref bean = " studentBean " />
< bean class = " ioc.StudentBean " >
< property name = " name " value = " ankit " />
< property name = " address " value = " delhi " />
</ bean >
</ list >
</ property >
</ bean >
Set
< property name = " sets " >
< set >
< value > 1 </ value >
< value > 1 </ value >
< value > 1 </ value >
< value > 1 </ value >
< ref bean = " studentBean1 " />
< bean class = " ioc.set.StudentBean " >
< property name = " name " value = " ankit " />
< property name = " address " value = " delhi " />
</ bean >
</ set >
</ property >
</ bean >
Props
< property name = " pros " >
< props >
< prop key = " roll " > 100 </ prop >
< prop key = " name " > satya </ prop >
</ props >
</ property >
</ bean >
Map
< property name ="details" >
< map >
< entry key ="Satya" value ="101" />
< entry key ="Rohit" value ="102" />
< entry key ="Aniket" value ="103" />
</ map >
</ property >
</ bean >
Null值
< property name ="message" >
< null />
</ property >
</ bean >
p-namespace (设置bean的field)
p:message ="Message passing using p-namespace" >
</ bean >
lazy initialization
lazy-init ="true" >
< property name ="city" value ="New Delhi" />
< property name ="state" value ="Delhi" />
</ bean >
autowiring by name
< property name ="registration" value ="BL001" />
< property name ="year" value ="2001" />
</ bean >
public void setStudent(Student student) {
this .student = student;
}
autowiring by Type
不是唯一的bean会报错(No unique bean of type is defined)
< property name ="registration" value ="BL001" />
< property name ="year" value ="2001" />
</ bean >
不是唯一的bean会报错(No unique bean of type is defined)
public College(Student student) {
this .student = student;
}
< property name ="registration" value ="BL001" />
< property name ="year" value ="2001" />
</ bean >
autowiring by autodetect
< property name ="registration" value ="BL001" />
< property name ="year" value ="2001" />
</ bean >
class ="spring.exclude.autwiring.College" autowire-candidate ="false" >
< property name ="registration" value ="BL001" />
< property name ="year" value ="2001" />
</ bean
@required
public void setStudent(Student student) {
this .student = student;
}
scope="singleton" (默认、单例)
scope="prototype" (多次实例化)
inherit
< property name ="subject" value ="Science" />
</ bean >
< bean id ="TeacherBean" parent ="baseBean" >
< property name ="student" ref ="StudentBean" />
< property name ="teachername" value ="Rakesh" />
</ bean >
inherit override
< property name ="subject" value ="Science" />
</ bean >
< bean id ="TeacherBean" parent ="baseBean" >
< property name ="subject" value ="Biology" />
< property name ="student" ref ="StudentBean" />
< property name ="teachername" value ="Rakesh" />
</ bean >
dependency-check ="simple" >
< property name ="rollNo" value ="101" />
</ bean >
< bean id ="student2" class ="spring.dependency.check.mode.StudentBean"
dependency-check ="objects" >
< property name ="nestedStudentBean" ref ="nestedStudentBean" />
</ bean >
< bean id ="student3" class ="spring.dependency.check.mode.StudentBean"
dependency-check ="all" >
< property name ="nestedStudentBean" ref ="nestedStudentBean" />
< property name ="rollNo" value ="101" />
</ bean >
< bean id ="nestedStudentBean"
class ="spring.dependency.check.mode.StudentBean" />
Spring List Factory
Ths ListFactoryBean is a simple factory for shared List instance. The list element is defined in the xml bean definitions. The setSourceList method set the source List which is written in xml list element. The SetTargetListClass method set the class to use target List.
< property name ="days" >
< bean class ="org.springframework.beans.factory.config.ListFactoryBean" >
< property name ="targetListClass" >
< value > java.util.ArrayList </ value >
</ property >
< property name ="sourceList" >
< list >
< value > Monday </ value >
< value > Tuesday </ value >
< value > Wednesday </ value >
< value > Thursday </ value >
< value > Friday </ value >
< value > Saturday </ value >
< value > Sunday </ value >
</ list >
</ property >
</ bean >
</ property >
</ bean >
Spring Set Factory
继承自AbstractFactoryBean
< property name ="days" >
< bean class ="org.springframework.beans.factory.config.SetFactoryBean" >
< property name ="targetSetClass" >
< value > java.util.HashSet </ value >
</ property >
< property name ="sourceSet" >
< list >
< value > Monday </ value >
< value > Tuesday </ value >
< value > Wednesday </ value >
< value > Thursday </ value >
< value > Friday </ value >
< value > Saturday </ value >
< value > Sunday </ value >
</ list >
</ property >
</ bean >
</ property >
</ bean >
Spring Map Factory
< property name ="days2" >
< bean class ="org.springframework.beans.factory.config.MapFactoryBean" >
< property name ="targetMapClass" >
< value > java.util.HashMap </ value >
</ property >
< property name ="sourceMap" >
< map >
< entry key ="Key1" value ="Sunday" />
< entry key ="Key2" value ="Monday" />
< entry key ="Key3" value ="Tuesday" />
< entry key ="Key4" value ="Wednesday" />
< entry key ="Key5" value ="Thursday" />
< entry key ="Key6" value ="Friday" />
< entry key ="Key7" value ="Saturday" />
</ map >
</ property >
</ bean >
</ property >
</ bean >
Spring Date Property
class ="org.springframework.beans.factory.config.CustomEditorConfigurer" >
< property name ="customEditors" >
< map >
< entry key ="java.util.Date" >
< bean class ="org.springframework.beans.propertyeditors.CustomDateEditor" >
< constructor-arg index ="0" >
< bean class ="java.text.SimpleDateFormat" >
< constructor-arg value ="dd/MM/yyyy" />
</ bean >
</ constructor-arg >
< constructor-arg index ="1" value ="false" />
</ bean >
</ entry >
</ map >
</ property >
</ bean >
< bean id ="bean1" class ="spring.date.propertya" >
< property name ="name" value ="Rakesh" />
< property name ="joiningDate" value ="1/09/2010" />
</ bean >
import java.util.Date;
public class Employee {
private Date joiningDate;
private String name;
public Date getJoiningDate() {
return joiningDate;
}
public void setJoiningDate(Date joiningDate) {
this .joiningDate = joiningDate;
}
public String getName() {
return name;
}
public void setName(String name) {
this .name = name;
}
}
class ="org.springframework.beans.propertyeditors.CustomDateEditor" >
< constructor-arg >
< bean class ="java.text.SimpleDateFormat" >
< constructor-arg value ="yyyy-MM-dd" />
</ bean >
</ constructor-arg >
< constructor-arg value ="true" />
</ bean >
< bean class ="org.springframework.beans.factory.config.CustomEditorConfigurer" >
< property name ="customEditors" >
< map >
< entry key ="java.util.Date" >
< ref local ="dateEditor" />
</ entry >
</ map >
</ property >
</ bean >
public class StudentService {
@Autowired
StudentDAO studentDAO;
@Override
public String toString() {
return " StudentService [studentDAO= " + studentDAO + " ] " ;
}
}
< context:exclude-filter type ="regex"
expression ="ioc\.component\.*Student.*" />
< context:exclude-filter type ="annotation"
expression ="org.springframework.stereotype.Service" />
</ context:component-scan >
DisposableBean InitializingBean marker interface
String message;
public String getMessage() {
return message;
}
public void setMessage(String message) {
this .message = message;
}
public void afterPropertiesSet() throws Exception {
System.out.println( " After properties has been set : " + message);
}
public void destroy() throws Exception {
System.out.println( " Cleaned everything!! " );
}
}
private ApplicationContext context;
public void setApplicationContext(ApplicationContext applicationContext)
throws BeansException {
context = applicationContext;
System.out.println(applicationContext);
}
}
public Object postProcessBeforeInitialization(Object bean, String beanName)
throws BeansException {
return bean;
}
public Object postProcessAfterInitialization(Object bean, String beanName)
throws BeansException {
System.out.println( " Initialized Bean : " + beanName);
return bean;
}
}
private String name;
public void setBeanName(String name) {
this .name = name;
}
}
public void initIt() throws Exception {
System.out.println( " After properties has been set : " + test);
}
@PreDestroy
public void cleanUp() throws Exception {
System.out.println( " Cleaned Everyting " );
}
ConfigurableApplicationContext context;
context.close();