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
      

_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
    

_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
    
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值