Java:基于注解的Spring使用【IOC容器】

第九章 关于Spring IOC容器的个人理解

9.1 Spring IOC容器 理解参考

Spring IOC容器 可以用Vuex进行参考。

  • IOC容器,就是装各个对象的。
  • Vuex,就是进行状态管理的。

获取装配对象,其实类似于前端的获取组件对象,

  • 有个组件对象,通过组件对象调用组件的方法
  • 获取装配对象,通过装配对象调用对象的方法。

9.2 本章主要内容

  • 将对象装配到IOC容器中(注解

    • @Component:装配普通组件到IOC容器
    • @Repository:装配持久化层组件到IOC容器
    • @Service:装配业务逻辑层组件到IOC容器
    • @Controller:装配控制层|表示层组件到IOC容器
  • 装配对象中的属性(可以理解为:初始化对象中属性的值

    • @Autowired:自动装配对象中属性【非字面量数值】
    • @Qualifier:配合@Autowired一起使用,将设置beanId名称装配到属性中
    • @Value:装配对象中属性【字面量数值】
  • 组件扫描

    • 默认使用情况
    • 包含扫描
    • 排除扫描
  • 零配置注解开发

    1. 创建配置类
    2. 在class上面添加注解
      • @Configuration:标识当前类是一个配置类,作用:代替XML配置文件
      • @ComponentScan:设置组件扫描当前包及其子包
    3. 使用AnnotationConfigApplicationContext容器对象
  • 自己的bean,可以用注解

  • 第三方的bean,没法用注解,只能用xml配置

第十章 Spring中注解【非常重要】

10.1 使用注解将对象装配到IOC容器中

约定:约束>配置【注解>XML】>代码

位置:在类上面标识

注意:

  • Spring本身不区分四个注解【四个注解本质是一样的@Component】,提供四个注解的目的只有一个:提高代码的可读性
  • 只用注解装配对象,默认将类名首字母小写作为beanId
  • 可以使用value属性,设置beanId;当注解中只使用一个value属性时,value关键字可省略
  • 装配对象四个注解

    • @Component:装配普通组件到IOC容器
    • @Repository:装配持久化层组件到IOC容器
    • @Service:装配业务逻辑层组件到IOC容器
    • @Controller:装配控制层|表示层组件到IOC容器
  • 使用注解步骤

    • 导入相关jar包【已导入】

      <dependencies>
          <!--导入spring-context-->
          <dependency>
              <groupId>org.springframework</groupId>
              <artifactId>spring-context</artifactId>
              <version>5.3.1</version>
          </dependency>
      </dependencies>
      
    • 开启组件扫描

      <!--    开启组件扫描
              base-package:设置扫描注解包名【当前包及其子包】
      -->
      <context:component-scan base-package="com.atguigu"></context:component-scan>
      
    • 使用注解标识组件

       @Service(value = "deptService")
      public class DeptServiceImpl implements DeptService {
      
          @Autowired
      //    @Qualifier("deptDao") // 指定装配的对象。要结合Autowired使用
          private DeptDao deptDao;
      
          @Override
          public void saveDept(Dept dept) {
              deptDao.insertDept(dept);
          }
      }
      

10.2 使用注解装配对象中属性【自动装配】

  • @Autowired注解

    • 作用:自动装配对象中属性

    • 装配原理:反射机制

    • 装配方式

      • 先按照byType进行匹配

        • 匹配1个:匹配成功,正常使用

        • 匹配0个:

          • 默认【@Autowired(required=true)】报错

            /*expected at least 1 bean which qualifies as autowire candidate. 	Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
            */
            
          • @Autowired(required=false),不会报错

        • 匹配多个

          • 再按照byName进行唯一筛选

            • 筛选成功【对象中属性名称==beanId】,正常使用

            • 筛选失败【对象中属性名称!=beanId】,报如下错误:

              //expected single matching bean but found 2: deptDao,deptDao2
              
    • @Autowired中required属性

      • true:表示被标识的属性必须装配数值,如未装配,会报错
      • false:表示被标识的属性不必须装配数值,如未装配,不会报错
  • @Qualifier注解

    • 作用:配合@Autowired一起使用,将设置beanId名称装配到属性中
    • 注意:不能单独使用,需要与@Autowired一起使用
  • @Value注解

    • 作用:装配对象中属性【字面量数值】

第十一章 Spring中组件扫描

11.1 默认使用情况

<!--    开启组件扫描
        base-package:设置扫描注解包名【当前包及其子包】
-->
<context:component-scan base-package="com.atguigu"></context:component-scan>

11.2 包含扫描

  • 注意:
    • 使用包含扫描之前,必须设置use-default-filters=“false”【关闭当前包及其子包的扫描】
    • type
      • annotation:设置被扫描注解的全类名
      • assignable:设置被扫描实现类的全类名
<context:component-scan base-package="com.atguigu" use-default-filters="false">
    <context:include-filter type="annotation" 			 expression="org.springframework.stereotype.Repository"/>
    <context:include-filter type="assignable" expression="com.atguigu.service.impl.DeptServiceImpl"/>
</context:component-scan>

11.3 排除扫描

<!--    【排除扫描】   假设:环境中共有100包,不想扫描2/100-->
    <context:component-scan base-package="com.atguigu">
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
<!--        <context:exclude-filter type="assignable" expression="com.atguigu.controller.DeptController"/>-->
    </context:component-scan>

第十三章 Spring完全注解开发【0配置】

13.1 完全注解开发步骤

  1. 创建配置类
  2. 在class上面添加注解
    • @Configuration:标识当前类是一个配置类,作用:代替XML配置文件
    • @ComponentScan:设置组件扫描当前包及其子包
  3. 使用AnnotationConfigApplicationContext容器对象

13.2 示例代码

// com/atguigu/spring/config/SpringConfig.java
/**
 * @author Chunsheng Zhang 尚硅谷
 * @create 2022/3/28 14:05
 */
@Configuration
@ComponentScan(basePackages = "com.atguigu.spring")
public class SpringConfig {
}
// src/test/java/Test0Xml.java
  @Test
    public void test0Xml(){
        //创建容器对象
//        ApplicationContext context =
//                new ClassPathXmlApplicationContext("applicationContext.xml");
        //使用AnnotationConfigApplicationContext容器对象
        ApplicationContext context =
                new AnnotationConfigApplicationContext(SpringConfig.class);
        DeptDaoImpl deptDao = context.getBean("deptDao", DeptDaoImpl.class);

        System.out.println("deptDao = " + deptDao);
    }

第十四章 Spring集成Junit4

14.1 集成步骤

  1. 导入jar包
    • spring-test-5.3.1.jar
    	<!--spring集成junit4-->
         <dependency>
             <groupId>org.springframework</groupId>
             <artifactId>spring-test</artifactId>
             <version>5.3.1</version>
             <scope>test</scope>
         </dependency>
    
  2. 指定Spring的配置文件的路径
    • @ContextConfiguration
  3. 指定Spring环境下运行Junit4的运行器
    • @RunWith

14.2 示例代码

报了空指针,不知道啥原因
jdk改成1.8之后可以正常执行

/**
 * @author Chunsheng Zhang 尚硅谷
 * @create 2022/3/28 14:12
 */
@ContextConfiguration(locations = "classpath:applicationContext_annotation.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());
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值