在Spring配置文件中bean标签中的autowire属性的值为byName
指:根据属性名自动装配。此选项将检查容器并根据名字查找与属性完全一致的bean,并将其与属性自动装配。例如,在bean定义中将autowire设置为by
name,而该bean包含master属性(同时提供setMaster(..)方法),Spring就会查找名为master的bean定义,并用它来装配给master属性。
案例:autowire=“byName”
1、创建TeacherServiceImpl类
2、创建StudentServiceImpl类
以上两步骤的代码见《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="byName"参数,
所以当引用StudentServiceImpl这个类时会自动在配置文件中寻找与studentServiceImpl对象名相同的id名进行加载就不需要使用ref来指明对象了。
注意:只适用于类的对象的引用
-->
<bean id="teacherServiceImpl" class="cn.zd.service.TeacherServiceImpl" autowire="byName">
<property name="teacherName">
<value>陈</value>
</property>
</bean>
</beans>
解析:TeacherServiceImpl这个类中因为使用了autowire="byName"参数,所以当引用StudentServiceImpl这个类时会自动在配置文件中寻找与studentServiceImpl对象名相同的id名进行加载就不需要使用ref来指明对象了。
注意:只适用于类的对象的引用。
4、测试类TestNO,代码见《Spring自动装配(一)autowire=“no”》
5、运行结果:
老师的名字是:陈
陈老师的学生是:张迪
------------------------------------------
以上为个人理解总结,若有不足,请高手指点....
分享到:
相关推荐
Spring自动装配的方式和举例、以及@Qualifier、@Autowire、@Resource的使用。
在idea2020 下写的spring5-autowired 小例子。person,cat dog ,给大家一个参考。
主要介绍了Spring实战之@Autowire注解用法,结合实例形式详细分析了Spring @Autowire注解具体实现步骤与相关使用技巧,需要的朋友可以参考下
2.autowire=“byName” 3.autowire=bytype 详细解析注入方式 例如:有如下两个类需要注入 第一个类: 1.package org.jia; 2. 3. public class Order { 4. private String orderNum;...
内容如下: spring.rar [spring_aop1] [spring_aop2] [spring_aop3] [spring_aop4] [spring_autowire_byName] [spring_autowire_byType] [spring_beginning] [spring_hibernate_1] [spring_hibernate_2] ...
NULL 博文链接:https://garrincha.iteye.com/blog/2109488
以Spring5.3.6为演示基础,以多个范例显示演示了autowire多种形式的应用 default、byName、byType、constructor 并对相关使用做了一定的对比
使用autowire属性指定自动装配的方式byName根据bean的名字和当前bean的setter风格属性名进行自动装配若有匹配,则自动转配若无匹配,则不装配
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自动导入句柄
<bean id="startQuertz" lazy-init="false" autowire="no" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="triggers"> <ref bean="runTime" /> </bean>
八、 自动装配autowire 13 (一) byName 13 (二) byType 14 (三) 注意 14 九、 生命周期 15 (一) lazy-init/default-lazy-init 15 (二) init-method destroy-method 不要和prototype一起用(了解) 15 第六课:...
实现了简单的ORM增删改查。 ... abstract="false" lazy-init="default" autowire="default" dependency-check="default"> <property name="dataSource"> <ref local="dataSource" /> </bean>
<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>
Spring系列第7篇:依赖注入之手动注入Spring系列第8篇:自动注入(autowire)详解,高手在于坚持Spring系列第9篇:depend-on到底是干什么的?Spring系列第10篇:primary可以解决什么问题?Spring系列第11篇:bean中...