scope:作用域 singleton prototype request session 默认为singleton
lazy-init:default=false ,false ,true 默认为default
false:不延迟初始化
lazy-init结合scope=singleton使用 scope="singleton" lazy-init="default" -->说明:容器已经加载就实例化对象 scope="singleton" lazy-init="true" -->说明:容器已经加载当使用到该对象的时候,实例化该对象
根据上篇博客进行补充讲解:http://blog.csdn.net/zhaoyazhi2129/article/details/8846707
HelloDaoImpl.java
package www.csdn.spring.dao;
public class HelloDaoImpl implements HelloDao{
public HelloDaoImpl(){
System.out.println("------HelloDaoImpl实例化");
}
@Override
public void sayHello() {
System.out.println("helloworld");
}
}
HelloServiceImpl.java
package www.csdn.spring.service;
import www.csdn.spring.dao.HelloDao;
public class HelloServiceImpl implements HelloService {
HelloDao helloDao;
public void setHelloDao(HelloDao helloDao) {
System.out
.println("控制反转:应用程序本身不在负责创建helloDao对象,而是由spring容器负责创建、管理、维护,这样控制权转移,称为反转。"
+ "可以通过依赖注入方式 注入该HelloDao对象");
this.helloDao = helloDao;
}
public HelloServiceImpl() {
System.out.println("HelloServiceImpl实例化");
}
@Override
public void sayHello() {
helloDao.sayHello();
}
}
情况1:scope=prototype,lazy-init=任何值: 都延迟加载,只有调用实例时候才加载
spring_dao.java
<?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">
<!-- 实例对象的名称,class:全名 spring容器,负责创建,管理,维护bean 并且能够依赖注入到相应组件上 -->
<bean id="helloDaoImpl" class="www.csdn.spring.dao.HelloDaoImpl" scope="prototype" lazy-init="false">
</bean>
</beans>
spring_service.java
<?xml version="1.0" encoding="UTF-8"?>
<beans
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans">
<!-- 实例对象的名称,class:全名 spring容器,负责创建,管理,维护bean 并且能够依赖注入到相应组件上 bean对象 -->
<bean id="helloServiceImpl" class="www.csdn.spring.service.HelloServiceImpl" scope="prototype" lazy-init="true">
<property name="helloDao" ref="helloDaoImpl"></property>
</bean>
</beans>
HelloTest.java
package www.csdn.spring.test;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class HelloTest {
@Test
public void test() {
// 容器创建 实例化容器
// 读取 classes 路径下面的文件 参数 动态参数、单个参数、数组 等
ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
// HelloService helloService = context.getBean("helloServiceImpl", HelloService.class);
// helloService.sayHello();
}
}
HelloTest.java
package www.csdn.spring.test;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class HelloTest {
@Test
public void test() {
// 容器创建 实例化容器
// 读取 classes 路径下面的文件 参数 动态参数、单个参数、数组 等
ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
HelloService helloService = context.getBean("helloServiceImpl", HelloService.class);
helloService.sayHello();
}
}
情况2:scope=singleton,lazy-init=default/false: 不延迟加载,不调用实例时候也加载
spring_dao.java
<?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">
<!-- 实例对象的名称,class:全名 spring容器,负责创建,管理,维护bean 并且能够依赖注入到相应组件上 -->
<bean id="helloDaoImpl" class="www.csdn.spring.dao.HelloDaoImpl" scope="singleton" lazy-init="false">
</bean>
</beans>
spring_service.java
<?xml version="1.0" encoding="UTF-8"?>
<beans
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans">
<!-- 实例对象的名称,class:全名 spring容器,负责创建,管理,维护bean 并且能够依赖注入到相应组件上 bean对象 -->
<bean id="helloServiceImpl" class="www.csdn.spring.service.HelloServiceImpl" scope="singleton" lazy-init="false">
<property name="helloDao" ref="helloDaoImpl"></property>
</bean>
</beans>
HelloTest.java
package www.csdn.spring.test;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class HelloTest {
@Test
public void test() {
// 容器创建 实例化容器
// 读取 classes 路径下面的文件 参数 动态参数、单个参数、数组 等
ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
// HelloService helloService = context.getBean("helloServiceImpl", HelloService.class);
// helloService.sayHello();
}
}
情况3:scope=singleton,lazy-init=true:
延迟加载,调用实例时候才加载
spring_dao.java
<?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">
<!-- 实例对象的名称,class:全名 spring容器,负责创建,管理,维护bean 并且能够依赖注入到相应组件上 -->
<bean id="helloDaoImpl" class="www.csdn.spring.dao.HelloDaoImpl" scope="singleton" lazy-init="ture">
</bean>
</beans>
spring_service.java
<?xml version="1.0" encoding="UTF-8"?>
<beans
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans">
<!-- 实例对象的名称,class:全名 spring容器,负责创建,管理,维护bean 并且能够依赖注入到相应组件上 bean对象 -->
<bean id="helloServiceImpl" class="www.csdn.spring.service.HelloServiceImpl" scope="singleton" lazy-init="true">
<property name="helloDao" ref="helloDaoImpl"></property>
</bean>
</beans>
HelloTest.java
package www.csdn.spring.test;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class HelloTest {
@Test
public void test() {
// 容器创建 实例化容器
// 读取 classes 路径下面的文件 参数 动态参数、单个参数、数组 等
ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
// HelloService helloService = context.getBean("helloServiceImpl", HelloService.class);
// helloService.sayHello();
}
}
情况4:两个配置文件一方属性为scope=singleton,lazy-init=false/default:
只有一方不延迟加载,不调用实例也加载,另一方则调用时加载
spring_dao.java
<?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">
<!-- 实例对象的名称,class:全名 spring容器,负责创建,管理,维护bean 并且能够依赖注入到相应组件上 -->
<bean id="helloDaoImpl" class="www.csdn.spring.dao.HelloDaoImpl" scope="singleton" lazy-init="false">
</bean>
</beans>
spring_service.java
<?xml version="1.0" encoding="UTF-8"?>
<beans
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans">
<!-- 实例对象的名称,class:全名 spring容器,负责创建,管理,维护bean 并且能够依赖注入到相应组件上 bean对象 -->
<bean id="helloServiceImpl" class="www.csdn.spring.service.HelloServiceImpl" scope="singleton" lazy-init="true">
<property name="helloDao" ref="helloDaoImpl"></property>
</bean>
</beans>
HelloTest.java
package www.csdn.spring.test;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class HelloTest {
@Test
public void test() {
// 容器创建 实例化容器
// 读取 classes 路径下面的文件 参数 动态参数、单个参数、数组 等
ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
// HelloService helloService = context.getBean("helloServiceImpl", HelloService.class);
// helloService.sayHello();
}
}
分享到:
相关推荐
Spring IOC Bean标签属性介绍 0.Bean标签属性介绍 1.0 新建一个Maven工程 1.1 pom.xml 1.2 实体类JavaBean 1.2.1 User类 1.3 当Scope="singleton"时 1.4 当 Scope="singleton" 且 lazy-init="true" 时 1.5 当scope=...
下面小编就为大家带来一篇浅谈spring中的default-lazy-init参数和lazy-init。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
这几天训网络最困扰我的问题就是服务器...EXT4-fs error (device sda1): ext4_lookup:1593: inode #6004222: comm trash-expunge: deleted inode referenced: 6004301 类似这样的错误,看样子是sda1磁盘文件系统的问题
这是react-lazy-load-image-component NPM 包的演示。 它既不打算在现实生活中使用,也不是好的编程实践的一个例子,而是要给出一个如何使用包react-lazy-load-image-component及其道具的例子。 观看现场演示: : ...
Spring系列第12篇:lazy-init:bean延迟初始化Spring系列第13篇:使用继承简化bean配置(abstract & parent)Spring系列第14篇:lookup-method和replaced-method比较陌生,怎么玩的?Spring系列第15篇:代理详解(Java...
该插件为上的所有img和iframe标签添加loading="lazy" 。 无需配置。 如果已经存在loading属性,则不会进行任何更改。 loading="lazy"导致图像和iframe延迟加载而没有任何JavaScript。 正在增长。 如果浏览器不支持...
lazy_object_proxy-1.6.0-cp37-cp37m-win_amd64
注:LazyImg 必须定义lazy-data属性,属性值是src的图片路径 引入JS文件:<script type="text/javascript" src="js/lazyImg.v1.0.js"> 默认情况下: 在IMG中满足以任何一个条件,都会加载图片; 1、没有class属性 2...
Generator Lazy Demo 跟着例子在3分钟内玩转Generator Lazy。 Useage 向导模式创建 在任意目录中执行yo lazy,然后一路NEXT即可。 在包含配置文件的项目中使用 命令: yo lazy cd build-with-config-exist yo lazy 在...
可以使用<bean lazy-init=""/>延缓实例化 可以使用<beans default-lazy-init=""/>延缓所有组件实例化 b.销毁时机:单例对象容器close()负责销毁 c.创建对象的模式(使用范围):scope控制,可以使用singleton和...
资源分类:Python库 所属语言:Python 资源全名:i_lazy-0.1.1-py3-none-any.whl 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059
可以使用<bean lazy-init=""/>延缓实例化 可以使用<beans default-lazy-init=""/>延缓所有组件实例化 b.销毁时机:单例对象容器close()负责销毁 c.创建对象的模式(使用范围):scope控制,可以使用singleton和...
可以使用<bean lazy-init=""/>延缓实例化 可以使用<beans default-lazy-init=""/>延缓所有组件实例化 b.销毁时机:单例对象容器close()负责销毁 c.创建对象的模式(使用范围):scope控制,可以使用singleton和...
:bento_box: 打字稿-nuxtjs-样板 typescript-nuxtjs-boilerplateはNuxt.jsでTypeScriptを使うためのボイラーになりますートになります :squid: 网址范例: : :rescue_worker’s_helmet: 要求 ...
lazy_object_proxy-1.4.3-cp27-cp27m-win_amd64
lazy_object_proxy-1.6.0-cp38-cp38-win_amd64
基于lazy-snapping的图片分割代码
@r0b0t3d/react-native-lazy-component :check_mark_button: 您的屏幕加载缓慢 :check_mark_button: 您的屏幕有很多沉重的组件,但一次只使用一些 :backhand_index_pointing_down:安装yarn add @r0b0t3d/react-...
前端开源库-markdown-it-lazy-headersMarkdown it Lazy Headers,Lazy ATX Headers插件用于Markdown it