• Day 88


    _Spring技术–注解开发–依赖注入

    1. Spring提供@Component注解的三个衍生注解

      • @Controller:用于表现层bean定义
      • @Service:用于业务层bean定义
      • @Repository:用于数据层bean的定义
    2. 纯注解开发时不需要配置applictionContext.xml文件:在这里插入图片描述

    3. 虽然不用xml文件定义,但是需要新建一个配置包,在里面配置bean:在这里插入图片描述

    4. 在这里插入图片描述

    5. 在这里插入图片描述

    6. 在test文件中编写测试java程序实现注解依赖注入:在这里插入图片描述

      • package test;
        
        import com.Alvis.config.SpringConfig;
        import com.Alvis.dao.BookDao;
        import com.Alvis.service.BookService;
        import com.Alvis.service.impl.BookServiceImpl;
        import org.springframework.context.ApplicationContext;
        import org.springframework.context.annotation.AnnotationConfigApplicationContext;
        import org.springframework.context.support.ClassPathXmlApplicationContext;
        
        public class App_01 {
            public static void main(String[] args) {
                // 配置文件
                ApplicationContext apx = new AnnotationConfigApplicationContext(SpringConfig.class);
        
                BookDao bookDao = (BookDao) apx.getBean("bookDao");
                bookDao.run();
        
                System.out.println("----------------------------------");
        
                BookService bookService = (BookService) apx.getBean(BookServiceImpl.class);
                bookService.run();
        
            }
        }
        =============================================
        this is BookDao......
        ----------------------------------
        this is BookService......
        this is BookDao......
        
        进程已结束,退出代码0
        
        • 1
        • 2
        • 3
        • 4
        • 5
        • 6
        • 7
        • 8
        • 9
        • 10
        • 11
        • 12
        • 13
        • 14
        • 15
        • 16
        • 17
        • 18
        • 19
        • 20
        • 21
        • 22
        • 23
        • 24
        • 25
        • 26
        • 27
        • 28
        • 29
        • 30
        • 31
        • 32

    _Spring技术–纯注解开发–依赖注入对应多个bean

    1. 如图所示:在这里插入图片描述

    2. 在test文件中编写测试程序实现需求:在这里插入图片描述

    3. package test;
      
      import com.Alvis.config.SpringConfig;
      import com.Alvis.dao.BookDao;
      import com.Alvis.service.BookService;
      import com.Alvis.service.impl.BookServiceImpl;
      import com.Alvis.service.impl.BookServiceImplCopy;
      import org.springframework.context.ApplicationContext;
      import org.springframework.context.annotation.AnnotationConfigApplicationContext;
      import org.springframework.context.support.ClassPathXmlApplicationContext;
      
      public class App_01 {
          public static void main(String[] args) {
              // 配置文件
              ApplicationContext apx = new AnnotationConfigApplicationContext(SpringConfig.class);
      
              BookDao bookDao = (BookDao) apx.getBean("bookDao");
              bookDao.run();
      
              System.out.println("----------------------------------");
      
              BookService bookService = (BookService) apx.getBean(BookServiceImplCopy.class);
              bookService.run();
      
          }
      }
      ==========================================
      this is BookDao......
      ----------------------------------
      this is **Not** BookServiceCopy......
      this is BookDao......
      
      进程已结束,退出代码0
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
      • 17
      • 18
      • 19
      • 20
      • 21
      • 22
      • 23
      • 24
      • 25
      • 26
      • 27
      • 28
      • 29
      • 30
      • 31
      • 32
      • 33

    _Spring技术–纯注解开发–@PropertySource

    1. 在配置类中定义配置注解@PropertySource:

      • 注意:@PropertySource中后接的配置文件的路径默认在resources文件中,而且不需要加额外的同等路径
      • 在这里插入图片描述
    2. 在这里插入图片描述

    3. 在resources中配置值文件:在这里插入图片描述

    4. 最后在test文件中编写Java程序实现需求:在这里插入图片描述

    5. package test;
      
      import com.Alvis.config.SpringConfig;
      import com.Alvis.service.InkService;
      import com.Alvis.service.impl.InkServiceImpl;
      import org.springframework.context.ApplicationContext;
      import org.springframework.context.annotation.AnnotationConfigApplicationContext;
      
      public class App_02 {
          public static void main(String[] args) {
              // 获取配置类
              ApplicationContext apx = new AnnotationConfigApplicationContext(SpringConfig.class);
      
              InkService inkService =apx.getBean(InkServiceImpl.class);
              inkService.run();
          }
      }
      =====================================
      this is InkService...
      this is InkDao...
      age: 100 name: "zhangsan"
      
      进程已结束,退出代码0
      
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
      • 17
      • 18
      • 19
      • 20
      • 21
      • 22
      • 23
      • 24
  • 相关阅读:
    Leetcod graph(new topic)
    创建第一个Vue3项目时遇到的报错及处理
    openstack搭建
    canvas的常用函数
    React+Vue相关插件使用的缺陷小合集
    Python 引用问题 - ImportError: attempted relative import with no known parent package
    【Linux网络】ssh服务与配置,实现安全的密钥对免密登录
    要不要提前去实习?
    C++笔试题详解+扩展
    【数据结构初阶】六、线性表中的队列(C语言 -- 链式结构实现队列)
  • 原文地址:https://blog.csdn.net/ALVIS_108/article/details/126311925