Getting to know IoC

First, the official document https://docs.spring.io/spring-framework/docs/current/reference/html/core.html#spring-core

Inversion of Control

As the name implies, the controller of the program is reversed from the program itself to controlled by the user. Spring implements IoC through dependency injection (DI). di enables the creation and management of objects to be handed over to the Spring IoC Container. This container needs a configuration file and a corresponding class. In the configuration file, the creation of objects is marked by bean tags.

Create HelloSprig

To use spring, we must import dependencies, create ordinary maven projects, import dependencies, the reason for importing this spring-webmvc is because this package depends on many packages, so that maven can automatically import these packages for us

    <dependencies>
        <!--导入spring依赖,之所以导入这个spring-webmvc是因为这个包依赖的包较多,这样maven可以自动帮我们导入这些包-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.3.3</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>5.3.3</version>
        </dependency>
    </dependencies>

To write a class, there must be a parameterless structure (if you only use the parameterized structure to create an object, you don’t need it, but don’t drill this horny tip), and setter methods (beans inject values ​​through setters)

package com.wt.pojo;

public class Hello {
    
    
    private String str;

    public Hello() {
    
    
    }

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

    public String getStr() {
    
    
        return str;
    }

    public void setStr(String str) {
    
    
        this.str = str;
    }

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

Write the configuration file, the following tag is equivalent to creating an object id equivalent to the name of the object, class is equivalent to what type of object is created, the property tag can set the properties of the object, this property value spring will be assigned through the setter, so the object There must be a settername to be aliased. You can use name to create one alias or multiple aliases. After creating an alias, you can use the alias to getBean. Of course, the alias can also be obtained with the alias tag in addition to the bean tag.

<?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
        https://www.springframework.org/schema/beans/spring-beans.xsd">

    <!--
    下面这个标签相当于创建了一个对象
    id相当于对象的名称,class相当于创建了什么类型的对象
    property标签可以设置对象的属性,这个属性值spring将会通过setter赋予,所以对象必须要有setter
    name可以起别名,可以用name起一个别名也可以起多个,起别名后就可以利用别名来getBean了
    当然别名也可以在bean标签之外用alias标签来取
    -->
    <bean id="hello" class="com.wt.pojo.Hello" name="hello2,abc">
        <property name="str" value="Hello Spring"/>
    </bean>

</beans>

test

import com.wt.pojo.Hello;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MyTest {
    
    
    public static void main(String[] args){
    
    
        //获取spring的上下文对象,就是获取Spring容器
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        //现在我们的对象都在spring中管理了,想要对象,直接用get方法取出来就行了
        //getBean方法要写bean的id作为参数,会返回一个Object类型的对象,需要强转成我们需要的类型
        //容器在手,天下我有,需要什么就get什么
        Hello hello = (Hello)context.getBean("hello");
        System.out.println(hello);
    }
}

By the way, there is also an import tag used to combine multiple "beans.xml" configuration files into one

Guess you like

Origin blog.csdn.net/kitahiragawa/article/details/113104308