Bean Configuration in Spring

Source: Internet
Author: User

Summary IOC & DI Overview • Configure the bean– configuration form: XML file-based approach, annotation-based –bean configuration: Through the full class name (reflection), through the factory method (static factory method & Instance Factory method), FACTORYBEAN–IOC Container Beanfactory & ApplicationContext Overview – Dependency Injection: Attribute injection, constructor injection – Injection of attribute value details – auto-transpose relationship between –bean: inheritance; scope of dependency –bean: Singleton ; Prototype;web Environment Scope – Use the external properties file –spel–ioc The life cycle of the Bean in the container –spring 4.x new features: generic dependency Injection IOC and di 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 lookup di (Dependency injection)-ioc Another way of expressing that a component accepts a resource injection from a container, such as a setter method, in some pre-defined way. In contrast to the IOC, this statement is more direct to the IOC---separation interface and implementation

Requirements: Generate different types of reports in HTML or PDF format

The IOC---used factory design patterns

The IOC---uses inversion control

In the Spring IOC container, configure Bean to configure the name of the bean Id:bean in the XML file through the Bean node. – must be unique within the IOC container – if the ID is not specified, spring automatically assigns the permission to the name of the Bean – ID can specify multiple names, separated by commas, semicolons, or spaces from the spring container • Read Bean configuration creation in spring IOC container The Bean instance must be instantiated before it is initialized. Only after the container is instantiated can you get the Bean instance from the IOC container and use the. Spring provides two types of IOC container implementations. The basic implementation of the –BEANFACTORY:IOC container. –applicationcontext: Provides more advanced features. is a beanfactory subinterface. –beanfactory is the spring framework infrastructure that targets spring itself, and ApplicationContext is intended for developers using the spring framework, and is used directly in almost all applications Applic Ationcontext, rather than the underlying beanfactory–, is the same regardless of the way the configuration file is used. ApplicationContext ApplicationContext Main Implementation class: –classpathxmlapplicationcontext: Load configuration file from Classpath- Filesystemxmlapplicationcontext: Loading configuration files from the file system Configurableapplicationcontext extended to ApplicationContext, The new addition of two main methods: Refresh () and close (), gives ApplicationContext the ability to start, refresh, and close the context ApplicationContext all singleton beans When the context is initialized. Webapplicationcontext is intended for Web applications and allows initialization from the path relative to the Web root to get bean calls from the IOC container ApplicationContext Getbean () method Dependency Injection The way Spring supports 3 types of dependency Injection – Attribute injection – Constructor injection – factory method injection (rarely used, not recommended) 1. Attribute injection • Attribute injection through setterMethod injects a Bean's attribute value or dependent object • Attribute injection use the <property> element, use the Name property to specify the Bean's property name, the Value property or the <value> child node specifies the attribute value • Attribute injection is the most commonly used injection in a practical application Method Constructor Injection • Inject the bean's property value or dependent object by constructing the method, which guarantees that the bean instance can be used after instantiation. • Constructor injection declares the attribute in the <constructor-arg> element, <constructor-arg> does not have a name attribute • Match into the parameter by index: • Match input by type: literal value • Literal: values represented by a string can be The <value> element tag or the Value property is injected. • The basic data type and its encapsulation class, String, etc. can be injected in literal form • If the literal contains special characters, you can use the <! [cdata[]]> wrapped the literal value together. Beans that reference other bean to compose applications 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 using 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 internal bean bean, which can be declared as an internal bean when the bean instance is used only for a specific property. The internal Bean Declaration is included directly in the <property> or <constructor-arg> element, and no ID or name attribute is required • Internal Bean cannot be used anywhere else to inject parameter details: null value and cascading property • Use dedicated <null/> element tags to inject null values for attributes of Bean strings or other object types • Like Struts, Hiberante, Spring supports the configuration of cascading properties. Collection Properties • The collection properties can be configured in spring through a set of built-in XML tags (for example: <list>, <set> or <map>). • Configure properties of the Java.util.List type, you need to specify <list>   tags, including 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, using the <list> configuration Java.util.Set need to use the <set> tag, the method of defining elements is the same as list. Java.util.map through < map> label definitions, <map> tags can use multiple <entry> as sub-tags. 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, it is freely possible to specify &LT;VALUE&GT 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. Use utility scheme to define a collection • When you define a collection using a Basic collection label, you cannot define the collection as a standalone bean, causing the collection to be referenced by other beans, so it is not possible to Shared collections. • You can use the collection tags in the UTIL schema to define a separate collection Bean. Note that you must add the Util schema definition   Use the P namespace in the <beans> root element • In order to simplify the configuration of XML files, more and more XML files use attributes rather than child element configuration information. Spring introduced a new P-namespace 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 the XML configuration Bean Automatic assembly Spring IOC container can automatically assemble beans. The only thing that needs to be done is to specify the automatic assembly mode Bytype (automatically assembled by type) in the Autowire attribute of <bean>: If there are multiple beans in the IOC container that are consistent with the target bean type. In this case, Spring will not be able to determine which Bean is most suitable for this property, so automatic assembly cannot be performed. byname (Auto-assemble by name): The name and property name of the target Bean must be set exactly the same. constructor (automatically assembled via the constructor): when This type of automatic assembly can be complicated when there are multiple constructors in the Bean. The disadvantage of automatic assembly of beans in XML configuration is deprecated • Setting the Autowire property in the Bean configuration file for automatic assembly will assemble all of the bean's properties. However, if you only want to assemble individual properties, the Autowire property is not flexible enough. The Autowire property is either automatically assembled by type or automatically assembled by name, not both. • In general, the automatic assembly function is rarely used in actual projects, Clear and clear configuration documents are more persuasive because of the benefits of automated assembly functions                           

Bean configuration in Spring

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.