在Spring配置文件的bean标签中的autowire属性值为byType
指:如果容器中存在一个与指定属性类型相同的bean,那么将与该属性自动装配。如果存在多个该类型的bean,那么将会抛出异常,并指出不能使用byType方式进行自动装配。若没有找到相匹配的bean,则什么事都不发生,属性也不会被设置。如果你不希望这样,那么可以通过设置dependency-check="objects"
让Spring抛出异常。
案例:autowire=“byType”
1、创建类StudentServiceImpl
2、创建类TeacherServiceImpl
以上两步骤请参考《Spring自动装配(一)autowire=“no”》
3、创建配置文件applicationContext.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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="studentServiceImpl" class="cn.zd.service.StudentServiceImpl">
<property name="studentName">
<value>张迪</value>
</property>
</bean>
<!-- TeacherServiceImpl这个类中因为使用了autowire="byType"参数,
所以当引用StudentServiceImpl这个类时会自动在配置文件中寻找与studentServiceImpl对象类型相同的bean来加载
就不需要使用ref来指明对象了。
注意:只适用于类的对象的引用
-->
<bean id="teacherServiceImpl" class="cn.zd.service.TeacherServiceImpl" autowire="byType">
<property name="teacherName">
<value>陈</value>
</property>
</bean>
</beans>
解析:TeacherServiceImpl这个类中因为使用了autowire="byName"参数,所以当引用StudentServiceImpl这个类时会自动在配置文件中寻找与studentServiceImpl对象类型相同的bean来加载就不需要使用ref来指明对象了。
注意:只适用于类的对象的引用
4、测试类TestNO,参考《Spring自动装配(一)autowire=“no”》
5、运行结果:
老师的名字是:陈
陈老师的学生是:张迪
-----------------------------------------------
以上为个人理解总结,若有不足,请高手指点,谢谢.....
分享到:
相关推荐
3.autowire=bytype 详细解析注入方式 例如:有如下两个类需要注入 第一个类: 1.package org.jia; 2. 3. public class Order { 4. private String orderNum; 5. @Suppres
Spring自动装配的方式和举例、以及@Qualifier、@Autowire、@Resource的使用。
内容如下: spring.rar [spring_aop1] [spring_aop2] [spring_aop3] [spring_aop4] [spring_autowire_byName] [spring_autowire_byType] [spring_beginning] [spring_hibernate_1] [spring_hibernate_2] ...
主要介绍了Spring实战之@Autowire注解用法,结合实例形式详细分析了Spring @Autowire注解具体实现步骤与相关使用技巧,需要的朋友可以参考下
NULL 博文链接:https://garrincha.iteye.com/blog/2109488
以Spring5.3.6为演示基础,以多个范例显示演示了autowire多种形式的应用 default、byName、byType、constructor 并对相关使用做了一定的对比
在idea2020 下写的spring5-autowired 小例子。person,cat dog ,给大家一个参考。
13.@Autowire注解与自动装配 14.让Spring自动扫描和管理Bean 15.使用JDK中的Proxy技术实现AOP功能 16.使用CGLIB实现AOP功能与AOP概念解释 17.使用Spring的注解方式实现AOP入门 18.使用Spring的注解方式实现AOP...
NULL 博文链接:https://diaochenlong2.iteye.com/blog/1831333
#学习springboot自动装配 ##一,手动装配 ### 1,模式注解装配 @Component注解,或者@Component注解的扩展,@ Controller,@ Service,存储库,@ Configruation等, ### 2. @ Configuration启动容器+ @ Bean注册...
使用autowire属性指定自动装配的方式byName根据bean的名字和当前bean的setter风格属性名进行自动装配若有匹配,则自动转配若无匹配,则不装配
<bean id="startQuertz" lazy-init="false" autowire="no" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="triggers"> <ref bean="runTime" /> </bean>
实现了简单的ORM增删改查。 ... abstract="false" lazy-init="default" autowire="default" dependency-check="default"> <property name="dataSource"> <ref local="dataSource" /> </bean>
八、 自动装配autowire 13 (一) byName 13 (二) byType 14 (三) 注意 14 九、 生命周期 15 (一) lazy-init/default-lazy-init 15 (二) init-method destroy-method 不要和prototype一起用(了解) 15 第六课:...
autowire自动导入句柄
可以在一个应用上下文中定义多个配置文件,每个配置文件设置自己的默认自动装配策略(default-autowire) 如果使用constructor自动装配策略,就不能混合使用constructor-arg 注解方式可以实现更细粒度的自动装配,...
<bean name="startQuertz" lazy-init="false" autowire="no" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="triggers"> <ref bean="myJobTrigger" /> ...
八、 自动装配autowire 13 (一) byName 13 (二) byType 14 (三) 注意 14 九、 生命周期 15 (一) lazy-init/default-lazy-init 15 (二) init-method destroy-method 不要和prototype一起用(了解) 15 第六课:...
<bean id="startQuertz" lazy-init="false" autowire="no" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="triggers"> <ref bean="getPolicyTime"/> </beans>