SSH与SSM学习之Spring02——bean元素配置
来源:互联网 发布:微信群淘宝优惠券代理 编辑:程序博客网 时间:2024/06/06 02:31
- SSH与SSM学习之Spring02bean元素配置
- 一bean元素说明
- 二scope属性
- 三scope代码示例
- 1 Userjava
- 2 测试代码
- 3 scope为singleton
- 31 applicationContextxml
- 32 运行结果
- 4 scope为prototype
- 41 applicationContextxml
- 42 运行结果
- 四生命周期属性
- 1 属性说明
- 2 applicationContextxml
- 3 代码示例
- 3 运行结果
SSH与SSM学习之Spring02——bean元素配置
一、bean元素说明
将对象交给spring容器管理的时候,我们需要使用 来配置。最基础配置如下
<?xml version="1.0" encoding="UTF-8" ?><beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd "> <!-- 将User对象交给spring容器管理 --> <!-- Bean元素:使用该元素描述需要spring容器管理的对象 class属性:被管理对象的完整类名. name属性:给被管理的对象起个名字.获得对象时根据该名称获得对象. 可以重复.可以使用特殊字符. id属性: 与name属性一模一样. 名称不可重复.不能使用特殊字符. 结论: 尽量使用name属性. --> <bean name="user" class="com.qwm.spring1.bean.User"></bean></beans>
这样我们在代码中就能使用这个对象了。
二、scope属性
scope属性有四个可选值,如下:
singleton(默认值):单例对象.被标识为单例的对象在spring容器中只会存在一个实例
prototype:多例原型.被标识为多例的对象,每次再获得才会创建.每次创建都是新的对象.整合struts2时,ActionBean必须配置为多例的.
request:web环境下.对象与request生命周期一致.
session:web环境下,对象与session生命周期一致.
三、scope代码示例
3.1 User.java
package com.qwm.spring1.bean;import java.io.Serializable;/** * @author: wiming * @date: 2017-09-25 14:45:56 星期一 * @decription: */public class User implements Serializable{ private String name; private int age; public User() { System.out.println("User实例化了----" + this); } public void init(){ System.out.println("---User---init---"); } public void destory(){ System.out.println("---User---destory---"); } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; }}
3.2 测试代码
@Testpublic void test1(){ //1.创建容器对象 ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); //2.获取User对象 User user1 = (User) applicationContext.getBean("user"); User user2 = (User) applicationContext.getBean("user"); User user3 = (User) applicationContext.getBean("user"); System.out.println(user1); System.out.println(user2); System.out.println(user3);}
3.3 scope为singleton
3.3.1 applicationContext.xml
这里我们可以使用默认,也可以自己设置。
可以是这样
<?xml version="1.0" encoding="UTF-8" ?><beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd "> <!--scope 可以选择 singleton(单例) 、prototype(多例)--> <bean name="user" class="com.qwm.spring1.bean.User"></bean></beans>
也可以是这样
<?xml version="1.0" encoding="UTF-8" ?><beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd "> <!--scope 可以选择 singleton(单例) 、prototype(多例)--> <bean name="user" class="com.qwm.spring1.bean.User" scope="singleton"></bean></beans>
3.3.2 运行结果
User实例化了----com.qwm.spring1.bean.User@3f49dace---User---init---com.qwm.spring1.bean.User@3f49dacecom.qwm.spring1.bean.User@3f49dacecom.qwm.spring1.bean.User@3f49dace
3.4 scope为prototype
3.4.1 applicationContext.xml
<?xml version="1.0" encoding="UTF-8" ?><beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd "> <!--scope 可以选择 singleton(单例) 、prototype(多例)--> <bean name="user" class="com.qwm.spring1.bean.User" scope="prototype"></bean></beans>
3.4.2 运行结果
User实例化了----com.qwm.spring1.bean.User@64bfbc86---User---init---User实例化了----com.qwm.spring1.bean.User@6325a3ee---User---init---User实例化了----com.qwm.spring1.bean.User@1d16f93d---User---init---com.qwm.spring1.bean.User@64bfbc86com.qwm.spring1.bean.User@6325a3eecom.qwm.spring1.bean.User@1d16f93d
四、生命周期属性
4.1 属性说明
init-method : 配置一个方法作为生命周期初始化方法.spring会在对象创建之后立即调用.
destory-method : 配置一个方法作为生命周期的销毁方法.spring容器在关闭并销毁所有容器中的对象之前调用.
4.2 applicationContext.xml
<?xml version="1.0" encoding="UTF-8" ?><beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd "> <!-- 将User对象交给spring容器管理 --> <!-- Bean元素:使用该元素描述需要spring容器管理的对象 class属性:被管理对象的完整类名. name属性:给被管理的对象起个名字.获得对象时根据该名称获得对象. 可以重复.可以使用特殊字符. id属性: 与name属性一模一样. 名称不可重复.不能使用特殊字符. 结论: 尽量使用name属性. --> <bean name="user" class="com.qwm.spring1.bean.User" init-method="init" destroy-method="destory"></bean></beans>
4.3 代码示例
/** * init 和 destory方法测试 */@Testpublic void test2(){ //1.创建容器对象 ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml"); //2.获取User对象 User User_1 = (User) ac.getBean("user"); System.out.println(User_1); //3.为了执行destory方法 ac.close();}
4.3 运行结果
User实例化了----com.qwm.spring1.bean.User@3f49dace---User---init---com.qwm.spring1.bean.User@3f49dace---User---destory---
阅读全文
0 0
- SSH与SSM学习之Spring02——bean元素配置
- SSH与SSM学习之SSH整合02——单独配置Spring容器
- SSH与SSM学习之SSH整合03——单独配置Struts2
- SSH与SSM学习之SSH整合05——单独配置Hibernate
- SSH与SSM学习之Spring16——Spring中AOP之使用配置文件配置
- SSH与SSM学习之Spring17——Spring中AOP之使用注解配置
- SSH与SSM学习之Spring22——Spring事务之xml配置方式管理事务
- SSH与SSM学习之Spring23——Spring事务之注解配置方式管理事务
- SSH与SSM学习之hibernate02——ORM配置详解
- SSH与SSM学习之hibernate03——主配置详解
- SSH与SSM学习之Spring09——Spring使用注解配置
- SSH与SSM学习之hibernate04——Configuration
- SSH与SSM学习之hibernate05——SessionFactory
- SSH与SSM学习之hibernate06——Session
- SSH与SSM学习之hibernate07——Transaction
- SSH与SSM学习之hibernate13——HQL查询
- SSH与SSM学习之hibernate14——Criteria查询
- SSH与SSM学习之hibernate21——离线查询
- XPDF安装与使用说明
- hibernate-使用data jpa
- 使用Spring Cloud Consul实现服务的注册和发现
- 西雅图的房子加价到底有多狠?
- 没有相爱,只有相杀:微软和谷歌又撕上了...
- SSH与SSM学习之Spring02——bean元素配置
- java中的Iterator和Iterable 区别
- 1011. A+B和C (15)
- SSH与SSM学习之Spring03——Spring创建对象的方式
- ionic 侧滑菜单
- html5 canvas绘图偶尔发生空白(图片不显示)
- DL 资源网站收集
- leetcode--链表总结
- Continuous-Time LTI System : The Convolution Integral Representation