Spring IOC自动装配、自动扫描bean组件

下面附上学习时的资料和代码练习,可能资料不是很全。

一、依赖注入自动装配

语法:<bean id="..." class="..." autowire="byType"/>

autowire属性取值如下:

§byType:按类型装配,可以根据属性的类型,在容器中寻找跟该类型匹配的bean。如果发现多个,那么将会抛出异常。如果没有找到,即属性值为null

§byName:按名称装配,可以根据属性的名称,在容器中寻找跟该属性名相同的bean,如果没有找到,即属性值为null

§constructorbyType的方式类似,不同之处在于它应用于构造器参数。如果在容器中没有找到与构造器参数类型一致的bean,那么将会抛出异常。

自动装配缺点:

Bean 配置文件里设置 autowire 属性进行自动装配将会装配 Bean 的所有属性. 然而, 若只希望装配个别属性时, autowire 属性就不够灵活了.

autowire 属性要么根据类型自动装配, 要么根据名称自动装配, 不能两者兼而有之.

二、在classpath自动扫描Bean组件

Spring 能够从 classpath 下自动扫描, 侦测和实例化具有特定注解的组件.

特定组件包括:

§@Component: 基本注解, 标识了一个受 Spring 管理的组件

§@Respository: 标识持久层组件

§@Service: 标识服务层(业务层)组件

§@Controller: 标识表现层组件

对于扫描到的组件, Spring 有默认的命名策略: 使用非限定类名, 第一个字母小写. 也可以在注解中通过 value 属性值标识组件的名称

三、使用 @Autowired 自动装配 Bean 

@Autowired 注解自动装配具有兼容类型的单个 Bean属性

构造器, 普通字段(即使是非 public), 一切具有参数的方法都可以应用@Authwired 注解

默认情况下, 所有使用 @Authwired 注解的属性都需要被设置. Spring 找不到匹配的 Bean 装配属性时, 会抛出异常, 若某一属性允许不被设置, 可以设置 @Authwired 注解的 required 属性为 false

默认情况下, IOC 容器里存在多个类型兼容的 Bean , 通过类型的自动装配将无法工作. 此时可以在 @Qualifier 注解里提供 Bean 的名称. Spring 允许对方法的入参标注 @Qualifiter 已指定注入 Bean 的名称

@Authwired 注解也可以应用在数组类型的属性上, 此时 Spring 将会把所有匹配的 Bean 进行自动装配.

@Authwired 注解也可以应用在集合属性上, 此时 Spring 读取该集合的类型信息, 然后自动装配所有与之兼容的 Bean.

@Authwired 注解用在 java.util.Map 上时, 若该 Map 的键值为 String, 那么 Spring 将自动装配与之 Map 值类型兼容的 Bean, 此时 Bean 的名称作为键值

自动装配applicationContext_autowire.xml :

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
               http://www.springframework.org/schema/beans/spring-beans.xsd
               http://www.springframework.org/schema/context
               http://www.springframework.org/schema/context/spring-context.xsd
               http://www.springframework.org/schema/util
               http://www.springframework.org/schema/util/spring-util.xsd ">

	<!-- 
	 -->
	<bean id="teacher" class="com.zzxtit.spring.ioc.first.Teacher">
		
	</bean>
	
	<bean id="mteacher" class="com.zzxtit.spring.ioc.first.MathTeacher">
	
	</bean>
	
	<!-- 根据 需求的类型进行自动装配 autowire="byType"
		如果IOC容器中没有需要自动装配的对象,则不能自动装配。如果IOC容器中有超过一个被装配类的的对象,则会出现异常:
		org.springframework.beans.factory.UnsatisfiedDependencyException: 
		Error creating bean with name 'student' defined in class path resource [config/applicationContext_autowire.xml]: 
		Unsatisfied dependency expressed through bean property 'teacher'; 
		nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: 
		No qualifying bean of type 'com.zzxtit.spring.ioc.first.Teacher' available: 
		expected single matching bean but found 2: teacher,mteacher
	
	-->
	<bean id="student" class="com.zzxtit.spring.ioc.first.Student" autowire="byName">
	<!-- 
		<property name="teacher" ref="teacher"></property>
	 -->
	</bean>
	 
	 
	 
</beans>

自动扫描config/applicationContext_auto.xml :

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
               http://www.springframework.org/schema/beans/spring-beans.xsd
               http://www.springframework.org/schema/context
               http://www.springframework.org/schema/context/spring-context.xsd
               http://www.springframework.org/schema/util
               http://www.springframework.org/schema/util/spring-util.xsd ">

	<context:component-scan base-package="com.zzxtit.spring.ioc.*.service"></context:component-scan>
	<context:component-scan base-package="com.zzxtit.spring.ioc.*.dao"></context:component-scan>
	<context:component-scan base-package="com.zzxtit.spring.ioc.*.action"></context:component-scan>
	 
	
	 
</beans>

Qualifier的意思是合格者,通过这个标示,表明了哪个实现类才是我们所需要的,添加@Qualifier注解,需要注意的是@Qualifier的参数名称为我们之前定义@Service注解的名称之一。 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值