spring Bean instantiated in three ways:
1, using the example of the class constructor; (no default parameters, generally used in this way)
2, using a static factory method instantiates (simple factory pattern);
3, Example Chemical instantiated (factory methods).
Example 1. Use of class constructor:
(1) bean class
/ **
* Bean instantiated three ways: using the constructor with no arguments way
* /
public class Bean1 {
public Bean1 () {
System.out.println ( "Bean1 is instantiated ...");
}
}
(2) configuration file:
<- first:! No argument constructor manner ->
<-! <The bean ID = "Bean1" class = "com.imooc.ioc.demo2.Bean1" /> ->
2, static factory method to instantiate
(1) bean class
/ **
* Bean instantiation of three ways: a static factory way of example
* /
public class Bean2 {
}
/ **
* Bean2 static factory
* /
public class Bean2Factory {
Bean2 createBean2 static public () {
System.out.println ( "Bean2Factory method has been performed ...");
return new new Bean2 ();
}
}
(2) configuration file:
<- The second:! Static factory way ->
! <- <bean the above mentioned id = "bean2" class = "com.imooc.ioc.demo2.Bean2Factory" = Factory's-Method, "createBean2" /> - >
3, examples of the chemical Example
1) bean class:
/ **
instantiation * Bean of three ways: instantiating an instance factory
* /
public class Bean3 {
}
/ **
Examples of plant * Bean3
* /
public class Bean3Factory {
public Bean3 createBean3 () {
System.out.println ( "Bean3Factory performed ...");
return new new Bean3 ();
}
}
(2) configuration file:
<- Third:! Example plant way ->
! <- <the bean ID = "bean3Factory" class = "com.imooc.ioc.demo2.Bean3Factory" />
<the bean ID = "bean3" factory- bean = "bean3Factory" factory-method = "createBean3" /> ->