Spring (i) IOC Commentary

Source: Internet
Author: User

I. What is the ioc?

IOC: control inversion, in Layman's terms, the creation of objects to spring, we need a new object, the spring to help us create, and then for us to Use.

Write the HelloWorld of the Ioc. Three simple steps to get it Done.

1. Import JAR Package

5 packages, 4 Core packs, 1 dependent packages

              

              

2. Write the configuration file

Spring helps us create objects, but we also have to have information about the classes, otherwise how can we create them?

Profile Name: Recommendation: applicationcontext.xml, take the other can also, but to take the suggested name, it is easier to find the configuration file,

Configuration file location: arbitrary, usually under Classpath (src)

File constraint content: add constraints, spring-framework-3.2.0.release to Spring's core docs for help documentation

Constraint position: spring-framework-3.2.0.release\docs\spring-framework-reference\html\xsd-config.html

              

                    

                

3. Getting objects from the spring container

            

    

Userservice.java

              

Ii. Dependency Injection (DI)

Now you should know what is Ioc,helloworld is very simple, but spring if only to provide such a simple function, you can not be so worshipped, above just the IOC this function for everyone to simply display, so that we have the idea of what the IOC is doing, what can be done, Strengthen it NOW.

Dependency Injection (DI): When spring creates object a, object b, which is dependent on object a, is also created and automatically injected into object A.

Dependency: (have A) has one meaning, such as that there is a Class B in class a, then say a depends on B. inheritance, implementation (is A)

                                  

And what we say about dependency injection is that when we create a object, we create the B object and automatically inject it into object a, and say, I just call spring to give me a object, but a can use the B object. This is very useful, but how to implement the dependency injection function?

            

            

UserService is used in the Userdao object, which we are familiar with the use of tricks, service layer calls the DAO layer Method. As we always do, we need to make our own new Userdao object in the service, but we don't need it in spring, and all the objects let Spring help us create it.

Applicationcontext.xml

            

The process is drawn, the UserService object is created, and then the dependency injection is completed by locating the userdaoid and creating the Userdao object based on the ref in the property, and then injecting it through the setter method, based on the name in the Property.

Test:

          

      

three, Spring three ways to create an instance object

Because not every bean is a generic class, some are obtained through a factory, and factories have static factories and instance factories, so spring creates instance objects in three cases,

The default parameterless construction, which is the way to write IOC helloworld, directly <bean id= "class=" ">

Static factory: because it is a static factory, it is possible to invoke the method of creating an instance directly from the factory class.

<bean id= "" class= "factory fully qualified class name" factory-method= "static method" >

Instance factory: you need to create a factory instance first, and then create an object from the factory

<bean id="myfactoryid" class="factory fully qualified class name"></bean>

<bean id= "" factory-bean= "myfactoryid" factory-method= "" ></bean>

      

Iv. attribute Dependency injection based on XML

The previous dependency injection is also in the category of attribute dependency injection, and at the very beginning we demonstrate that the IOC (control inversion), which is the spring helping us to create an instance, is simply created through the non-parametric construction method, so in actual development it is not possible to create, So we need to configure some property information in Spring's configuration file so that when spring helps us create it, we can inject some of the object's properties into it, and there are two ways

By constructing methods to inject

              

Both normal and reference data can be injected by constructing a method.

Injection by Setter method

               

By using setter methods to inject common and referenced properties, what if there are collections in the created object? So how do you get into the injection?

Collection of injection list, Map, set, array, properties, and so On.

List

                

Set

              

Map

              

Array

              

Propertyis

              

Note: properties This type of storage is similar to map, stored as key and value,

                  

here, basically all the types of properties that can be injected are explained, the injected type is basically divided into three blocks, ordinary types, reference types and Collections. The following is a comment-based injection of various, because the xml-based feel is Cumbersome.

V. Attribute dependency injection based on annotations

Annotation Format: @xxx

Use annotations: you must scan where annotations are used, otherwise the annotations are Useless. and scanning needs to do two things.

1. Add namespaces,

In the location where we look for constraints in the configuration file that: spring-framework-3.2.0.release\docs\spring-framework-reference\html\xsd-config.html Locate the namespace for the Context.

              

                       

2, Scan the specified directory,

                  

Annotations:

1, @Component Replace <bean id= "" class= "" > can configure any bean, add the annotation on the same class,

The @Component ("userid") userid is equivalent to the id attribute value in the bean

                 

2, these three are the same as the use of @component, but in order to better reflect the three-tier architecture, there are three annotations

@Controller Modify classes in the Web Tier.

@Service class that modifies the Service layer

@Repository classes that modify the DAO layer

Two ways, one that does not declare a name, and one that declares the name

@Controller ("xxx") @Controller

If you declare a name, you can use the combination of @autowired or @autowired with @qualifier or use the @resource to inject it by name if it is referenced elsewhere.

If no name is declared, it can only be injected using @autowired if it is referenced elsewhere.

3. Attribute Injection

Normal properties

@Value @Value ("") to inject property values into normal properties

Reference properties

@Autowired injected by default type

@Qualifier ("") Injected by name

If you use the @qualifier annotation, then you need two to work together to take Effect. If you use only @autowired, then you can write @qualifier

Reference properties

@Resource is injected directly by name and is equivalent to the above two annotations

@Resource (name= "xxx")

             

4. Use

           

                       

5. Other Annotations

@Scope ("prototype") scope Annotations (the Scope of the bean instance that spring helps us create, as explained Below)

@PostConstruct modifier Initialization

@PreDestory Decoration Destruction

The last two uses not much, grasps the front Can.

      

Vi. scope of the bean created by spring 

That's the scope of the beans that spring helped us Create.

Format: <bean id= "" class= "" scope= "...">

Value:

            

Master the top two, Singleton and prototype

Singleton: a singleton, that is, when spring creates a bean object for us through an XML configuration file, What we get is always the same object, there is only one instance of the Bean. Default value

                

              

Prototype: every time we need a bean instance, we will create a new instance.

                

              

Vii. writing Applicationcontext.xml hint questions

Five steps to solve

First step: find the scheme XSD document in Spring's core jar

Spring-framework-3.2.0.release\schema\beans\spring-beans-3.2.xsd

              

Step two: from the XML configuration file, get the XSD document location, copy .... spring-beans.xsd

              

Step three: window/preferences/search for "xml catalog"

                

Fourth Step:

              

Fifth Step:

              

        

Note: if XML is not prompted, close the XML rewrite Open.

Viii. Summary

Spring's first focus on the ioc, this is finished, in fact, after reading very simple, is to let spring to create objects for you, you want to object, you find spring, let him Give.

What you learn Is:

What is IOC and what is dependency Injection.

The dependency injection is then explained in Detail.

Using XML and Annotated methods

To be simple, This section is about learning to configure and let Spring help us create the beans we want.

Transfer from http://www.cnblogs.com/whgk/p/6616593.html If there is infringement, feel free to contact me to delete the Article.

Spring (i) IOC Commentary

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.