Spring's Learning _ 2.IoC Control Inversion

Keywords: PHP Spring xml Java Attribute

1.Ioc (inversion of control) controls inversion (an idea).

IoC is an idea. Before that, the creation of objects used in programs must be completed by programmers. IoC entrusts the creation, preservation and management of objects (life cycle) to Spring, so Spring is also equivalent to a container (object of objects stored), which is the process of control inversion.

Function: It realizes the decoupling between objects, greatly reduces the coupling and makes the module independent.

2. A small case:

(use maven under Idea to complete this demo)

1. Import the Jar package (spring web mvc) by enabling:

<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>4.3.9.RELEASE</version>
</dependency>

2. Entity class writing:

public class Hello {

    private String name;

    public Hello() {
    }

    public Hello(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void show(){
        System.out.println("Hello,"+name);
    }

    @Override
    public String toString() {
        return "Hello{" +
                "name='" + name + '\'' +
                '}';
    }
}

3. Writing of the application Context.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<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 It's just one. java Object by Spring Management and creation-->
    <bean id="hello" class="com.kuang.pojo.Hello">
        <property name="name" value="Spring"/>
    </bean>

    <!--bean It's just one. java Object by Spring Management and creation-->
    <bean id="hello2" class="com.kuang.pojo.Hello">
        <property name="name" value="WestOS"/>
    </bean>

    <!--// Hello hello2 = new Hello();-->
    <!--// hello2.setName (WestOS)-->

</beans>

4. Test class:

public class HelloTest {
    @Test
    public void test(){

        //analysis beans.xml configuration file,Corresponding to production management Bean object;
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");

        //adopt Bean Get the entity of this object
        Hello hello = (Hello)context.getBean("hello2");

        hello.show();

    }
}

Case summary:

1. In applicationContext.xml, each bean is an object instance, which is created by Spring to save and manage its life cycle.

2. Create objects by reflection.

3. Applying entity set method to attribute assignment.

4.Ioc creates objects in two ways:

Mode 1: Apply attribute names directly (essence: first create an object by calling the parametric construction method of entity class, then assign attributes by calling set method)

 <bean id="user" class="com.kuang.pojo.User">
        <property name="name" value="qinjiang"/>
    </bean>

Method 2: Use parametric construction method to create objects directly and assign attributes.

 <!--Assignment using constructor parameter Subscripts-->
    <bean id="user2" class="com.kuang.pojo.User">
        <constructor-arg index="0" value="kuangshen"/>
        <constructor-arg index="1" value="18"/>
    </bean>

Posted by hammerslane on Mon, 14 Oct 2019 07:33:59 -0700