..._请简述spring中作用域分别为singleton和prototype的bean实例化的时间点">
赞
踩
默认预先实例化,即在容器启动时实例化。
可以设置为懒实例化,即在第一次使用bean时实例化。
<bean id="user" factory-method="getUserBean" factory-bean="userbeanfactory" init-method="init" lazy-init="true">
<constructor-arg name="username" value="${username}"/>
<constructor-arg name="password" value="${password}"/>
</bean>
实际应用中都是预先实例化,虽然启动时较慢,但是用户访问时速度较快。
BeanFactory容器
只能懒实例化,使用bean时才会实例化。
简介:
在IOC容器中bean默认是单例的,存在问题。
单例中bean的全局属性是线程不安全的,多线程并发访问会出现问题。数据不安全
设置scope属性来指定作用域,配置为非单例的
<!--作用域
scope取值
singleton 单例
prototype 非单例
request 同一个请求中单例
session 同一个会话中单例
-->
<bean id="user" class="com.ioc13.entity.User" scope="singleton">
<property name="userName" value="admin"/>
<property name="pasword" value="admin"/>
</bean>
先添加jar包
<dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <scope>test</scope> </dependency> <!--spring-core--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> </dependency> <!--spring-bean--> <!-- https://mvnrepository.com/artifact/org.springframework/spring-beans --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> </dependency> <!--spring-context--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> </dependency> <!--spring-expression--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-expression</artifactId> </dependency> </dependencies>
第二步创建配置文件spring.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.xsd">
</beans>
第三步书写一个类User
package com.ioc13.entity; /** * package_name:com.ioc13.entity * Author:徐亚远 * Date:2020/2/6 15:15 * 项目名:springDemo01 * Desription: **/ public class User { private String userName; private String pasword; public User() { System.out.println("无参构造方法"); } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public void setPasword(String pasword) { this.pasword = pasword; } public String getPasword() { return pasword; } }
第四步 配置spring.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.xsd"> <!--作用域 scope取值 singleton 单例 prototype 非单例 request 同一个请求中单例 session 同一个会话中单例 --> <bean id="user" class="com.ioc13.entity.User" scope="singleton" lazy-init="true"> <property name="userName" value="admin"/> <property name="pasword" value="admin"/> </bean> </beans>
书写测试类UserController
package com.ioc13.controller; import com.ioc13.entity.User; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; /** * package_name:com.ioc13.controller * Author:徐亚远 * Date:2020/2/6 15:17 * 项目名:springDemo01 * Desription: **/ public class UserController { public static void main(String args[]) { ApplicationContext ac = new ClassPathXmlApplicationContext("/ioc13/spring.xml"); User user = (User) ac.getBean("user"); User user1 = (User) ac.getBean("user"); System.out.println("user:" + user); System.out.println("user1:" + user1); } }
测试结果如图:
赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。