[000-01-010].第02节:Spring基础开发环境搭建

1.1.新建空项目:

  • 1.新建Empty项目,主要是为了方便之后把各个模块的代码统一的放在一起:
    在这里插入图片描述

  • 2.设置JDK:
    在这里插入图片描述

  • 3.设置maven版本:
    在这里插入图片描述


1.2.建立第一个Spring项目模块:

  • 1.新建模块:
    在这里插入图片描述

  • 2.配置依赖:

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
    
        <groupId>com.jianqun</groupId>
        <artifactId>spring6-001-revelation</artifactId>
        <version>1.0-SNAPSHOT</version>
    
        <!-- 打包方式 -->
        <packaging>jar</packaging>
    
        <properties>
            <maven.compiler.source>17</maven.compiler.source>
            <maven.compiler.target>17</maven.compiler.target>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        </properties>
    
        <!-- 配置多个仓库   -->
        <repositories>
            <!-- Spring里程碑仓库   -->
            <repository>
                <id>spring-milestones</id>
                <name>Spring Milestones</name>
                <url>https://repo.spring.io/milestone</url>
                <snapshots>
                    <enabled>false</enabled>
                </snapshots>
            </repository>
        </repositories>
    
        <!-- 依赖  -->
        <dependencies>
            <!-- 引入Spring Context依赖之后,就将Spring的基础依赖引入了 -->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context</artifactId>
                <version>6.0.0-M2</version>
            </dependency>
    
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>4.13.2</version>
                <scope>test</scope>
            </dependency>
        </dependencies>
    
    </project>
    
    
  • 3.编写java类:

    package com.jianqun.spring6.bean;
    
    /**
     * 这是一个Bean,封装了用户的信息。Spring可以帮助我们创建User对象吗?
     * @author 动力节点
     * @version 1.0
     * @className User
     * @since 1.0
     **/
    public class User {
        // Spring是怎么实例化对象的?
        // 默认情况下Spring会通过反射机制,调用类的无参数构造方法来实例化对象。
        // 实现原理如下:
        // Class clazz = Class.forName("com.jianqun.spring6.bean.User");
        // Object obj = clazz.newInstance();
        public User() {
            System.out.println("User的无参数构造方法执行。");
        }
    
        public User(String s){
    
        }
    }
    
    
  • 4.编写Spring的配置文件:

    <?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">
    
        <!--这就是Spring的配置文件-->
        <!--IDEA工具为我们提供了这个文件的模板,一定要使用这个模板来创建。-->
        <!--这个文件名不一定叫做spring.xml,可以是其它名字。-->
        <!--这个文件最好是放在类路径当中,方便后期的移植。-->
        <!--放在resources根目录下,就相当于是放到了类的根路径下。-->
        <!--配置bean,这样spring才可以帮助我们管理这个对象。-->
        <!--
            bean标签的两个重要属性:
                id:是这个bean的身份证号,不能重复,是唯一的标识。
                class:必须填写类的全路径,全限定类名。(带包名的类名)
        -->
        <bean id="userBean" class="com.jianqun.spring6.bean.User"/>
    
        <!--<bean id="userBean" class="com.jianqun.spring6.bean.Vip"/>-->
    
        <!--配置其他的bean-->
        <bean id="userDaoBean" class="com.jianqun.spring6.dao.UserDaoImplForMySQL"/>
    
        <!--配置java.util.Date Bean-->
        <bean id="nowTime" class="java.util.Date"/>
    
    
    </beans>
    
    
  • 5.编写测试文件:

    package com.jianqun.spring6;
    
    import org.junit.Test;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class FirstSpringTest {
        @Test
        public void testSpringCode(){
            //1.获取Spring容器对象
            //ApplicationContext翻译为:应用上下文。其实就是Spring容器
            //ApplicationContext就是一个接口,在其下面有很多实现类
            //ClassPathXmlApplicationContext是其中一个实现类,是专门从类路径中加载Spring配置文件的
            //这行代码执行就代表启动了Spring容器,解析Spring.xml文件,并且实例化所有bean对象,放到Spring容器中
            ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring6.xml");
    
            //2.根据beanId来从Spring容器中获取这个对象
            Object userBean = applicationContext.getBean("userBean");
            System.out.println(userBean);
    
            Object userDaoBean = applicationContext.getBean("userDaoBean");
            System.out.println(userDaoBean);
        }
    }
    
    

1.3.资料分享:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值