Java Interview--spring Technical Essentials--spring IOC

Source: Internet
Author: User

7 Spring IOC (Dependency injection) way

    1. Setter Method Injection
    2. Constructor Method Injection
    3. Factory method Injection (instance factory/static factory)
    4. Automatic assembly (requires open configuration, not recommended )

http://blessht.iteye.com/blog/1162131

8 IOC Mode recommendations (constructor injection or setter method injection)

Both methods of dependency can be used, constructor injection and setter method injection. The best solution is to implement a forced dependency with constructor parameters, and thesetter method implements an optional dependency.

9 What is spring beans

Springbeans are Java objects that form the backbone of the spring application. They are initialized, assembled, and managed by SPRINGIOC containers. These beans are created through the metadata that is configured in the container. For example, it is defined in the form of <bean/> in an XML file.

The beans defined by the Spring framework are all singleton beans. There is an attribute "singleton" in Beantag, if it is assigned to True,bean is a singleton, otherwise it is a prototype bean. The default is true, so all beans defaults in the spring frame are singleton.

what is included in the Spring bean definition

A Springbean definition contains all the configuration metadata that a container must know, including how to create a bean, its lifecycle details, and its dependencies.

http://blog.csdn.net/yuqinying112/article/details/7245837

A spring container provides a way to configure metadata

Here are three important ways to provide configuration metadata to the spring container.

    1. XML configuration file.
    2. Annotation-based configuration.
    3. Java-based configuration.

http://jinnianshilongnian.iteye.com/blog/1463704

scope of Spring class

The Spring framework supports five scopes (three of which are available only in Web-based springapplicationcontext).

Table 2 Spring Scope scope detailed

Singleton

A bean definition corresponds to an object instance in each spring IOC container.

Prototype

A bean definition corresponds to multiple object instances.

Request

In an HTTP request, a bean definition corresponds to an instance, that is, each HTTP request will have its own bean instance, which is created from a bean definition. This scope is valid only in the case of a web-based spring ApplicationContext.

Session

In an HTTP session, a bean definition corresponds to an instance. This scope is valid only in the case of a web-based spring ApplicationContext.

Global session

In a global HTTP session, a bean definition corresponds to an instance. Typically, this is only valid when using the Portlet context. This scope is valid only in the case of a web-based spring ApplicationContext.

http://blog.itpub.net/25261409/viewspace-1055868/

Spring's singleton bean is not thread safe

No, singleton beans in the spring framework are not thread-safe.

the life cycle of beans in spring

  1. Bean Creation : The spring container reads the bean definition from the XML file and instantiates the bean.
  2. attribute Injection : Spring populates all properties based on the Bean's definition.
  3. If the bean implements the Beannameaware interface, spring passes the Bean's ID to the Setbeanname method.
  4. If the bean implements the Beanfactoryaware interface, Spring passes beanfactory to the Setbeanfactory method.
  5. If the bean implements the Applicationcontextaware interface, Spring passes ApplicationContext to the Setapplicationcontext method.
  6. If any beanpostprocessors,spring associated with the bean are called within the postprocesserbeforeinitialization() method.
  7. If the bean implementation is Intializingbean , call its Afterpropertyset method, and if the bean declares the initialization method, call this initialization method.
  8. Execute Bean Init-method: You can use the "Init-method" property in the bean definition file to set the method name for example: <beancalss= "Onlyfun.caterpillar.HelloBean "Init-method=" > "Initbean".
  9. If there are beanpostprocessors and bean associations, the postprocessafterinitialization() method of these beans will be called.
  10. If the bean implements Disposablebean, it will call the Destroy () method.
  11. Execute Bean Destroy-method: When the container is closed, you can use the "Destroy-method" property in the bean definition file to set the method name, for example: <bean destroy-method= " Destroybean ">

http://blog.163.com/tangyang_personal/blog/static/46229613200832235353419/

internal bean of spring

Spring's internal bean, when injected, can only be used for the current bean attribute injection and cannot be referenced outside. In spring's XML-based configuration metadata, <bean/> elements can be used within the <property/> or <constructor-arg/> elements, and internal beans are usually anonymous. Their scope is generally prototype.

inject Java collections into spring

Spring provides configuration elements for the following collections:

    • The <list> type is used to inject a column of values, allowing the same value.
    • The <set> type is used to inject a set of values that do not allow the same value.
    • The <map> type is used to inject a set of key-value pairs, and the keys and values can be any type.
    • The <props> type is used to inject a set of key-value pairs, and the keys and values are only string types

Bean Assembly and automatic Assembly

Bean Assembly : The Bean is assembled in a spring container, provided that the container needs to know the bean's dependencies and how to assemble them together through dependency injection.

Spring containers can automatically assemble mutually cooperative beans, which means that containers do not require <constructor-arg> and <property> configuration, and can automatically handle collaboration between beans through the bean factory.

how spring is automatically assembled

There are five automatic assembly methods that can be used to instruct spring containers to use automatic assembly for dependency injection.

    • No: The default way is not to assemble automatically, but by explicitly setting the Ref property.
    • byname: Automatically assembled by the parameter name, the spring container discovers that the bean's Autowire property is set to ByName in the configuration file, and then the container tries to match, assemble, and bean whose attributes have the same name.
    • bytype:: With automatic assembly of parameter types, the spring container discovers that the bean's Autowire property is set to Bytype in the configuration file, after which the container tries to match, assemble, and have the same type of bean as the bean's property. If there are multiple Bean If the condition is met, an error is thrown.
    • Constructor: This is similar to Bytype, but to give the constructor arguments, an exception will be thrown if there is no deterministic constructor parameter type for the parameter.
    • AutoDetect: First try to use constructor to move the assembly, if not, use the Bytype method.

http://blog.csdn.net/wangli325/article/details/7471327

19 Limitations of automatic Assembly

The limitations of automatic assembly are:

    • rewrite : You still need <constructor-arg> and <property> configuration to define dependencies, which means you always rewrite automatic assembly.
    • Basic Data type : You cannot automatically assemble simple properties such as basic data types, string strings, and classes.
    • Fuzzy characteristics : Automatic assembly is not as precise as explicit assembly, it is recommended to use explicit assembly if possible.


Java Interview--spring Technical Essentials--spring IOC

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.