学习笔记——Spring中组件扫描(包含扫描、排除扫描)、Spring中完全注解开发;Spring整合Junit4步骤
2023-03-31 10:39:19 时间
2023-01-18
一、Spring中组件扫描
1、默认使用的情况
<context:component-scan base-package="com.hh"></context:component-scan>
2、包含扫描
注:使用包含扫描之前,必须设置use-default-filters="false"(关闭当前包及其子包的扫描)
type类型:
①annotation:设置被扫描注解的全类名
②assignable:设置被扫描实现类的全类名
<context:component-scan base-package="com.hh" use-default-filters="false"> <context:include-filter type="annotation" expression="org.springframework.stereotype.Repository"/> <!-- <context:include-filter type="assignable" expression="com.hh.service.DeptService"/>--> <context:include-filter type="annotation" expression="org.springframework.stereotype.Service"/> <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan>
3、排除扫描
<context:component-scan base-package="com.hh" use-default-filters="false"> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan>
二、Spring中完全注解开发
1、完全注解开发步骤
(1)创建配置类
@Configuration @ComponentScan(basePackages = "com.hh") public class SpringConfig { }
(2)在class上面添加注解
①@Configuration:标识当前类是一个配置类,作用:代替XML配置文件
②@ComponentScan:设置组件扫描的当前包及其自包
(3)使用AnnotationConfigApplicationContext容器对象
public class Test0Xml { @Test public void test0Xml(){ //创建一个容器对象 // ApplicationContext context = new ClassPathXmlApplicationContext(""); ApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class); DeptDaoImpl deptDao = context.getBean("DeptDao", DeptDaoImpl.class); System.out.println("deptDao = " + deptDao); } }
三、Spring整合Junit4步骤
1、集成步骤
(1)导入jar包
<!-- https://mvnrepository.com/artifact/org.springframework/spring-test --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>5.3.10</version> <scope>test</scope> </dependency>
(2)指定Spring的配置文件的路径(@ContextConfiguration)
(3)指定Spring环境下运行Junit4的运行器
①RunWith
(4)集成示例
@ContextConfiguration(locations = "classpath:applicationContext.xml") @RunWith(SpringJUnit4ClassRunner.class) public class TestSpringJunit4 { @Autowired private DeptService deptService; @Test public void testService(){ //创建容器对象 // ApplicationContext context = // new ClassPathXmlApplicationContext("applicationContext.xml"); // DeptService deptService = context.getBean("deptService", DeptServiceImpl.class); deptService.saveDept(new Dept()); } }
相关文章
- 卸载手机软件很多人都做错了,只有这样操作,才能将软件删除干净
- 是谣言还是实话:手机更新系统,真的会越来越卡吗?
- 工信部:已下架 540 款违规处理用户信息等拒不整改的 App,通报 2049 款
- 屏幕旋转切换机制详解
- “元宇宙”真的是骗局吗?为什么“资本”和普通人的看法不一样?
- 想和中国空间站“同框”?超简单攻略来了
- 苹果iOS15.2测试版更新首次加入数字遗产功能
- 手机电池不耐用?学会六个省电小妙招,两天一充不是梦
- Watchdog机制源码分析
- 全球三大即时通讯工具,除了中国的微信,另外两个有多强?
- iOS 15.2 Beta 2发布:五个新变化!修复了最讨厌的Bug
- 避免被跟踪,苹果 iOS 15.2 Beta2 支持扫描附近的 AirTag
- 苹果发布iOS 15.2最新测试版:新增儿童通信安全功能!
- 苹果3nm芯片最快2023年问世:最高或集成40核CPU
- 手机内存空间不足怎么办?教你三个清理方法,瞬间清除十几G空间
- 更新iOS15.1仅12天:电池健康从92%掉到87%,iPhone XR惨遭负优化
- 研究了一波 Android Native C++ 内存泄漏的调试
- 支付宝借呗改名信用贷 借呗回应:已启动品牌隔离 服务不受影响
- 苹系统版本再更新,由iOS 14.6 迎来了iOS 15.2
- 教你四招,别为移动应用程序花冤枉钱!