设为首页 加入收藏

TOP

Spring6(一)
2023-07-25 21:42:13 】 浏览:45
Tags:Spring6

Spring6

Spring项目的创建

  • 打开IDEA,新建一个maven项目

  • 在maven项目中引入spring的仓库和依赖

     <repositories>
            <repository>
                <id>repository.spring.milestone</id>
                <name>Spring Milestone Repository</name>
                <url>https://repo.spring.io/milestone</url>
            </repository>
        </repositories>
    
        <dependencies>
            <!--spring6-->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context</artifactId>
                <version>6.0.3</version>
            </dependency>
        </dependencies>
    
  • 编写Spring配置文件。

    • maven成功引入Spring之后,在resource目录下新建一个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 配置文件,名字任意,最好放在根目录下,方便后期移植-->
    </beans>
    
  • 配置bean

    • 在Java目录下新建一个类,在Spring6配置文件中配置bean
    <!--    配置bean,这样spring才能帮助我们管理这个对象-->
    <!--    bean标签有两个重要的属性,-->
    <!--    	--id    是bean的唯一标志,不能重复-->
    <!--    	--class 必须是全路径,全限定类名(带包名的类名)-->
    
    <bean id="studentBean" class="com.winter.spring6.bean.studentBean"/>
    
  • 编写测试程序

    • 引入junit依赖
    • 编写测试程序
    package com.winter.spring6.test;
    
    import org.junit.Test;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    /**
     * @author Winter
     * @version 1.0
     * @since 1.0
     */
    
    public class FirstSrpingTest {
        @Test
        public void testFristSpringCode(){
    //        第一步 获取spring容器对象
    //        ApplicationContext 翻译为应用上下文,其实就是spring容器
    //        ApplicationContext是一个接口,该接口下有很多实现类,其中一个就是ClassPathXmlApplicationContext
    //        ClassPathXmlApplicationContext是专门从类路径加载spring配置文件的上下文对象
            ApplicationContext context = new ClassPathXmlApplicationContext("Spring.xml");
    //        第二步 根据bean的id从这个spring容器中获取这个对象
            Object studentBean = context.getBean("studentBean");
            System.out.println(studentBean);
        }
    }
    

    控制台输出:

    com.winter.spring6.bean.studentBean@158d2680
    

    表示第一个Spring程序完成,Spring成功创建了一个student对象

第一个Spring程序的一些小细节

  1. bean标签的id属性不可以重复

  2. 底层是怎样创建对象的?

    • 默认情况下Spring会通过反射机制,调用类的无参数构造方法来实例化对象

    •   Class clazz = Class.forName("com.winter.spring6.bean.student")
        Object obj = clazz.newInstance();
      
    • 要想让Spring帮你创建对象,那么就必须保证有无参数构造方法。

  3. 创建好的对象都存放在什么样的数据结构中?

    • Map<String , Object>
  4. Spring的配置文件名称不是固定的,想写啥写啥,但是要和下面代码中填入的字符串一致

      ApplicationContext context = new ClassPathXmlApplicationContext("Spring.xml");
    
  5. Spring的配置文件可以有多个

      ApplicationContext context = new ClassPathXmlApplicationContext("Spring1.xml","Spring2.xml");
    
  6. 配置文件中配置的类不一定是自定义的,也可以是JDK中的类

    <bean id="dataBean" class="java.util.Date"/>
    
  7. getBean() 方法调用时,如果指定的id不存在时,不会返回null而是会报错。

  8. getBean() 方法返回的类型时Object,如果访问子类特有属性和方法时,使用时必须向下转型

    或者是在getBean() 方法传入第二个参数,XXXX.class(指定要返回的类型)

    User user = ApplicationContext.getBean("userBean",User.class);
    
  9. 如果是从绝对路径调用spring配置文件,那么就需要

     ApplicationContext context = new Fil
首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇gRPC之初体验 下一篇day03-Spring管理Bean-IOC-01

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目