全注解下的spring IOC之依赖注入
主要讲IOC容器如何加载bean之间的依赖关系和bean值得获取。
案例
Person接口
package com.wuk.demo4;
public interface Person {
/*使用动物服务*/
public void service();
/*设置动物*/
public void setAnimal(Animal animal);
}
Animal接口
package com.wuk.demo4;
public interface Animal {
public void use();
}
BusinessPerson 类
package com.wuk.demo4;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class BusinessPerson implements Person {
//该注解会根据属性类型在IOC中找到相对应的对象bean的值并注入
@Autowired
private Animal animal;
@Override
public void service() {
this.animal.use();
}
@Override
public void setAnimal(Animal animal) {
this.animal=animal;
}
}
Dog类
package com.wuk.demo4;
import org.springframework.stereotype.Component;
//表示这个类将被springIOC容器扫描装配
@Component
public class Dog implements Animal {
@Override
public void use() {
System.out.println("狗"+Dog.class.getSimpleName()+"是用来看门的");
}
}
AppConfig 配置类
package com.wuk.demo4;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
//表示它会进行扫描,只扫描该类所在额包和子包
@ComponentScan
//表示这是一个配置文件
@Configuration
public class AppConfig {
}
测试类
package com.wuk.demo4;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class IocTest4 {
public static void main(String[] args) {
ApplicationContext app=new AnnotationConfigApplicationContext(AppConfig.class);
Person person=app.getBean(BusinessPerson.class);
person.service();
}
}
结果
狗Dog是用来看门的
注解@Autowired
注入的机制是根据类型。
如果我们现在再创建一个Cat类
package com.wuk.demo4;
import org.springframework.stereotype.Component;
//表示这个类将被springIOC容器扫描装配
@Component
public class Cat implements Animal {
@Override
public void use() {
System.out.println("猫"+Cat.class.getSimpleName()+"是用来抓老鼠的");
}
}
如果再执行上述代码,就会报错如下:
Caused by: org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'com.wuk.demo4.Animal' available: expected single matching bean but found 2: cat,dog
at org.springframework.beans.factory.config.DependencyDescriptor.resolveNotUnique(DependencyDescriptor.java:215)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1113)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1062)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:581)
... 14 more
现在对BusinessPerson类中的animal修改为dog
package com.wuk.demo4;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class BusinessPerson implements Person {
//该注解会根据属性类型在IOC中找到相对应的对象bean的值并注入
@Autowired
private Animal dog;
@Override
public void service() {
this.dog.use();
}
@Override
public void setAnimal(Animal animal) {
this.dog=animal;
}
}
异常消失。
消除歧义性-@Primary和@Qualifier
package com.wuk.demo4;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;
@Primary
@Component
public class Cat implements Animal {
@Override
public void use() {
System.out.println("猫"+Cat.class.getSimpleName()+"是用来抓老鼠的");
}
}
@Primary就是告诉IOC,当发现多个相同类型的Bean时候,请优先使用我进行注入。
但是问题又来了,如果有多个类上使用@Primary怎么办?
@Qualifier("dog")
@Autowired
private Animal animal;
@Qualifier(“dog”) 这样IOC容器就会根据类型和名称去寻找对应的Bean进行注入。
带有参数的构造方法类的装配
在上述中我们都是基于一种情况,就是在不带参数的构造方法下实现依赖注入。
package com.wuk.demo4;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
@Component
public class BusinessPerson implements Person {
private Animal animal;
public BusinessPerson(@Autowired @Qualifier("dog") Animal animal){
this.animal=animal;
}
@Override
public void service() {
this.animal.use();
}
@Override
public void setAnimal(Animal animal) {
this.animal=animal;
}
}
public BusinessPerson(@Autowired @Qualifier(“dog”) Animal animal)
还没有评论,来说两句吧...