Idea2019搭建Spring框架環境demo

1、創建項目

在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述

2、寫幾個demo稍微對Spring的作用進行初步瞭解

1、寫一個實體類

/**
 * 實體類
 * @author Xuan
 * @date 2019/9/17 16:32
 */
public class Xuan {
    private String name;

    public String getName() {
        return name;
    }

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

3、IOC是Inversion of Control的縮寫,多數書籍翻譯成“控制反轉”,還有些書籍翻譯成爲“控制反向”或者“控制倒置”。

4、不用Spring的IOC容器

/**
 * @author Xuan
 * @date 2019/9/17 16:33
 */
public class TestMain {
    public static void main(String[] args) {
        Xuan xuan = new Xuan();
        xuan.setName("測試");
        System.out.println(xuan.getName());
    }
}

在這裏插入圖片描述

5、使用SpringIoc的容器

    <bean id="xuan" class="Xuan">
        <property name="name" value="小軒哥哥"/>
    </bean>

在這裏插入圖片描述

6、取出存在IOC容器裏面的具體bean

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * @author Xuan
 * @date 2019/9/17 16:33
 */
public class TestMain {
    public static void main(String[] args) {
//        Xuan xuan = new Xuan();
//        xuan.setName("測試");
//        System.out.println(xuan.getName());
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-config.xml");
        Xuan xuan =  (Xuan)applicationContext.getBean("xuan");
        System.out.println(xuan.getName());
    }
}

在這裏插入圖片描述

7、簡單的注入過程解釋

在這裏插入圖片描述

8、輸出Spring的IOC容器裏所有已經存在的bean

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * @author Xuan
 * @date 2019/9/17 16:33
 */
public class TestMain {
    public static void main(String[] args) {
//        Xuan xuan = new Xuan();
//        xuan.setName("測試");
//        System.out.println(xuan.getName());
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-config.xml");
//        Xuan xuan =  (Xuan)applicationContext.getBean("xuan");
//        System.out.println(xuan.getName());
        String [] str = applicationContext.getBeanDefinitionNames();
        for (String x:str) {
            System.out.println("注入的"+x);
        }
    }
}

在這裏插入圖片描述

9、遇到困難可以評論(有信必回)小軒微信17382121839。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章