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>