全注解下的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)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

技术闲聊DD

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值