beans.xml中,添加bean,必须添加的属性有id和class,同时,还有一个可选、但很重要的属性,scope,指明这个bean的域空间,spring默认的scope是singleton,支持以下四种scope
[list]
[*]prototype
[*]singleton
[*]session
[*]request
[/list]
beans.xml的配置如下:
1、prototype表示原始类型,也就是我们最开始使用的java对象声明方式,会生成多个对象。
2、singleton就是指单例,每个类只有一个实例
3、session和request都是在spring mvc里面的概念,这里感兴趣的同学可以网上搜罗下这两者的区别,简单的说,session是指整个会话的时间内有效,reqeust是指一次请求有效
[list]
[*]prototype
[*]singleton
[*]session
[*]request
[/list]
beans.xml的配置如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean id="userService" class="com.jt.service.UserService" scope="prototype">
</bean>
</beans>
1、prototype表示原始类型,也就是我们最开始使用的java对象声明方式,会生成多个对象。
2、singleton就是指单例,每个类只有一个实例
3、session和request都是在spring mvc里面的概念,这里感兴趣的同学可以网上搜罗下这两者的区别,简单的说,session是指整个会话的时间内有效,reqeust是指一次请求有效