spring1-test1-spring使用步骤之组件注册、使用简单总结,并解决BeanFactory not initialized or already closed - call 'refres

错误:BeanFactory not initialized or already closed - call ‘refresh’ before accessing beans via the ApplicationContext

实验1:spring的创建过程

  • 通过各种方式给容器注册对象(注册会员)
  • 以前是自己new对象,现在所有的对象交给容器创建,给容器中注册组件。
  • 当使用maven创建项目之后,并且配置好了相应的依赖。之后按照正常的步骤来测试下spring的容器组件是否可以正常运行。maven的创建过程在之前的博客中有提到详细的步骤。
    在这里插入图片描述

1、person类

public class Person {
    private String name;
    private Integer age;
    private String gender;
    private String email;

    public String getName() {
        return name;
    }

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

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", gender='" + gender + '\'' +
                ", email='" + email + '\'' +
                '}';
    }
}

2、spring-config.xml配置文件

<?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">
    <!-- 注册一个person对象,spring会自动创建这个person对象 -->
    <!-- 一个bean标签可以注册一个组件(对象、类)
     class:要写注册的组件的全类名;
     id:是对象的唯一标识;
     -->
    <bean id="person1" class="Person">
        <!-- 使用property为Person对象的属性赋值
         name="name" :指定属性名;
         value="张山":为这个属性赋值;
         -->
        <property name="name" value="张山"></property>
        <property name="age" value="23"></property>
        <property name="email" value="[email protected]"></property>
        <property name="gender" value="男"></property>
    </bean>
</beans>

3、测试类

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

public class IOCTest {
    /**
     * 从容器中拿到这个组件看一下
     */
    @Test
    public void test(){
        //ApplicationContext:代表IOC容器
        //ClassPathXmlApplicationContext:当前应用的配置文件xml在ClassPath路径下
        ApplicationContext ioc = new ClassPathXmlApplicationContext("spring-config.xml");
        //根据spring的配置文件得到IOC对象
        Person bean = (Person)ioc.getBean("person1");
        System.out.println(bean);
    }
}

4、项目错误解决:

错误1:Error:java: 错误: 不支持发行版本 5已经日志中Build completed with 1 error and 0 warnings in 5 s 403 ms在这里插入图片描述

解决方法:改下Java compiler版本号

在这里插入图片描述

错误2改了版本号之后,又出现了BeanFactory not initialized or already closed - call ‘refresh’ before accessing beans via the ApplicationContext

在这里插入图片描述
在这里插入图片描述

不是空参传入,代码写错了,该!!!在ClassPathXmlApplicationContext(spring-config.xml"")传入配置文件的名。

 ApplicationContext ioc = new ClassPathXmlApplicationContext(spring-config.xml"");

在这里插入图片描述

该木偶挖~

发布了52 篇原创文章 · 获赞 1 · 访问量 2264

猜你喜欢

转载自blog.csdn.net/Shen_R/article/details/104865893