spring基础知识

本文介绍了spring整合Junit的步骤,包括拷贝jar包、使用@RunWith和@ContextConfiguration注解指定配置文件位置。还阐述了不同spring容器的特点和创建方式,如ApplicationContext立即加载,BeanFactory延迟加载,以及ClassPathXmlApplicationContext、FileSystemXmlApplicationContext和AnnotationConfigApplicationContext的使用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

spring整合Junit的步骤

1.拷贝整合的jar spring-test-5.0.2.RELEASE.jar

2.使用使用Junit提供的@RunWith注解把原有的运行器替换掉(@RunWith(SpringJUnit4ClassRunner.class))

3.使用spring提供的@ContextConfiguration注解指定配置文件的位置(@ContextConfiguration(locations=“classpath:bean.xml”)或者@ContextConfiguration(classes=SpringConfiguration.class))。
注:前一个注解是针对配置文件的,指定的是配置文件的位置,后一个注解是针对纯注解的,指定的是spring的配置类。

ApplicationContext(常用)

他是BeanFactory下的子接口的再子接口;特点是创建bean对象时,使用立即加载的策略(即:当读取完XML配置文件,配置文件中所有的bean对象都已经创建完成)。

ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");

BeanFactory

他是springioc容器的顶层接口;特点是特点是创建bean对象时,使用延迟加载的策略(即:当读取完XML配置文件,配置文件中所有的bean对象不会立刻经创建,只有真正从容器中获取bean对象时,才会创建)。

Resource resource = new ClassPathResource("bean2.xml");
BeanFactory beanFactory = new XmlBeanFactory(resource);

ClassPathXmlApplicationContext(常用)

他是通过读取类路径下的配置文件,创建spring容器(配置文件必须在类路径下,否则无法创建spring容器)。

FileSystemXmlApplicationContext:(几乎不用)

他是通过读取文件系统下的配置文件,创建spring容器(配置文件在文件文件系统路径下即可)。

AnnotationConfigApplicationContext

他是通过读取spring配置类,创建spring容器

ApplicationContext ac = new AnnotationConfigApplicationContext(SpringConfiguration.class);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值