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();}}

原创粉丝点击