装配bean

在spring容器内把bean组合起来就叫做装配bean,装配bean需要告诉spring有哪些bean需要使用,以及他们的依赖注入如何配合使用
 
加载配置
可以使用XmlBeanFactory 调用ClassPathResource加载配置文件
或者通过ClassPathXmlApplictionContext 从类的路径加载配置文件
 
对bean的基本配置是 配置bean的id 和 class 的全类名, id 不可重复
例如:
<bean id="service" class="com.helloSpring.UserService">
<!-- property 就是UserService 这个类的属性 value就是我们给属性赋值 相当于调用了UserService.setName-->
<property name="name">
<value>spring学习</value>
</property>
<!-- 设置引用属性 UserService 引用了GroupService ref 引用必须是一个已经存在的配置-->
<property name="groupService" ref="groupService"/>
</bean>
 
scope 配置
scope 是bean里面的配置,基本的类型主要有以下几种:
prototype, singleton, request-session, global-session,在spring中默认的scope 是singleton
例如:
<bean id="student" class="com.getBean.Student" scope="prototype">
<property name="name">
<value>胖子哟</value>
</property>
<property name="age">
<value>1</value>
</property>
<property name="id">
<value>1</value>
</property>
</bean>
如果非必要的情况下,不要使用prototype. 因为每次调用都会创建一个全新的对象,这样对性能不好
 
posted @ 2018-01-13 17:33  晴天小猫  阅读(133)  评论(0编辑  收藏  举报