Spring -- 全注解下的IoC(3)

  依赖注入

    1.定义基本的接口和类

      目录结构

      

     Person 接口:  

package com.com.example.demo4;

/**
 * 实现人类接口
 */
public interface Person {
    public void service();

    //依赖的类
    public void setAnimal(Animal animal);
}

    Animal 接口:

package com.com.example.demo4;

public interface Animal {
    public void use();
}

    BusinessPerson implement Person :

package com.com.example.demo4.imple;

import com.com.example.demo4.Animal;
import com.com.example.demo4.Person;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

@Component

public class BussiessPerson implements Person {

    /* @Autowired 会自动的注入一个依赖实例
      * 1.但我们注入的实例由一个接口派生了多个类,我们需要指定一个类时,我们需要将该类的名字
      * 例如 Animal dog = null (Dog 是实现 Animal 接口类之一)
      * */
    @Autowired
    /* 当某个类,或某几个类被依赖注入时 使用了@Primary 优先注入,使用该注解可以不选择优先注入而是 使用括号里命名的类 */
    @Qualifier("dog")
    private Animal animal = null;

    @Override
    public void service() {
        this.animal.use();
    }

    /*
        你也可以在构造函数里进行@Autowired 上面的注解取消掉
    */
    public BussiessPerson(@Autowired Animal animal) {
        this.animal = animal;
    }

    @Override
    public void setAnimal(Animal animal) {
        this.animal  = animal;
    }
}

    Cat implement Animal:

package com.com.example.demo4.imple;

import com.com.example.demo4.Animal;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;

@Component
/* @Primary 当该类需要被依赖注入时,还有其它与该类实现同样接口时,该类会被优先注入 */
@Primary
public class Cat implements Animal {
    @Override
    public void use() {
        System.out.println("cat["+Cat.class.getSimpleName()+"] mew mew mew");
    }
}

    Dog implement Animal:

package com.com.example.demo4.imple;

import com.com.example.demo4.Animal;
import org.springframework.stereotype.Component;

@Component
public class Dog implements Animal {
    @Override
    public void use() {
        System.out.println("Dog["+Dog.class.getSimpleName()+"] is wow wow wow!");
    }
}

    AppConfig:

package com.com.example.demo4.imple;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@ComponentScan(basePackages = {"com.com.example.demo4.*"})
public class AppConfig {

}    

    Test:

package com.com.example.demo4.imple;

import com.com.example.demo4.Person;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class Test {

    public static void main(String[] args) {
        ApplicationContext atx = new AnnotationConfigApplicationContext(AppConfig.class);
        Person person = atx.getBean(BussiessPerson.class);
        person.service();
    }
}

    2.新增的注解

      1.@Autowired 注解 : 当某个类需要依赖另一个类时使用

       当这个接口被多个类同时实现时,这里的代码会导致Spring 不知道到底要注入哪个类而报错

    

·      2.@Primary 注解: 优先注入的依赖

       当某个多个类同时实现了一个接口,那么注入一个接口是,由@Primary 的会被优先注入

      

 

      3.@Qualifier("name") 注解,当已经有多个@Primary标识的类存在,我们需要注入一个指定的类,就可以使用该注解

        “name” 标识的时存入Bean 时自定义或者系统生成的名称;

      

 

     4. 在构造函数中注入依赖

 

posted @ 2018-12-06 16:33  Weeeeew  阅读(224)  评论(0编辑  收藏  举报