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 ...
随机推荐
- Linux-- 查看文件 more与其它
more 翻页查看 用法:more 文件名 nl 显示行号打印(不常用) 1.不打印空白行行号:nl -b t 文件名 类似 cat -b 文件名 2.打印所有行行号:nl -b a 文件名 类似 c ...
- BootStrapValidate 简单使用
前阵子用了bootstrapvalidate写了一个登录验证,这里小记一笔 首先需要引入 bootstrapValidator.css //可不引入 jquery-2.1.0.min.js boots ...
- 前端基础-jQuery的最常用的的方法each、data、
阅读目录 each inArray get index data 一.jQuery中each方法的应用 jQuery中有个很重要的核心方法each,大部分jQuery方法在内部都会调用each,其主要 ...
- php (zip)文件下载设置
普通下载头大概意思,文件输出的地方二选一,小文件下载.如文件较大时注意执行时间与内存使用.可以看php大文件下载 $filename = $_GET['filename']; $pathname = ...
- Spark Streaming核心概念与编程
Spark Streaming核心概念与编程 1. 核心概念 StreamingContext Create StreamingContext import org.apache.spark._ im ...
- Rails中生成并在ActionMailer附件中发送csv、excel、pdf、zip文件
1.修改Gemfile # PDF generator gem 'prawn' gem 'prawn-table' # Excel generator gem 'rubyXL' #Zip genera ...
- actor、reactor与proactor模型:高性能服务器的几种模型概念(转)
actor模型: 实体之通过消息通讯,各自处理自己的数据,能够实现这并行. 说白了,有点像rpc. skynet是actor模型. reactor模型: 1 向事件分发器注册事件回调 2 事件发生 4 ...
- React with webpack
//entry.js require("!style!css!./style.css"); require("./hello.jsx"); // documen ...
- Linux Shell中的特殊符号和含义简明总结(包含了绝大部份)
case语句适用于需要进行多重分支的应用情况. case分支语句的格式如下: case $变量名 in 模式1) 命令序列1 ;; 模式2) 命令序列2 ;; *) 默认执行的命令序列 ...
- python 发红包的小程序
红包1 import random def redpacket(cash,person): lst=[] sum1=0 cash = cash * 100 while cash>0 and pe ...