当前位置: 首页 > news >正文

什么是bean的自动装配,有哪些方式? -- java面试

自动装配与手动装配

Spring自动装配:无需在Spring配置文件中描述javaBean之间的依赖关系(如配置<property><constructor-arg>)。 IOC容器会自动建立javaBean之间的关联关系。

如果没有采用自动装配的话,就需要手动装配,通常在配置文件中实现。
以下代码就是手动装配:

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
	http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
 
	<bean id="customerDAO" class="com.hebeu.customer.dao.JdbcCustomerDAO">
		<property name="dataSource" ref="dataSource" />
	</bean>
 
</beans>

通过<property name="dataSource" ref="dataSource" />向customerDAO的bean注入了dataSource。

在Spring框架中,只需要在中定义"autowire"属性,即可开启自动装配功能。

<bean id="customer" class="com.yiibai.common.Customer" autowire="byName" />

自动装配模式

在Spring中,支持5种自动装配模式。

  • no 缺省情况下,自动配置是通过"ref"属性手动设定

  • byName 根据bean的属性名称进行自动装配

    Cutomer的属性名称是person,Spring会将bean id为person的bean通过setter方法进行自动装配。

<bean id="cutomer" class="com.xxx.xxx.Cutomer" autowire="byName"/>
<bean id="person" class="com.xxx.xxx.Person"/>
  • byType 根据bean的类型进行自动装配

    Cutomer的属性person的类型为Person,Spirng会将Person类型通过setter方法进行自动装配。

<bean id="cutomer" class="com.xxx.xxx.Cutomer" autowire="byType"/>
<bean id="person" class="com.xxx.xxx.Person"/>
  • constructor 类似byType,不是是应用于构造器的参数。如果一个bean与构造器参数的类型相同,则进行自动装配,否则导致异常。

    Cutomer构造函数的参数person的类型为Person,Spirng会将Person类型通过构造方法进行自动装配。

<bean id="cutomer" class="com.xxx.xxx.Cutomer" autowire="construtor"/> <bean id="person" class="com.xxx.xxx.Person"/>
  • autodetect 如果有默认的构造器,则通过constructor方式自动装配,否则使用byType方式自动装配。

    在Spring3.0以后的版本被废弃,已经不再合法了。

相关文章:

  • Spring Boot、Spring MVC 和 Spring 有什么区别 -- java面试
  • SpringMVC工作流程 -- java面试
  • Spring MVC的主要组件 -- java面试
  • javaConfig是什么
  • Spring Boot自动配置原理 -- java面试
  • 如何理解 Spring Boot 中的 Starter -- java面试
  • 什么是嵌入式服务器?为什么要使用嵌入式服务器? -- java面试
  • mybatis的介绍以及优缺点 -- java面试
  • MyBatis 与Hibernate 有哪些不同 -- java面试
  • #{}和${}的区别是什么 -- java面试
  • idea上传到github以及移除文件操作
  • Modifier ‘private‘ not allowed here
  • 验证线程池扩展能力的测试方法的问题
  • win10 wifi有时候搜索不到
  • Mac 命令行关闭和打开蓝牙
  • Dubbo 整合 Pinpoint 做分布式服务请求跟踪
  • Hibernate【inverse和cascade属性】知识要点
  • JS数组方法汇总
  • leetcode388. Longest Absolute File Path
  • MySQL-事务管理(基础)
  • MySQL用户中的%到底包不包括localhost?
  • React 快速上手 - 06 容器组件、展示组件、操作组件
  • V4L2视频输入框架概述
  • webpack入门学习手记(二)
  • 蓝海存储开关机注意事项总结
  • 面试遇到的一些题
  • 模型微调
  • 事件委托的小应用
  • 在 Chrome DevTools 中调试 JavaScript 入门
  • 找一份好的前端工作,起点很重要
  • ​2021半年盘点,不想你错过的重磅新书
  • ​Kaggle X光肺炎检测比赛第二名方案解析 | CVPR 2020 Workshop
  • ​马来语翻译中文去哪比较好?
  • ​人工智能书单(数学基础篇)
  • ​学习一下,什么是预包装食品?​
  • $jQuery 重写Alert样式方法
  • (java版)排序算法----【冒泡,选择,插入,希尔,快速排序,归并排序,基数排序】超详细~~
  • (ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY)讲解
  • (附源码)spring boot建达集团公司平台 毕业设计 141538
  • (附源码)springboot掌上博客系统 毕业设计063131
  • (论文阅读26/100)Weakly-supervised learning with convolutional neural networks
  • (算法设计与分析)第一章算法概述-习题
  • (一) springboot详细介绍
  • .[hudsonL@cock.li].mkp勒索加密数据库完美恢复---惜分飞
  • .MyFile@waifu.club.wis.mkp勒索病毒数据怎么处理|数据解密恢复
  • .NET设计模式(7):创建型模式专题总结(Creational Pattern)
  • @AliasFor注解
  • @Autowired和@Resource的区别
  • @configuration注解_2w字长文给你讲透了配置类为什么要添加 @Configuration注解
  • [AI]文心一言出圈的同时,NLP处理下的ChatGPT-4.5最新资讯
  • [BIZ] - 1.金融交易系统特点
  • [BZOJ 3282] Tree 【LCT】
  • [bzoj1006]: [HNOI2008]神奇的国度(最大势算法)
  • [CC-FNCS]Chef and Churu
  • [go] 策略模式