Spring Framework Core Concepts (a)

First, an example of the way Bean

  1, the constructor of Example : In this way, the configuration file written as follows

1 <bean id="exampleBean" class="com.mazhuo.ExampleBean">
2 </bean>

  2, using the example of a static factory method :

    When using this method, in addition to the setting value class, set the required factory-method property specifies a method Bean instance created. Profile worded as follows:

1 <bean id="exampleBean" class="com.mazhuo.ExampleBean" factory-methord="creatBean">
2 </bean>

    Class corresponding to the application defined as follows:

1 public class ExampleBean{
2     private static ExampleBean exampleBean = new ExampleBean();
3     private ExampleBean(){}
4     public static ExampleBean creatBean(){
5          return exampleBean;  
6     }
7 }

  3, using the factory method to instantiate instances :

    This method is similar to the second method, when using this method, class attribute is set to null, and the factory-method attribute values ​​must specify the name of the current bean (or an ancestor) container comprising a plant process, and the factory bean factory method itself must be specified by factory-method attribute. Wherein there may be a plurality of factory class factory method. Profile worded as follows:

1  <! - plant Bean, comprises creatBean () Method -> 
2  < the bean ID = "serviceLocator" class = "com.mazhuo.ServiceLocator" > 
. 3          <! - Other dependency injection -> 
4  </ the bean > 
. 5  
. 6  <-! Bean plants created by Bean -> 
. 7  < the bean ID = "ClientService" factory-the bean = "serviceLocator" factory-methord = "creatBean" > 
. 8  </ the bean >

      Arranged above class definition file:

1 public class ServiceLocator {
2     private static ClientService clientService = new ClientService();
3     private ServiceLocator(){}
4     public ClientService creatBean() {
5         return clientService;
6     }
7 }

Two, Bean injection method

  1, based on constructors:

    By calling the constructor of the container having a plurality of parameters to complete, identify the dependencies of each parameter, which is configured to call the static method bean plants are almost equivalent.

. 1  public  class the SimpleMovieLister {
 2      // the SimpleMovieLister dependent on MovieFinder 
. 3      Private MovieFinder MovieFinder;
 . 4      // the Spring MovieFinder container may be injected by the constructor 
. 5      public the SimpleMovieLister (MovieFinder MovieFinder) {
 . 6          the this .movieFinder = MovieFinder;
 . 7      }
 . 8 }

       In the above code, the configuration file required <constructor-arg> element specifies the constructor parameter or index type.

    Developers can also remove the ambiguity parameters by name, but it should be noted that this method works in code debugging must be enabled flagged by the compiler, developers can also use @ConstructorProperties marked to show the name of the constructor is declared. Reference code is as follows

1 public class ExampleBean {
2     private int year;
3     private String name;
4     @ConstructorProperties({"year","name"})
5     public ExampleBean(int year, String name){
6         this.name = name;
7         this.year = year;
8     }
9 }

  2, based setter methods:

    After invoking a no-argument constructor or a static factory method without parameters to instantiate bean, accomplished by calling the setter method.

Third, automatic assembly

  1、@configuration:

    Used to define the configuration class, alternative xml configuration file, the internal class annotated with one or more methods are annotated @Bean, these methods will be AnnotationConfigApplicationContext AnnotationConfigWebApplicationContext classes or scanning, and used to construct bean definition.

    Note : @Configuration annotation configuration class has the following requirements: @Configuration may not be the final type; @Configuration is not anonymous class; nested configuration is a static class.

  2, @ Bean annotation registration bean, and you can specify initialization and destruction methods

     @Bean(name="testBean",initMethod="start",destroyMethod="cleanUp")

  3, spring scope attribute in the bean, the following five types:

             Example singleton represents a single spring container, always returns the unique instance of the bean by a spring when the container is obtained.
           prototype represents bean-per-acquisition will generate a new object.
       It represents a valid request (only web application) in a http request.
       It represents a valid session (only web application) within a user session.
       globalSession represents a valid (only web application) in the global session.

  4, @ Configuation equivalent to <Beans> </ Beans>

     @Bean equivalent to <Bean> </ Bean>

    @ComponentScan等价于<context:component-scan base-package="com.dxz.demo"/>

Guess you like

Origin www.cnblogs.com/ma-zhuo/p/11065753.html