Bean的装配
1、可以从ApplicationContext上下文获取和bean工厂获取容器,bean工厂只建议在移动端应用使用。
2、如果使用的是applicationContext配置的是bean,如果作用域是singleton,不管你使不使用都会被实例化。(好处是预先加载,欠缺点是耗内存)
3、如果是FactoryBean,则当实例化对象的时候,不会马上实例化bean,使用的时候才会实例化。(好处是节约内存,缺点速度慢)
4、bean作用域

强调一点:尽量使用scope=singleton,不要使用prototype,因为这样会对性能造成影响。
5、bean的装配
三种获取ApplicationContextCD的方法:
①ClassPathXmlApplicationContext 通过路径
②FileSystemXmlApplicationContext通过文件路径
③XmlWebApplicationContext从web系统加载。
6、给集合注入属性
①数组
<property name="empName">
<list>
<value>小明</value>
<value>小红</value>
<value>小化</value>
</list>
</property>
②list
<property name="empList">
<list>
<ref bean="emp1" />
<ref bean="emp2" />
<ref bean="emp3" />
</list>
</property>
③set
<property name="empSet">
<set>
<ref bean="emp1" />
<ref bean="emp2" />
<ref bean="emp3" />
</set>
</property>
④Map
<property name="empMap">
<map>
<entry key="11" value-ref="emp1"></entry>
<entry key="22" value-ref="emp2"></entry>
<entry key="33" value-ref="emp3"></entry>
</map>
</property>
</bean>
<bean id="emp1" class="com.Collection.Employee">
<property name="name" value="北京" />
<property name="id" value="1"></property>
</bean>
<bean id="emp2" class="com.Collection.Employee">
<property name="name" value="天津" />
<property name="id" value="2"></property>
</bean>
<bean id="emp3" class="com.Collection.Employee">
<property name="name" value="上海" />
<property name="id" value="3"></property>
</bean>
⑤继承
<bean id="student" class="com.inherit.Student">
<property name="name" value="xiaoming"></property>
<property name="age" value="22"></property>
</bean>
<bean id="graduate" parent="student" class="com.inherit.Graduate" >
<property name="degree" value="博士"></property>
</bean>
7、通过构造函数来注入
<bean id="employee" class="com.Construct.Employee" >
<constructor-arg index="0" type="java.lang.String" value="xiaohong"></constructor-arg>
<constructor-arg index="1" type="int" value="21"></constructor-arg>
</bean>
8、自动装配
①byName
<bean id="dog" class="com.autowire.Dog" >
<property name="name" value="大黄"></property>
<property name="age" value="3"></property>
</bean>
<bean id="master" class="com.autowire.Master" autowire="byName">
<property name="name" value="鸣人"></property>
</bean>
②byType
<bean id="dog12" class="com.autowire.Dog" >
<property name="name" value="大黄"></property>
<property name="age" value="3"></property>
</bean>
<bean id="master" class="com.autowire.Master" autowire="byType">
<property name="name" value="鸣人"></property>
</bean>
③constructor
<bean id="dog12" class="com.autowire.Dog" >
<property name="name" value="大黄"></property>
<property name="age" value="3"></property>
</bean>
<bean id="master" class="com.autowire.Master" autowire="constructor">
<property name="name" value="鸣人"></property>
</bean>
Bean的装配的更多相关文章
- Spring bean依赖注入、bean的装配及相关注解
依赖注入 Spring主要提供以下两种方法用于依赖注入 基于属性Setter方法注入 基于构造方法注入 Setter方法注入 例子: public class Communication { priv ...
- Spring学习记录(三)---bean自动装配autowire
Spring IoC容器可以自动装配(autowire)相互协作bean之间的关联关系,少写几个ref autowire: no ---默认情况,不自动装配,通过ref手动引用 byName---根据 ...
- Spring4学习笔记 - 配置Bean - 自动装配 关系 作用域 引用外部属性文件
1 Autowire自动装配 1.1 使用:只需在<bean>中使用autowire元素 <bean id="student" class="com.k ...
- Spring - 配置Bean - 自动装配 关系 作用域 引用外部属性文件
1 Autowire自动装配1.1 使用:只需在<bean>中使用autowire元素<bean id="student" class="com.kej ...
- spring2——IOC之Bean的装配
spring容器对于bean的装配提供了两个接口容器分别是"ApplicationContext接口容器"和"BeanFactory接口容器",其中" ...
- Spring温故而知新 - bean的装配(续)
按条件装配bean 就是当满足特定的条件时Spring容器才创建Bean,Spring中通过@Conditional注解来实现条件化配置bean package com.sl.ioc; import ...
- Spring温故而知新 - bean的装配
Spring装配机制 Spring提供了三种主要的装配机制: 1:通过XML进行显示配置 2:通过Java代码显示配置 3:自动化装配 自动化装配 Spring中IOC容器分两个步骤来完成自动化装配: ...
- Spring -bean的装配和注解的使用
一,bean的装配 bean是依赖注入的,通过spring容器取对象的. 装配方法有: 前面两种没什么好讲的,就改改参数就好了. 这里重要讲注解. 注解的主要类型见图,其中component是bean ...
- bean的装配方式(注入方式,构造注入,setter属性注入)
bean的装配方式有两种,构造注入和setter属性注入. public class User { private String username; private String password; ...
- Spring XML配置里的Bean自动装配
Spring自动装配 这段是我们之前编写的代码,代码中我们使用了P命名空间 并且使用手动装配的方式将car <bean id="address" class="cn ...
随机推荐
- Unity各平台内置宏定义
属性 方法 UNITY_EDITOR #define directive for calling Unity Editor scripts from your game code. UNITY_EDI ...
- canvas转img,blob相互转换
摘自:https://www.cnblogs.com/jyuf/p/7251591.html 函数都比较简单,直接看就ok了 /*----------------------------------- ...
- c++类模板分文件编写存在的问题
c++分文件编写的编译机制: 各个文件独立编译,如果在某.cpp文件中出现了函数调用,但是在此.cpp文件并没有对应函数的实现.此时就会在函数调用出生成特定的符号,在之后的链接过程完成函数调用. C+ ...
- thinphp5-image图片处理类库压缩图片
使用tp5的thinkphp-image类库处理图片 使用方法手册都有,为了增加印象我自己记录一下 手册:https://www.kancloud.cn/manual/thinkphp5/177530 ...
- coinmarketcap.com爬虫
coinmarketcap.com爬虫 写的真是蛋疼 # -*- coding:utf-8 -*- import requests from lxml import etree headers = { ...
- [Golang学习笔记] 07 数组和切片
01-06回顾: Go语言开发环境配置, 常用源码文件写法, 程序实体(尤其是变量)及其相关各种概念和编程技巧: 类型推断,变量重声明,可重名变量,类型推断,类型转换,别名类型和潜在类型 数组: 数组 ...
- spark 例子groupByKey分组计算
spark 例子groupByKey分组计算 例子描述: [分组.计算] 主要为两部分,将同类的数据分组归纳到一起,并将分组后的数据进行简单数学计算. 难点在于怎么去理解groupBy和groupBy ...
- 目标反射回波检测算法及其FPGA实现 之一:算法概述
目标反射回波检测算法及其FPGA实现之一:算法概述 前段时间,接触了一个声呐目标反射回波检测的项目.声呐接收机要实现的核心功能是在含有大量噪声的反射回波中,识别出发射机发出的激励信号的回波.我会分几篇 ...
- poj 2349 Arctic Network(prime)
Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 25165 Accepted: 7751 Description The ...
- Using Angular 1.x With ES6 and Webpack
http://angular-tips.com/blog/2015/06/using-angular-1-dot-x-with-es6-and-webpack/