spring(4)_ bean属性 scope:作用域和lazy-init
2013-04-25 17:17
393 查看
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" -->说明:容器已经加载当使用到该对象的时候,实例化该对象
根据上篇博客进行补充讲解:/article/1565644.html
HelloDaoImpl.java
HelloServiceImpl.java
spring_service.java
HelloTest.java
HelloTest.java
spring_service.java
HelloTest.java
情况3:scope=singleton,lazy-init=true:
spring_dao.java
spring_service.java
HelloTest.java
情况4:两个配置文件一方属性为scope=singleton,lazy-init=false/default:
spring_dao.java
spring_service.java
HelloTest.java
lazy-init:default=false ,false ,true 默认为default
false:不延迟初始化
lazy-init结合scope=singleton使用
scope="singleton" lazy-init="default" -->说明:容器已经加载就实例化对象
scope="singleton" lazy-init="true" -->说明:容器已经加载当使用到该对象的时候,实例化该对象
根据上篇博客进行补充讲解:/article/1565644.html
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(4)_ bean属性 scope:作用域和lazy-init
- Spring中bean作用域属性scope
- Spring_2_Spring中lazy-init和scope属性
- Spring - bean的lazy-init属性(懒加载)
- Spring的Bean作用域(配置javabean时设置scope属性为以下相应值)
- Spring的lazy-init属性延迟初始化Bean
- Spring_2_Spring中lazy-init和scope属性
- Spring:Bean的别名(name)和作用域(scope属性)
- Spring中bean的作用范围:Scope属性的值
- Spring入门之创建Bean的时间以及lazy-init属性
- 编程方式实现SpringBean LazyInit
- [Java] Spring 3.0 /id.vs.name/简单属性的注入/bean中的scope属性/集合注入/自动装配/生命周期/
- Spring-bean作用域scope详解
- spring两个重要属性lazy-init和abstract
- spring中bean属性scope介绍
- spring中bean属性scope介绍
- [Java] Spring 3.0 /id.vs.name/简单属性的注入/bean中的scope属性/集合注入/自动装配/生命周期/
- Spring的bean标签的scope和parent属性
- spring beans标签的default-lazy-init属性
- Spring配置中 bean上的属性parent的作用