首页 > 其他分享 >学习笔记——Spring中组件扫描(包含扫描、排除扫描)、Spring中完全注解开发;Spring整合Junit4步骤

学习笔记——Spring中组件扫描(包含扫描、排除扫描)、Spring中完全注解开发;Spring整合Junit4步骤

时间:2023-01-18 08:44:25浏览次数:35  
标签:Spring 扫描 public context 注解 Junit4 class

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());
    }
}

 

标签:Spring,扫描,public,context,注解,Junit4,class
From: https://www.cnblogs.com/isDaHua/p/17059085.html

相关文章

  • SpringCloud Config分布式配置中心
    1、介绍①what微服务意味着需要将单体拆成很多子服务,每个服务都需要配置才能运行。所以需要一套集中式,动态的配置管理,来解决这个问题。SpringCloudConfig为微服务架构......
  • spring data r2dbc 通过 Entity Callbacks 来实现密码加密功能
    不使用使用EntityCallbacks实现密码在保存时密码加密功能@AutowiredPasswordEncoderpasswordEncoder;publicMono<ServerResponse>save(ServerRequestrequest){......
  • Java | Spring Boot数据源配置原理
    在数据库访问过程中,“数据源”无疑是最重要的概念之一,它不仅可以对与数据库访问相关的各种参数进行封装和统一管理,还可以管理数据库连接池,提高数据库连接性能。目前,在市面......
  • Java | Spring Boot统一日志框架
    在项目开发中,日志十分的重要,不管是记录运行情况还是定位线上问题,都离不开对日志的分析。在Java领域里存在着多种日志框架,如JCL、SLF4J、Jboss-logging、jUL、log4j、log......
  • 通过Docker启动DB2,并在Spring Boot整合DB2
    1简介DB2是IBM的一款优秀的关系型数据库,简单学习一下。2Docker安装DB2为了快速启动,直接使用Docker来安装DB2。先下载镜像如下:dockerpullibmcom/db2:11.5.0.0启动......
  • Spring 自动装配
    Spring框架的自动装配功能使您可以隐式注入对象依赖项。它在内部使用setter或构造函数注入。自动装配不能用于注入基本值和字符串值。它仅适用于参考。自动装配的优点它......
  • Spring setter方法注入
    我们也可以通过setter方法注入依赖项。 <bean>的 <property>子元素用于Setter注入。在这里,我们要注入 原始和基于字符串的值从属对象(包含对象)集合值等通过setter方......
  • SpringCloud GateWay网关(入门)
    1、介绍强烈推荐,看官网文档SpringCloudGateway①简介Cloud全家桶里有个重要组件:网关SpringCloudGateway基于WebFlux框架WebFlux底层使用高性能的Reactor模式(异步......
  • day04-Spring管理Bean-IOC-02
    Spring管理Bean-IOC-022.基于XML配置bean2.7通过util空间名称创建listBookStore.java:packagecom.li.bean;importjava.util.List;/***@author李*@version......
  • Spring6
    Spring6Spring项目的创建打开IDEA,新建一个maven项目在maven项目中引入spring的仓库和依赖<repositories><repository><id>repository.spri......