2 The configuration of the Spring4 bean

Source: Internet
Author: User

Configuration of the bean of the Spring4

1 IOC & DI Overview

  • IOC (Inversion of Control): The idea is to reverse the direction of resource acquisition . The traditional resource lookup method requires that the component initiate a request lookup resource to the container. In response, the container returns resources in a timely manner. When the IOC is applied, it is the container that proactively pushes the resources to the components it manages, and what the component does is to choose an appropriate way to accept resources . This behavior is also known as the passive form of finding
  • Another way to express DI (Dependency injection)-IOC is that the component accepts resource injections from such containers in a pre-defined way (for example, setter methods) . In contrast to the IOC, this statement is more straightforward

?

2 Configuring Beans

    • configuration form: XML file -based approach; annotation-based approach
    • how beans are configured: Through the full class name (reflection), through the factory method (static factory method & Instance Factory method), Factorybean
    • IOC container beanfactory & ApplicationContext Overview
    • Method of Dependency Injection: attribute injection; constructor injection
    • Inject attribute value details
    • Auto-Transfer
    • The relationship between beans: inheritance; dependency
    • Bean Scope: Singleton;prototype;web Environment scope
    • Using the external properties file
    • Spel
    • The life cycle of beans in an IOC container
    • Spring 4.x new features: generic Dependency Injection

3 Configuring Beans in Spring's IOC container

  • To configure a bean in an XML file through a bean node

  • The name of the Id:bean.
    • Must be unique within the IOC container
    • If the ID is not specified, Spring automatically assigns the permission class name to the Bean
    • ID can specify more than one name, separated by commas, semicolons, or spaces
  • Before the Spring IOC container reads the bean configuration to create the bean instance, it must be instantiated. The bean instance can be obtained from the IOC container and used only after the container is instantiated .
  • Spring provides two types of IOC container implementations.
    • beanfactory: The basic implementation of the IOC container.
    • ApplicationContext: provides more advanced features. is the sub-interface of the beanfactory.
    • Beanfactory is the spring framework infrastructure for spring itself; ApplicationContext for developers using the spring framework, almost all applications are used directly ApplicationContext rather than the underlying beanfactory (mainly using ApplicationContext configuration Bean)
    • The configuration file is the same regardless of the way it is used.

3.1 ApplicationContext

    • ApplicationContext's main implementation class:
      • classpathxmlapplicationcontext: Loading a configuration file from a classpath
      • Filesystemxmlapplicationcontext: Loading configuration files from the file system
    • Configurableapplicationcontext expanded to ApplicationContext, adding two new main methods: Refresh () and Close (), allowing ApplicationContext Ability to start, refresh, and close contexts
    • ApplicationContext instantiates all singleton beans when the context is initialized.
    • Webapplicationcontext is intended for Web applications and allows initialization to be done from a path relative to the Web root directory

3.2 Getting beans from the IOC container

    • Call ApplicationContext's Getbean () method

3.3 Ways to rely on injection

3.3.1 Attribute Injection

    • Property injection is a setter method that injects the bean's property values or dependent objects
    • Attribute injection uses the <property> element, using the Name property to specify the Bean's property name, the Value property, or the <value> child node to specify the property value
    • Attribute injection is the most commonly used injection method in practical applications.

3.3.2 Construction Method Injection

    • By constructing a method to inject the Bean's property value or dependent object, it guarantees that the bean instance is ready for use after it is instantiated.
    • constructor Injection declaration attribute in <constructor-arg> element , no Name attribute in <constructor-arg>
    • Match into parameters by index:(the attributes in the constructed method are matched sequentially)

    • Match entry by Type:

3.4 Details of attribute injection

3.4.1 literal value

    • Literal: A value that can be represented by a string, injected by the <value> element label or by the Value property.
    • The basic data type and its wrapper class, String, and so on can be injected in a literal way
    • If the literal contains special characters, you can use the <! [cdata[]]> wrapped the literal value together.

3.4.2 References other beans contain beans

    • The beans that make up the application often need to collaborate with each other to complete the functionality of the application. To enable beans to access each other , you must specify a reference to the bean in the bean configuration file
    • In the bean's configuration file, you can specify a reference to the bean by either the <ref> element or the Ref property for the Bean's property or constructor parameter.
    • You can also include a bean's declaration in a property or constructor , which is called an internal bean, and an internal bean cannot be referenced by an external bean without setting an ID (it is useless)

?

3.4.3 Internal Bean

    • When a bean instance is used only for a specific property, it can be declared as an internal bean. The internal Bean declaration is included directly in the <property> or <constructor-arg> element and does not require any ID or Name property to be set
    • Internal beans cannot be used anywhere else

Injection parameters in detail: null values and cascading properties

    • You can use a dedicated <null/> element tag to inject a null value into a Bean's string or property of another object type (with little meaning)
    • Like Struts, Hiberante and other frameworks,Spring supports the configuration of cascading properties .

3.4.4 Collection Properties

    • The collection properties can be configured in spring through a set of built-in XML tags (for example: <list>, <set> or <map>).
    • To configure a property of type java.util.List, you need to specify the <list> tag, which contains some elements in the tag. These tags can specify a simple constant value by <value> and a reference to another Bean through <ref> . Specify the built-in bean definition through <bean> . Specifies an empty element by <null/>. You can even embed other collections.
    • Arrays are defined in the same way as lists, and are used <list>
    • Configuring Java.util.Set requires the use of <set> tags, and defines elements in the same way as List.

    • Java.util.Map is defined by the <map> tag and multiple <entry> as sub-tags can be used in the <map> tag. Each entry contains a key and a value.
    • Keys must be defined in the <key> tab
    • Because there are no restrictions on the types of keys and values, you are free to specify <VALUE> for them, <ref>, <bean> or <null> elements.
    • You can define the key and value of the MAP as a property of <entry>: Simple constants are defined using key and value; Bean references are defined by the Key-ref and Value-ref properties
    • Use <props> to define java.util.Properties, which uses multiple <prop> as sub-labels. Each <prop> tag must define the key property.

3.4.5 defining a collection using utility scheme

    • When you define a collection with a basic collection label, you cannot define the collection as a standalone bean, so that the collection cannot be referenced by other beans, so the collection cannot be shared between different beans .
    • You can use the collection tags in the UTIL schema to define a separate collection Bean. It is important to note that the Util schema definition must be added to the <beans> root element

3.4.6 using the P namespace

    • To simplify the configuration of XML files, more and more XML files use attributes rather than child element configuration information.
    • Spring has introduced a new P-namespace starting from version 2.5, which allows you to configure the properties of a bean by <bean> element properties.
    • With the P namespace, XML-based configuration will further simplify

2 The configuration of the Spring4 bean

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.