第二讲 使用Spring IoC创建对象的3种方式
来源:互联网 发布:postgresql vs mysql 编辑:程序博客网 时间:2024/05/22 23:05
一、通过无参的构造方法来创建对象
- 编写实体类
publicclass User {
publicUser() {
System.out.println("user的无参构造方法");
}
privateString name;
publicvoid setName(String name) {
this.name= name;
}
publicvoid show() {
System.out.println("name="+ name);
}
}
- 编写beans.xml配置文件
<beansxmlns="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.xsd">
<!-- scope属性默认是单例的可以不用写 -->
<beanid="user"class="com.liujie.model.User"scope="singleton">
<propertyname="name"value="张三"></property>
</bean>
</beans>
spring中bean的作用域(scope属性),有如下5种类型:
- singleton:单例模式,在整个Spring IoC容器中,使用singleton定义的Bean将只有一个实例;
- prototype:原型模式,每次通过容器的getBean方法获取prototype定义的Bean时,都将产生一个新的Bean实例;
- request:对于每次HTTP请求,使用request定义的Bean都将产生一个新实例,即每次HTTP请求将会产生不同的Bean实例。只有在 Web应用中使用Spring时,该作用域才有效;
- session:对于每次HTTP Session,使用session定义的Bean都将产生一个新实例。同样只有在Web应用中使用Spring时,该作用域才有效;
- global session:每个全局的HTTP Session,使用session定义的Bean都将产生一个新实例。典型情况下,仅在使用portlet context的时候有效。同样只有在Web应用中使用Spring时,该作用域才有效。
在多数情况,我们只会使用singleton和prototype两种scope,如果在spring配置文件内未指定scope属性,默认为singleton。
- 测试
publicclass Test {
publicstatic void main(String[]args) {
// 解析beans.xml文件,生成管理相应的bean对象
ApplicationContextcontext = newClassPathXmlApplicationContext("beans.xml");
// BeanFactory context = new ClassPathXmlApplicationContext("beans.xml");
Useruser = (User) context.getBean("user");
user.show();
//用来检测spring创建user对象时是否使用的是单例模式(通过构造方法是否被调用多次来检测的)
context.getBean("user");
context.getBean("user");
}
}
二、通过有参构造方法来创建对象
- 编写实体类
publicclass User {
privateString name;
publicUser(String name) {
super();
this.name= name;
}
publicvoid show() {
System.out.println("name="+ name);
}
}
- 编写beans.xml配置文件
第一种:根据参数的下标来设置
<?xmlversion="1.0"encoding="UTF-8"?>
<beansxmlns="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.xsd">
<beanid="user"class="com.liujie.model.User">
<!-- index指构造方法参数,下标从0开始 -->
<constructor-argindex="0"value="李四"></constructor-arg>
</bean>
</beans>
第二种:根据参数名称来设置
<beanid="user"class="com.liujie.model.User">
<!-- name指参数名 -->
<constructor-argname="name"value="李四"></constructor-arg>
</bean>
第三种:根据参数类型来设置
<beanid="user"class="com.liujie.model.User">
<constructor-argtype="java.lang.String"value="李四"></constructor-arg>
</bean>
- 测试
publicclass Test {
publicstatic void main(String[]args) {
ApplicationContextcontext = newClassPathXmlApplicationContext("beans.xml");
Useruser = (User) context.getBean("user");
user.show();
}
}
三、通过工厂方法来创建对象
静态工厂:
- 编写实体类
publicclass User {
privateString name;
publicUser(String name) {
super();
this.name= name;
System.out.println("user有参构造方法");
}
publicvoid show() {
System.out.println("name="+ name);
}
}
- 编写工厂类
publicclass UserFactory {
publicstatic User newInstance(Stringname) {
returnnew User(name);
}
}
- 编写beans.xml配置文件
<beanid="user"class="com.liujie.factory.UserFactory"factory-method="newInstance">
<constructor-argindex="0"value="王五"></constructor-arg>
</bean>
- 测试
publicclass Test {
publicstatic void main(String[]args) {
ApplicationContextcontext = newClassPathXmlApplicationContext("beans.xml");
Useruser = (User) context.getBean("user");
user.show();
context.getBean("user");
context.getBean("user");
}
}
动态工厂:
- 编写工厂类
publicclass UserDynamicFactory {
publicUser newInstance(Stringname) {
returnnew User(name);
}
}
- 编写beans.xml配置文件
<beanid="userFactory"class="com.liujie.factory.UserDynamicFactory"></bean>
<beanid="user"factory-bean="userFactory"factory-method="newInstance">
<constructor-argindex="0"value="王五"></constructor-arg>
</bean>
不管是动态工厂还是静态工厂,user对象还是受
<beanid="user"factory-bean="userFactory"factory-method="newInstance"scope="prototype">
中的scope参数的影响。
如果bean对象的scope="singleton"(默认),bean对象是直接创建的,通过applicationContext.getBean获取对象时,永远都
不会创建新的对象;
如果bean对象的scope="prototype",bean对象不会直接创建,编写bean以后一定要通过applicationContext.getBean获取对象
时,才会是创建对象(如果有被引用的bean且scope="prototype",这个时候也会被创建),并且获取一次创建一次。
阅读全文
0 0
- 第二讲 使用Spring IoC创建对象的3种方式
- spring IOC容器创建对象的三种方式
- Spring IOC创建对象的三种方式
- 重温spring框架1--ioc创建对象的方式
- spring创建对象的方式(3种)
- Spring - 开始 , IOC创建对象,别名 和 创建对象的方式
- 使用ioc来创建对象有三种方式
- 使用Spring创建对象的方式
- 深入研究Spring-IoC :容器创建的几种方式
- Spring IOC 创建bean实例的方式
- Spring创建对象的方式3种方式
- Spring(IOC) 对象创建三种方式、对象bean起别名
- spring创建对象的几种方式
- 使用IOC创建对象
- 【Spring】IOC容器创建对象
- spring IOC控制反转及IOC实现的三种方式和bean标签使用
- Spring 研究spring创建对象的方式
- Spring - IOC创建对象的时机,创建Bean的Scope 及其 IOC的 生命周期
- RAD Studio 10 自带Demo代码汇总说明
- [学习笔记]用户界面优化之Android ViewPager
- POJ 1405 Heritage 笔记
- oracle锁表查询及解锁kill进程
- JVM结构、GC工作机制详解
- 第二讲 使用Spring IoC创建对象的3种方式
- Javaweb中快速生成验证码Captcha
- 云服务器 ECS YUM 在线安装软件方法和常见问题处理
- Qt 界面窗口设置
- TensorFlow在Windows 7上的详细安装步骤
- 数据结构上机作业3:栈——八皇后
- 深度优先搜索例1--走迷宫问题
- Web.xml详解
- STC15系列单片机命名规则