zoukankan      html  css  js  c++  java
  • ________________初学Spring2

    三种ApplicationContext依赖注入的方式

    1、注解

    @Component

    public class User {

    @Autowired

    private ApplicationContext applicationContext;

    public void show() {

    System.out.println("user:"+applicationContext.getClass());

    }

    }

    2、实现ApplicationContextAware接口

    @Component

    public class Book implements ApplicationContextAware {

    private ApplicationContext applicationcontext;

    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {

    this.applicationcontext=applicationContext;

    }

    public void show() {

    System.out.println("book:"+applicationcontext.getClass());

    }

    }

    3、构造方法直接构造(局限性 1⃣️构造函数只能有一个,如果有多个必须要有无参构造此时spring 就会调用无参,不会调用 applicationcontext

                  2⃣️构造函数的参数,必须要在Spring 容器中有 )

    @Component

    public class Bank {

     private ApplicationContext applicationContext;

    public Bank(ApplicationContext applicationContext) {

    this.applicationContext=applicationContext;

    }

    public void show() {

    System.out.println("Bank:"+applicationContext.getClass());

    }

    }

  • 相关阅读:
    二叉树
    队列和栈
    时间复杂度和空间复杂度
    二分查找法
    排序算法值归并排序
    排序算法之选择排序类
    5.7.1.3 Global 对象的属性
    5.7.1.2 eval() 方法
    5.7.1.1 单体内置对象
    5.6.3.8 fromCharCode()方法
  • 原文地址:https://www.cnblogs.com/qiqisx/p/9335723.html
Copyright © 2011-2022 走看看