spring configuration detail and springboot

  1. ApplicationContext BeanFactory interface is an extension of ,, in addition to DI services, ApplicationContext also provides news source and application event handling affairs, AOP services, internationalization and so on.
  2. <Context: component-scan> tag tells Spring scan code to find used @ Component, @ Controller, @ Serivice, @ Repository annotation inject bean, as well as support the use of @ Autowired at a given package, @ Inject, @ Resource annotation bean.
  3. @Configuration annotation class is used to specify the configuration class, the method may be used in this type of annotation @Bean;
  4. @ComponentScanning annotation is a java class configuration, and <context: component-scan> Similarly tag;
  5. Jdk @Resource annotation is itself comes, to support name parameter requirement for finer DI; @Autowired annotation spring is provided, @ Autowired uses reflection to fill the required dependency;
  6. @Service is a special case of @Component, @ Service show annotated classes are providing business services to other layers in the application;
  7. Find method (Lookup Method Injection) @Lookup injection work is to find a single case statement method that returns an instance of non-singleton Bean. CGlib spring used to generate an object subclass of the class to dynamically override this method;
  8. Alternatively MethodReplacer method implemented by the interface, replacing any method in the target, it is actually used CGlib embodiment;
  9. @Autowired injected with @Qualifier annotation specifies specific bean name;
  10. Bean naming rules: set id for the bean, the bean name id, if no id, spring looks for the name attribute (the first value, name can be set in more than one name), if the id and name attributes are not set, will the class name as the name;
  11. If there are two different bean names of the project, but the use of the same class, such as foo and superfoo, if you want to quote superfoo changed foo, you can delete superfoo the bean, then add an alias superfoo behind the name foo;
  12. By default, all spring bean are single embodiment, may be used scope is set the prototype (prototype patterns) and the like;
  13. the prototype (prototype patterns) spring representing each application program requests the bean instance instantiates a new instance of the bean;
  14. When choosing a single example:
    1. No state of shared objects;
    2. Only the shared object read state;
    3. Having a shared state shared objects;
    4. High-throughput target has written state (a large number of applications to create an object, you can use synchronous write mode control state)
  15. Examples of bean scopes
    1. Scope singleton
    2. Prototype scope
    3. Request scope
    4. Session scope (http request with the session bean action)
    5. Global session scope
    6. Scope thread
    7. Custom scopes
  16. Injection sequence arranged according to need when the bean xml configuration, to prevent a case where the uninitialized bean, bean carried out by another injection;
  17. If the bean is not configured in accordance with the order when the xml configuration injection, implantation may be performed by arranging a dependent bean depends-on flag (specified by the dependent bean front depends-on attribute bean, bean-dependent pre-created prior to the present example of the bean good), we need to pay attention to make a Abean is dependent on another Bbean, Abean need to implement ApplicationContextAware interfaces;
  18. ApplicationContextAware interface provides a characteristic has a spring interface, all Bean Spring container will detect the container, then if we find Bean implements ApplicationContextAware interfaces, Spring container will create the Bean, automatically call setApplicationContextAware the Bean's () method, call the when the method, the container itself will applicationContext instance variable as a parameter to the method, while assigned to that object, so the next can be accessed by the vessel itself applicationContext instance variable, the process is completed after call bean constructor of.
  19. bean automatic assembly can configure the automatic assembly mode autowire = "byName" in xml
    1. byName mode
    2. byType
    3. Constructor
    4. The default mode
    5. no
  20. For a bean class has two configurations, it may be added in a primary bean configuration flag, which is set bean (a different type of the same name) preferentially used; for example, a class A, two bean configured in XML, respectively beanname of a1, a2, but the use of the injected code: a Private a ; default the byType this case, there are found two meet implantation conditions, can be solved using the primary case;
  21. For 20 cases occur, it can also be used @Autowired injected with @Qualifier annotation specifies specific bean name;
  22. init-method, method of performing the initialization of the class specified in the configuration xml
  23. InitializingBean interface, the interface can be obtained bean spring completion notification has been configured, i.e. the information can be obtained for all bean spring, a default value may be set or detecting bean bean configuration is correct;
  24. @Postconstruct annotation applied methods similar init-method
  25. @Bean (initMethod = "init") Similarly xml configuration init-method
  26. Bean load order
    1. First, call the constructor to create the bean
    2. Note dependencies (mobilized setter)
    3. Now bean already exists and provides dependencies, pre-initialized bean BeanPostProcessor institutional basis will be queried to check to see if they want to call the task something (@Postconstruct notes) created from the bean
    4. InitializingBean interface implemented afterPropertiesSet () for performing dependency immediately after injection;
    5. Finally, the implementation of init-method property, because it is the actual initialization method of the bean;
  27. BeanNameAware interface, the interface class implements setBeanName method, values ​​can be obtained by this method beanName, configuration values ​​can be achieved when beanName print log as part of the log to facilitate the inquiry;
  28. ApplicationContextAware interface by implementing this interface can be obtained throw configuration bean ApplicationContext application example, the reason this interface is created in order to allow access to ApplicationContext bean in spring applications, such as bean object is obtained by the specified name getBean method.
  29. FactoryBean interface that acts as a spring adapter can not use the standard semantics of creating and managing objects; FactoryBean can automatically access to resources in the transaction and proxy JNDI context.

Guess you like

Origin www.cnblogs.com/use-D/p/12634039.html