Spring ioc configuration snippet
IOC : Inversion of Control
DI :Dependency Injection
setter injection
<bean id="message" class="ioc.SetterInjection">
<property name="message" value="Spring is simple." />
</bean>
<property name="message" value="Spring is simple." />
</bean>
Constructor injection
<bean id="basic" class="ioc.ConstructorInjection">
<constructor-arg value="Hello Spring" />
</bean>
<constructor-arg value="Hello Spring" />
</bean>
<bean id="springMessage" class="ioc.ConstructorInjection">
<constructor-arg type="java.lang.String" value="Rakesh Pandey" />
<constructor-arg type="int" value="101" />
</bean>
<constructor-arg type="java.lang.String" value="Rakesh Pandey" />
<constructor-arg type="int" value="101" />
</bean>
<bean id="springMessage" class="ioc.ConstructorInjection">
<constructor-arg index="0" value="Aakash Pandey" />
<constructor-arg index="1" value="1001" />
</bean>
<constructor-arg index="0" value="Aakash Pandey" />
<constructor-arg index="1" value="1001" />
</bean>
Reference injection
<bean id="simpleMessage" class="java.lang.String">
<constructor-arg value="Spring is simple" />
</bean>
<bean id="message" class="ReferentialBean">
<property name="message" ref="simpleMessage" />
</bean>
<constructor-arg value="Spring is simple" />
</bean>
<bean id="message" class="ReferentialBean">
<property name="message" ref="simpleMessage" />
</bean>
<bean id="anotherBean" class="ioc.AnotherBean" />
<bean id="show" class="ioc.FirstBean">
<constructor-arg ref="anotherBean" />
</bean>
<bean id="show" class="ioc.FirstBean">
<constructor-arg ref="anotherBean" />
</bean>
idref (可验证的)
<bean id="first" class="ioc.FirstBean">
<property name="message" value="Spring is simple."/>
</bean>
<bean id="another" class="ioc.AnotherBean">
<property name="amessage" >
<idref bean="first"/>
</property>
</bean>
<property name="message" value="Spring is simple."/>
</bean>
<bean id="another" class="ioc.AnotherBean">
<property name="amessage" >
<idref bean="first"/>
</property>
</bean>
inner bean
<bean id="outer" class="ioc.OuterBean">
<constructor-arg>
<bean class="ioc.InnerBean">
<property name="name" value="satya" />
<property name="address" value="patna" />
</bean>
</constructor-arg>
</bean>
<constructor-arg>
<bean class="ioc.InnerBean">
<property name="name" value="satya" />
<property name="address" value="patna" />
</bean>
</constructor-arg>
</bean>
list
<bean id="collegeBean" class="ioc.CollegeBean">
<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>
<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
<bean id="collegeBean1" class="ioc.set.CollegeBean">
<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>
<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
<bean id="collegeBean3" class="collection.props.example.CollegeBean">
<property name="pros">
<props>
<prop key="roll">100</prop>
<prop key="name">satya</prop>
</props>
</property>
</bean>
<property name="pros">
<props>
<prop key="roll">100</prop>
<prop key="name">satya</prop>
</props>
</property>
</bean>
Map
<bean id="mapbean" class="spring.map.example.MapBean">
<property name="details">
<map>
<entry key="Satya" value="101"/>
<entry key="Rohit" value="102"/>
<entry key="Aniket" value="103"/>
</map>
</property>
</bean>
<property name="details">
<map>
<entry key="Satya" value="101"/>
<entry key="Rohit" value="102"/>
<entry key="Aniket" value="103"/>
</map>
</property>
</bean>
Null值
<bean id="basic" class="spring.nullvalue.example.NullBean">
<property name="message">
<null />
</property>
</bean>
<property name="message">
<null />
</property>
</bean>
p-namespace (设置bean的field)
<bean id="basic" class="spring.pnamespace.example.SpringBean"
p:message="Message passing using p-namespace">
</bean>
p:message="Message passing using p-namespace">
</bean>
lazy initialization
<bean id="addressBean" class="lazyinitialized.bean.example.Address"
lazy-init="true">
<property name="city" value="New Delhi" />
<property name="state" value="Delhi"/>
</bean>
lazy-init="true">
<property name="city" value="New Delhi" />
<property name="state" value="Delhi"/>
</bean>
autowiring by name
<bean id="college" class="spring.noautowiring.mode.College" autowire="byName">
<property name="registration" value="BL001" />
<property name="year" value="2001" />
</bean>
<property name="registration" value="BL001" />
<property name="year" value="2001" />
</bean>
@Autowired
public void setStudent(Student student) {
this.student = student;
}
public void setStudent(Student student) {
this.student = student;
}
autowiring by Type
不是唯一的bean会报错(No unique bean of type is defined)
<bean id="college" class="ioc.autowire.College" autowire="byType">
<property name="registration" value="BL001" />
<property name="year" value="2001" />
</bean>
<property name="registration" value="BL001" />
<property name="year" value="2001" />
</bean>
autowiring by Name
不是唯一的bean会报错(No unique bean of type is defined)
不是唯一的bean会报错(No unique bean of type is defined)
@Autowired
public College(Student student) {
this.student = student;
}
public College(Student student) {
this.student = student;
}
<bean id="college" class="ioc.autowire2.College" autowire="constructor">
<property name="registration" value="BL001" />
<property name="year" value="2001" />
</bean>
<property name="registration" value="BL001" />
<property name="year" value="2001" />
</bean>
autowiring by autodetect
<bean id="college" class="ioc.autowire2.College" autowire="autodetect">
<property name="registration" value="BL001" />
<property name="year" value="2001" />
</bean>
<property name="registration" value="BL001" />
<property name="year" value="2001" />
</bean>
autowire-candidate
The autowire-candidate attribute is set in the <bean/> element and when it is set to false the container excludes that specific bean from autowiring.
@required
scope="singleton" (默认、单例)
scope="prototype" (多次实例化)
inherit
inherit override
<bean id="college5"
class="spring.exclude.autwiring.College" autowire-candidate="false">
<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
@Required
public void setStudent(Student student) {
this.student = student;
}
public void setStudent(Student student) {
this.student = student;
}
<context:annotation-config />
Property 'student' is required for bean 'college'
scope="singleton" (默认、单例)
<bean id="simplebean1" class="ioc.singleton.SimpleBean" scope="singleton"/>
scope="prototype" (多次实例化)
<bean id="simplebean1" class="ioc.singleton.SimpleBean" scope="prototype"/>
inherit
The support of inheritance is present in the Spring framework and common values or configuration is shared among beans. The child bean inherits the properties and configuration of the parent bean or base bean.
<bean id="baseBean" class="bean.configuration.inheritance.Teacher">
<property name="subject" value="Science" />
</bean>
<bean id="TeacherBean" parent="baseBean">
<property name="student" ref="StudentBean" />
<property name="teachername" value="Rakesh" />
</bean>
<property name="subject" value="Science" />
</bean>
<bean id="TeacherBean" parent="baseBean">
<property name="student" ref="StudentBean" />
<property name="teachername" value="Rakesh" />
</bean>
inherit override
<bean id="baseBean" class="bean.configuration.inheritance.Teacher">
<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>
<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
It check for all bean's dependencies, that is expressed in its properties are satisfied or not.
<bean id="student1" class="spring.dependency.check.mode.StudentBean"
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" />
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.
<bean id="week" class="list.factory.example.Week">
<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>
<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
<bean id="week" class="set.factory.example.Week">
<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>
<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
<bean id="week2" class="map.factory.example.Week">
<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>
<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
Properties editors used to convert from String values to object types such as java.util.Properties.
<bean id="customEditorConfigurer"
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>
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>
package spring.date.property;
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;
}
}
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;
}
}
CustomEditorConfigurer
DisposableBean InitializingBean marker interface
Passing a date format in the bean property is not allowed and in order to do so you need to register the CustomDateEditor in CustomEditorConfigurer first so that it will convert the bean properties which type is java.util.Date
<bean id="dateEditor"
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>
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>
@Component、@Repository、@Service、@Controller
@Component
public class StudentService {
@Autowired
StudentDAO studentDAO;
@Override
public String toString() {
return "StudentService [studentDAO=" + studentDAO + "]";
}
}
public class StudentService {
@Autowired
StudentDAO studentDAO;
@Override
public String toString() {
return "StudentService [studentDAO=" + studentDAO + "]";
}
}
<context:component-scan base-package="ioc.component">
<context:exclude-filter type="regex"
expression="ioc\.component\.*Student.*" />
<context:exclude-filter type="annotation"
expression="org.springframework.stereotype.Service" />
</context:component-scan>
<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
public class StudentService implements InitializingBean, DisposableBean {
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!!");
}
}
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!!");
}
}
org.springframework.context.ApplicationContextAware实现接口 自动装配
public class StudentBean implements ApplicationContextAware {
private ApplicationContext context;
public void setApplicationContext(ApplicationContext applicationContext)
throws BeansException {
context = applicationContext;
System.out.println(applicationContext);
}
}
private ApplicationContext context;
public void setApplicationContext(ApplicationContext applicationContext)
throws BeansException {
context = applicationContext;
System.out.println(applicationContext);
}
}
org.springframework.beans.factory.config.BeanPostProcessor 修改bean
class StudentBean implements BeanPostProcessor {
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;
}
}
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;
}
}
org.springframework.beans.factory.BeanNameAware 自动装配配置文件里该bean的ID
class StudentBean implements BeanNameAware {
private String name;
public void setBeanName(String name) {
this.name = name;
}
}
private String name;
public void setBeanName(String name) {
this.name = name;
}
}
@PreDestroy @PostConstruct
@PostConstruct
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");
}
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();
<context:annotation-config/>
浙公网安备 33010602011771号