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 ...
随机推荐
- multiprocessing进程开发RuntimeError
windows环境下multiprocessing报如下异常信息: RuntimeError: An attempt has been made to start a new process befo ...
- 日期插件rolldate.js的使用
日期插件rolldate.js的使用 下载地址:http://www.jq22.com/jquery-info19834 效果: 代码: <!DOCTYPE html> <html ...
- vue-网易云音乐
vuejs仿写网易云音乐webapp 1.项目API来源 2.项目地址 3.项目主要截图 4.功能 音乐播放,搜索及主要页面的展示,用户登录部分及登录后才能获取的数据还未实现 5.技术 ...
- php 获取当前完整url地址
echo $url = $_SERVER["REQUEST_SCHEME"].'://'.$_SERVER["SERVER_NAME"].$_SERVER[&q ...
- 05.kafka提前准备工作:搭建zookeeper集群环境
总体参考:http://www.cnblogs.com/zhangs1986/p/6564839.html 搭建之间同步下spark01.02.03的环境 复制/opt/flume这个文件夹到 spa ...
- firefox burp ssl证书配置
打开浏览器 设置->证书->证书频发机构->添加证书 添加成功->找到位置->编辑信任->信任->查看证书 导出-> win下可直接安装证书->受 ...
- 关于使用READ TABLE语句
READ tabe 是用来遍历内表,取第一条符合条件的记录. READ TABLE <itab> [INTO <wa>] WITH KEY <key> [BINAR ...
- Python之路(六)---> 函数、变量
Python中的函数和数学上的函数定义是不一样的,从数学的角度上来说函数的定义:给定一个数集A,假设其中的元素为x.现对A中的元素x施加对应法则f,记作f(x),得到另一数集B.假设B中的元素为y.则 ...
- vs2013发布网站合并程序是出错(ILmerge.merge:error)
Vs2013发布网站时,生成错误提示: 合并程序集时出错: ILMerge.Merge: ERROR!!: Duplicate type 'manage_ForcePasswrod' found in ...
- maven拓展——使用tomcat插件运行maven项目
首先,在pom.xml中配置插件: <build> <plugins> <plugin> <groupId>org.apache.tomcat.mave ...