Spring Point three core technologies

Spring core technical points are:

IOC (inversion of control) inversion control (inversion control)

We lived in or use to get a thing, often the first reaction is to find materials and tools of these things, like, we drink a glass of orange juice, then we must first find a juicer, then buy oranges, and water to create orange juice.

Here we will extend two basic concepts: the positive control and anti-control

Positive control: To use an object to make their own responsible for creating objects, such as: User user = new User ();

Anti-control: To use an object from the Spring container just need to get to object, object creation process does not care, that is, to create objects of control to Spriing control.

DI (Dependency injection) dependency injection.

I would first say a scene: If one day you are thirsty, need to buy a small shop selling a bottle of water, but a small shop selling very far from your home. A very simple way, of course, is that you go directly to buy a small shop selling water, buy water before you have to know how to sell a small shop, a small shop selling also know there is no water you need, go to a small shop selling a car or not so on. You might also consider many other issues in order to buy a bottle of water, that is to say, for a bottle of water, you need to use other tools, cars and so on, the problem becomes quite complicated.

So, the simplest way is: a small shop selling home delivery service, who registered as a member of a small shop selling, can enjoy this service, you only need to tell a small shop selling what you need, a small shop selling will promptly goods delivered to their doorsteps.

As a result you need to do two things: first, a small shop to sell registered as a member. Second, informing a small shop selling what you need.

This Spring and would like to practice, spring is a small shop selling, you are the object A, B object is water.

First, Spring declare a class A,

Second, tell Spring, A needs B

Spring refers to the process of creating an object, the object dependency property (simple values, collections, objects) to the target set by coordination to achieve DI must have done IOC support.

<bean name="" class="com.shop.domain.User">

// table with four birthday this object is injected into people in this category

        <property name="birthday" ref="birthday"/>

</bean>

<bean name="birthday" class="java.util.Date"/>

XML manner DI operation

Injection method: set mode injection Constructor injection, p namespace injection, spel expression injection

Data type injection: basic types: int, floart, double, String; object type; complex type

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean name="User" class="com.shop.domain.User">
        <!--必须对应的类中可以灭有get方法,但是必须有set方法-->
        <property name="name" value="yly"/>
        <property name="id" value="10"/>
        <property name="myCar" ref="MyCar"/>

    </bean>

    <bean name="MyCar" class="com.shop.domain.Car">
        <property name="carColor" value="黄色"/>
        <property name="carName" value="法拉利"/>
    </bean>
</beans>

Car object then added in the User and add set method.

Constructor creates an object, why the above object can be created, because the IC to create objects by default constructor with no arguments, you can participate by printing a look to know

However, if the class is not the default constructor with no arguments, such as the following:


    public User(String name,int id){
        this.id=id;
        this.name=name;
    }

Then you would use the following method for injection:


    <bean name="User" class="com.shop.domain.User">
        <constructor-arg name="name" value="姚"/>
        <constructor-arg name="id" value="10"/>
    </bean>

Priority may also be determined by performing the index value for the constructor

You can also be used to control the type to find that type of argument


    <bean name="User" class="com.shop.domain.User">
        <constructor-arg name="name" value="姚" index="1"/>
        <constructor-arg name="id" value="10" index="0" type="java.lang.Integer"/>
    </bean>

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Published 75 original articles · won praise 31 · views 10000 +

Guess you like

Origin blog.csdn.net/yaoyaoyao_123/article/details/103705545