码迷,mamicode.com
首页 > 其他好文 > 详细

装配bean

时间:2018-01-13 20:50:50      阅读:141      评论:0      收藏:0      [点我收藏+]

标签:global   pos   hello   告诉   ali   对象   singleton   text   group   

在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. 因为每次调用都会创建一个全新的对象,这样对性能不好
 

装配bean

标签:global   pos   hello   告诉   ali   对象   singleton   text   group   

原文地址:https://www.cnblogs.com/SunshineLittleCat/p/8280037.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!