spring1-test1-spring使用步骤之组件注册、使用简单总结,并解决BeanFactory not initialized or already closed - call 'refres
2020-04-02 07:52
1016 查看
错误: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="zhanshang@guigu.com"></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"");
该木偶挖~
- 点赞
- 收藏
- 分享
- 文章举报
相关文章推荐
- tomcat过一段时间就报spring的错:BeanFactory not initialized or already closed - call 'refr
- BeanFactory not initialized or already closed - call 'refresh' before accessing beans解决办法
- spring注解过程中java.lang.IllegalStateException: BeanFactory not initialized or already closed - call 're
- BeanFactory not initialized or already closed - call 'refresh' before accessing beans解决办法
- spring配置出错: BeanFactory not initialized or already closed - call 'refresh' before accessing beans vi
- maven项目-加载不到spring文件,BeanFactory not initialized or already closed - call 'refresh' bef
- BeanFactory not initialized or already closed - call 'refresh' before accessing beans解决
- Spring如果遇到这个问题:java.lang.IllegalStateException: BeanFactory not initialized or already closed - call
- BeanFactory not initialized or already closed - call 'refresh' before accessing beans via the Applic
- java.lang.IllegalStateException: BeanFactory not initialized or already closed - call 'refresh' befo
- BeanFactory not initialized or already closed - call 'refresh' before accessing beans via the Applic
- java.lang.IllegalStateException: BeanFactory not initialized or already closed - call 'refresh' befo
- java.lang.IllegalStateException: BeanFactory not initialized or already closed - call 'refresh' befo
- BeanFactory not initialized or already closed - call 'refresh' before accessing beans via the Applic
- 问题BeanFactory not initialized or already closed - call 'refresh' before access
- spring整合jax-ws时报错(java.lang.IllegalStateException: BeanFactory not initialized or already closed)
- BeanFactory not initialized or already closed - call 'refresh' before accessing beans via the Applic
- tomacat 启动时报错 : BeanFactory not initialized or already closed - call 'refresh' before accessing bean
- 对于 BeanFactory not initialized or already closed - call 'refresh' before accessing beans via the...
- BeanFactory not initialized or already closed - call 'refresh' before accessing