spring(4)_ bean属性 scope:作用域和lazy-init
来源:互联网 发布:淘宝悬浮导航制作方法 编辑:程序博客网 时间:2024/06/05 22:49
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实例化");}@Overridepublic 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实例化");}@Overridepublic 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"?><beansxsi: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 {@Testpublic 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 {@Testpublic 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"?><beansxsi: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 {@Testpublic 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"?><beansxsi: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 {@Testpublic 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"?><beansxsi: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 {@Testpublic void test() {// 容器创建 实例化容器// 读取 classes 路径下面的文件 参数 动态参数、单个参数、数组 等ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml"); // HelloService helloService = context.getBean("helloServiceImpl", HelloService.class);// helloService.sayHello();}}
- spring(4)_ bean属性 scope:作用域和lazy-init
- Spring_2_Spring中lazy-init和scope属性
- spring Bean的生命周期和lazy-init
- spring lazy-init属性
- Spring的lazy-init属性延迟初始化Bean
- Spring Bean中的lazy-init
- spring bean scope属性
- spring bean属性scope
- Spring中bean的作用域scope
- spring之bean的作用域scope
- spring中bean的scope(作用域)
- Spring-bean作用域scope详解
- Spring-bean作用域scope详解
- Spring-bean作用域scope详解
- Spring-bean作用域scope详解
- Spring bean的作用域Scope
- Spring-bean作用域scope详解
- Spring bean的作用域(scope)
- 面向对象的5条基本设计原则
- 使用Jquery EasyUI Combobox 时的一个问题
- google test
- 电源管理之pmu驱动分析
- C# POST数据到指定url并返回结果页面内容
- spring(4)_ bean属性 scope:作用域和lazy-init
- Ubuntu12.04 下配置qtcreator交叉编译
- [Audio] Android 耳机切换
- 不使用JQuery的多行新闻滚动
- Spring AOP 配置
- 窗口句柄浅析和获取窗口句柄的方法
- [Python]GUI编程练习 -- 获取天气预报
- DOM节点的操作
- 反射原理获得bean的属性