容器对象spring(4)_ bean属性 scope:作用域和lazyinit

最近笔者几篇文章介绍了改容器对象的文章. 关联文章的地址

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

}

文章结束给大家分享下程序员的一些笑话语录: 乔布斯:怎么样还是咱安全吧!黑客:你的浏览器支持国内网银吗?苹果可以玩国内的网游吗乔布斯:......不可以黑客:那我研究你的漏洞干嘛,我也需要买奶粉!

原文地址:https://www.cnblogs.com/xinyuyuanm/p/3043337.html