JavaEE Spring框架学习笔记(理论学习之Bean的自动装配)
2016-12-13 21:57
441 查看
Bean的自动装配可实现无须配置xml即可完成Bean的装配,Bean的自动装配分为以下几种方式:
1.no 不使用自动装配(默认选项)2.byName Bean通过实现set方法IOC就可以自动装载与该属性名(类名)跟xml中相同ID值的Bean的自动装载。 必须设置与该类型相同名称的id值
2.byType 这种方式IOC容器将自动装载xml文件中配置相同类型 即相同类名的Bean;无须设置ID值
3.constructor 构造器的方式! 通过构造器自动装载与该参数相同类型的Bean id值为可选值
注意:byName 与byType 够必须实现set方法。 而constructor必须创建构造函数
官方说明:
Table 7.2. Autowiring modes
Mode | Explanation |
---|---|
no | (Default) No autowiring. Bean references must be defined via a refelement. Changing the default setting is not recommended for larger deployments, because specifying collaborators explicitly gives greater control and clarity. To some extent, it documents the structure of a system. |
byName | Autowiring by property name. Spring looks for a bean with the same name as the property that needs to be autowired. For example, if a bean definition is set to autowire by name, and it contains a master property (that is, it has a setMaster(..) method), Spring looks for a bean definition named master, and uses it to set the property. |
byType | Allows a property to be autowired if exactly one bean of the property type exists in the container. If more than one exists, a fatal exception is thrown, which indicates that you may not use byType autowiring for that bean. If there are no matching beans, nothing happens; the property is not set. |
constructor | Analogous to byType, but applies to constructor arguments. If there is not exactly one bean of the constructor argument type in the container, a fatal error is raised. |
相关文章推荐
- Java中常遇的中文乱码问题解决方案
- JpaRepository关于日期(不带时间)查询注意事项
- java数据类型及java的跨平台
- 解密Java byte类型的一个小问题
- 反转链表--Java实现
- Eclipse代码格式化无效解决方案
- eclipse+sdk+adk安卓开发环境部署
- maven+springmvc+dubbo+zookeeper
- Java 最基本常用类
- java继承
- shiro springmvc整合第一步
- SpringBoot 使用 @Value 从 YAML文件读取属性
- Initialization of bean failed; nested exception is java.lang.StackOverflowError
- MyEclipse注册
- eclipse、myeclipse开发工具
- 【SSM】Eclipse使用Maven创建Web项目+整合SSM框架
- Spring学习02--属性编辑器
- java响应式
- java 实现两个list交叉循环遍历
- Java编程思想:数据存储