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。

发布了47 篇原创文章 · 获赞 57 · 访问量 8877

猜你喜欢

转载自blog.csdn.net/qq_41741884/article/details/100930480