Sunday, April 24, 2016

Spring DI(Dependency Injection)


singleton: initialized instance when bean creates by application context
prototype: one bean to many instances
request: get a new instance for a bean in each request
session: get a new instance for a bean in each session
global session: get a new instance for a bean in whole spring container

Lazy mode

Spring default is not lazy mode, when spring starts up, it inits all beans.

If we want bean to init when calling ac.getBean()
do this:
<beans .... default-lazy-init="true">


public class Bean1 {
 private String strValue;
 private int intValue;
 private List listValue;
 private Set setValue;
 private String[] arrayValue;
 private Map mapValue;
 public String getStrValue() {
  return strValue;
 public void setStrValue(String strValue) {
  this.strValue = strValue;
 public int getIntValue() {
  return intValue;
 public void setIntValue(int intValue) {
  this.intValue = intValue;
 public List getListValue() {
  return listValue;
 public void setListValue(List listValue) {
  this.listValue = listValue;
 public Set getSetValue() {
  return setValue;
 public void setSetValue(Set setValue) {
  this.setValue = setValue;
 public String[] getArrayValue() {
  return arrayValue;
 public void setArrayValue(String[] arrayValue) {
  this.arrayValue = arrayValue;
 public Map getMapValue() {
  return mapValue;
 public void setMapValue(Map mapValue) {
  this.mapValue = mapValue;

 <bean id="bean1" class="com.gvace.domain.Bean1">
  <property name="strValue" value="abc"/>
  <property name="intValue" value="123"/>
  <property name="listValue">
  <property name="setValue">
  <property name="arrayValue">
  <property name="mapValue">
    <entry key="k1" value="v1"/>
    <entry key="k2" value="v2"/>

No comments:

Post a Comment