IOC control inversion and di injection in spring

Source: Internet
Author: User

Spring's control reversal

  Introduction of 1.spring

   Spring is a popular Java open source framework that revolves around the beanfactory and forms the core.

       

2.IOC Control Inversion

2.1 Definition: Give the object creation to spring management, no new method, no Factory mode

2.2 Principle: 1.xml configuration file, 2.dom4j parsing xml,3. Factory design mode, 4. Reflection

2.3 Use: 1. Configuration file method, 2. Annotation method

  

3.IOC Injection Bean Management

3.1.1 How XML is configured

1. The way spring creates objects in the XML configuration file, that is, the configuration of the bean tags

2. Instantiation mode:

2.1: Use class parameterless constructor (most commonly used)

2.2: Using a static factory

2.3: use instance Factory

3.1.2 Common Properties

1.id: Name based on the id attribute is worth to the configuration object, can be arbitrarily named, but cannot contain special symbols

2.class: The full path of the class where the object is created

The scope of 3.scope:1.bean

2.scope Value: Singleton single case, default

Prototype multiple cases

Request to save the object to the request domain

Session to save the object to the Session field

Globalsession saving an object to the Globalsession domain

  

3.1.3 Attribute Injection

1. When creating an object, set the property value

2. Mode: 1. Using the Constructor-arg sub-label in the bean label using the parametric constructor

2. Use the property sub-label in the bean label using Set mode (most commonly used)

3. Using the interface (basically no, spring does not support this way)

3. Inject Complex Type attribute: Array uses the list child label of the child Tag property of the Bean

List using the list child label of the Bean's Child tag property

Map child tag using the child tag property of the Bean

property uses the props child label of the child Tag property of the Bean

4. For example

<!--1. Set type injected--classclass= "Com.userinfo" ><property name = "username" value = "Zhang San"/><property name = "number" value= "1001"/><property name = "P" ref= "per"/></bean>
<!--2. Include list or map injection mode--class= "Com.userinfo" ><property name= "username" value= "Zhang San"/ ><property name= "number" value= "1001"/><property name= "per" ><list><value> Zhang San </value ><value> John Doe </value><value> Harry </value><value>hello</value></list> </property></bean>

3.2.1 Injection of beans by annotations

1. Notation: @ Note Name (attribute name = attribute value), you can use annotations on classes, methods, properties.

Annotations can be used instead of configuration files, but not completely configured, just reduced configuration

2. Turn on annotation scanning: Configure <context:compoment-scan base-package= "" in XML ></con

TEXT:COMPOMENT-SCAN>BASE-PACKAGE Specifies the package name spring will scan

3. Create object annotations: @Component (@Controller: Web Tier, @Service: Business layer

@Repository: Persistence layer These three annotations are the derivative annotations of the component annotation, and in order to make the label class itself clear, the four annotation functions are now consistent, and will be enhanced by subsequent versions of spring)

The annotation for the Scope attribute in the bean is @scope, which can be used under four annotations

4. Annotation of the injected attribute: @Autowired automatically find the corresponding object instance based on the class name

@Resource (name= the object name specified in the class annotation for the object to be introduced) can specify the object

5. Annotation examples of injected attributes

5.1 @component, @Controller, @Service, @Repository injection method

First XML file (definition Scanner)

              <!--scanner--><context:component-scan base-package= "/com"/>

Description, assembled using @value (used to assemble object properties) @Autowired (Assembly object domain properties, belonging to Bytype injection) @Resource ("Value") (Object domain properties, which belong to ByName injection) @Resource ( Without parameters is Bytype injection) (Note: @Qualifier ("") is also byname injection, but must be preceded by @autowired)

The whole life of 5.2 bean

@PostConstruct (initial), @preDestroy (Destroy) (Learn what)

5.3 Using Javaconfig to configure (Learn content)

5.4 Using JUNIT4 Test (Learn content)

5.5 XML file configuration with higher precedence than annotations

3.2.2 XML configuration file and annotations in mixed use

Creating objects using an XML configuration file

Injecting attributes using annotations

IOC control inversion and di injection in spring

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.