Details of the Spring Bean basic management instance

  • 2020-04-01 04:16:58
  • OfStack

This article illustrates the basic management of Spring beans. Share with you for your reference, as follows:

Use setters to complete dependency injection

Below are the beans and the beans-config.xml file.


public class HelloBean { 
  private String helloWord; 
  //. Omit getter and setter methods
}


<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE beans PUBLIC "-//SPRING/DTD BEAN/EN" 
"http://www.springframework.org/dtd/spring-beans.dtd"> 
<beans> 
  <bean id="helloBean" 
     class="onlyfun.caterpillar.HelloBean"> 
    <property name="helloWord"> 
      <value>Hello!Justin!</value> 
    </property> 
  </bean> 
</beans>

public class SpringDemo { 
  public static void main(String[] args) { 
    Resource rs = new FileSystemResource("beans-config.xml"); 
    BeanFactory factory = new XmlBeanFactory(rs); 
    HelloBean hello = (HelloBean) factory.getBean("helloBean"); 
    System.out.println(hello.getHelloWord()); 
  } 
}

Execute an injection using constructor mode


public class HelloBean { 
  private String name; 
  private String helloWord; 
  //It is recommended to have a no-parameter construction method
  public HelloBean() { 
  } 
  public HelloBean(String name, String helloWord) { 
    this.name = name; 
    this.helloWord = helloWord; 
  } 
  //. Omit getter and setter methods
}


<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE beans PUBLIC "-//SPRING/DTD BEAN/EN" 
"http://www.springframework.org/dtd/spring-beans.dtd"> 
<beans> 
  <bean id="helloBean" 
     class="onlyfun.caterpillar.HelloBean"> 
    <constructor-arg index="0"> 
      <value>Justin</value> 
    </constructor-arg> 
    <constructor-arg index="1"> 
      <value>Hello</value> 
    </constructor-arg> 
  </bean> 
</beans>


public class SpringDemo { 
  public static void main(String[] args) { 
    ApplicationContext context = 
      new FileSystemXmlApplicationContext("beans-config.xml"); 
    HelloBean hello = (HelloBean) context.getBean("helloBean"); 
    System.out.print("Name: "); 
    System.out.println(hello.getName()); 
    System.out.print("Word: "); 
    System.out.println(hello.getHelloWord()); 
  } 
}

Three, attribute reference


public class HelloBean { 
  private String helloWord; 
  private Date date; 
  //. Omit getter and setter methods
}


<beans> 
  <bean id="dateBean" class="java.util.Date"/> 
  <bean id="helloBean" class="onlyfun.caterpillar.HelloBean"> 
    <property name="helloWord"> 
      <value>Hello!</value> 
    </property> 
    <property name="date"> 
      <ref bean="dateBean"/> 
    </property> 
  </bean> 
</beans>

public class SpringDemo { 
  public static void main(String[] args) { 
    ApplicationContext context = 
      new FileSystemXmlApplicationContext("beans-config.xml"); 
    HelloBean hello = (HelloBean) context.getBean("helloBean"); 
    System.out.print(hello.getHelloWord()); 
    System.out.print(" It's "); 
    System.out.print(hello.getDate()); 
    System.out.println("."); 
  } 
}

Iv. Automatic binding of "byType"

You can complete the automatic binding of bean properties by type by changing the configuration file in "three" to the following.


<beans> 
  <bean id="dateBean" class="java.util.Date"/> 
  <bean id="helloBean" class="onlyfun.caterpillar.HelloBean" autowire="byType"> 
    <property name="helloWord"> 
      <value>Hello!</value> 
    </property> 
  </bean> 
</beans>

Five, "byName" automatic binding

You can complete the automatic binding of bean properties by name by changing the configuration file in "three" to the following.


<beans> 
  <bean id="dateBean" class="java.util.Date"/> 
  <bean id="helloBean" class="onlyfun.caterpillar.HelloBean" autowire="byName"> 
    <property name="helloWord"> 
      <value>Hello!</value> 
    </property> 
  </bean> 
</beans>

Constructor automatic binding

By changing the configuration file in "three" to the following, you can complete the build-by-construct automatic binding of the bean properties. When establishing a dependency, the Srping container attempts to match the type of Bean instance in the container with the parameter type on the associated constructor to see if it matches the type, if any, of the constructor used to create the Bean instance. If you can't binding, it throws org. Springframework. Beans. Factory. UnsatisfiedDependencyException anomalies.


<beans> 
  <bean id="dateBean" class="java.util.Date"/> 
  <bean id="helloBean" class="onlyfun.caterpillar.HelloBean" autowire="constructor"> 
    <property name="helloWord"> 
      <value>Hello!</value> 
    </property> 
  </bean> 
</beans>

Vi. Automatic binding of "autodetect

By changing the configuration file in "three" to the following, you can complete the automatic binding of the bean property, which Spring will try to use a constructor to handle the dependency establishment and, if not, try to establish the dependency using the byType class.


<beans> 
  <bean id="dateBean" class="java.util.Date"/> 
  <bean id="helloBean" class="onlyfun.caterpillar.HelloBean" autowire="autodetect"> 
    <property name="helloWord"> 
      <value>Hello!</value> 
    </property> 
  </bean> 
</beans>

Vii. Dependent inspection method

In auto-binding, since there is no way to see clearly from the definition file whether each property is set, to determine if some dependency is indeed established, you can check if the dependency is set, in the < Bean> With the TAB set to "dependency check, "there are four dependency checks available: simple, objects, all, and none.

Simple: simply checks whether simple type properties (like native data types or string objects) complete the dependency.
Objects: checks whether the properties of the object type complete the dependency.
All: checks that all attributes complete the dependency.
None: the setting is the default value to indicate that the dependency is not checked.


<beans> 
  <bean id="dateBean" class="java.util.Date"/> 
  <bean id="helloBean" class="onlyfun.caterpillar.HelloBean" autowire="autodetect" dependeny-check="all"> 
    <property name="helloWord"> 
      <value>Hello!</value> 
    </property> 
  </bean> 
</beans>

Eight, set object injection

For collection objects such as arrays, lists, sets, maps, etc., you can also leave it to Spring's IoC container to automatically maintain or generate collection objects and complete dependency injection when some objects must be populated into the collection before injection and then injected into the required beans.


public class SomeBean { 
  private String[] someStrArray; 
  private Some[] someObjArray; 
  private List someList; 
  private Map someMap; 
  public String[] getSomeStrArray() { 
    return someStrArray; 
  } 
  public void setSomeStrArray(String[] someStrArray) { 
    this.someStrArray = someStrArray; 
  } 
  public Some[] getSomeObjArray() { 
    return someObjArray; 
  } 
  public void setSomeObjArray(Some[] someObjArray) { 
    this.someObjArray = someObjArray; 
  } 
  public List getSomeList() { 
    return someList; 
  } 
  public void setSomeList(List someList) { 
    this.someList = someList; 
  } 
  public Map getSomeMap() { 
    return someMap; 
  } 
  public void setSomeMap(Map someMap) { 
    this.someMap = someMap; 
  } 
}
public class Some { 
  private String name; 
  public String getName() { 
    return name; 
  } 
  public void setName(String name) { 
    this.name = name; 
  } 
  public String toString() { 
    return name; 
  } 
}


<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE beans PUBLIC "-//SPRING/DTD BEAN/EN" 
"http://www.springframework.org/dtd/spring-beans.dtd"> 
<beans> 
  <bean id="some1" class="onlyfun.caterpillar.Some"> 
    <property name="name"> 
      <value>Justin</value> 
    </property> 
  </bean> 
  <bean id="some2" class="onlyfun.caterpillar.Some"> 
    <property name="name"> 
      <value>momor</value> 
    </property> 
  </bean> 
  <bean id="someBean" class="onlyfun.caterpillar.SomeBean"> 
    <property name="someStrArray"> 
      <list> 
        <value>Hello</value> 
        <value>Welcome</value> 
      </list> 
    </property> 
    <property name="someObjArray"> 
      <list> 
         <ref bean="some1"/> 
         <ref bean="some2"/> 
      </list> 
    </property> 
    <property name="someList"> 
      <list> 
         <value>ListTest</value> 
         <ref bean="some1"/> 
         <ref bean="some2"/> 
      </list> 
    </property> 
    <property name="someMap"> 
      <map> 
         <entry key="MapTest"> 
           <value>Hello!Justin!</value> 
         </entry> 
         <entry key="someKey1"> 
           <ref bean="some1"/> 
         </entry> 
      </map> 
    </property> 
  </bean> 
</beans>


public class SpringDemo { 
  public static void main(String[] args) { 
    ApplicationContext context = 
      new FileSystemXmlApplicationContext( 
          "beans-config.xml"); 
    SomeBean someBean = 
      (SomeBean) context.getBean("someBean"); 
    //Gets an array type dependency injection object
    String[] strs = 
      (String[]) someBean.getSomeStrArray(); 
    Some[] somes = 
      (Some[]) someBean.getSomeObjArray(); 
    for(int i = 0; i < strs.length; i++) { 
      System.out.println(strs[i] + "," 
          + somes[i].getName()); 
    } 
    //Gets the List type dependency injection object
    System.out.println(); 
    List someList = (List) someBean.getSomeList(); 
    for(int i = 0; i < someList.size(); i++) { 
      System.out.println(someList.get(i)); 
    } 
    //Gets a map-type dependency injection object
    System.out.println(); 
    Map someMap = (Map) someBean.getSomeMap(); 
    System.out.println(someMap.get("MapTest")); 
    System.out.println(someMap.get("someKey1")); 
  } 
}

I hope this article has been helpful to you in Java programming.


Related articles: