spring4第6课-bean之间的关系+bean的作用范围

发布于:2025-06-06 ⋅ 阅读:(36) ⋅ 点赞:(0)

总结:

①bean之间的关系有  1,继承; 2,依赖; 3,引用;

②bean的作用范围(scope)

1,singleton Spring ioc 容器中仅有一个 Bean 实例,Bean 以单例的方式存在;(重点)
2,prototype 每次从容器中调用 Bean 时,都返回一个新的实例;(重点)
3,request 每次 HTTP 请求都会创建一个新的 Bean;
4,session 同一个 HTTP Session 共享一个 Bean;
5,global session 同一个全局 Session 共享一个 Bean,一般用于 Portlet 应用环境;(使用不多)
6,application 同一个 Application 共享一个 Bean;

详解如下:↓↓↓↓↓

①bean之间的关系有  1,继承; 2,依赖; 3,引用;

结合代码看比较好理解

<?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">
   
   <bean id="dog" class="com.java1234.entity.Dog">
      <property name="name" value="jack"></property>
   </bean>
   
   <bean id="abstractPeople" class="com.java1234.entity.People" abstract="true"> <!--abstract="true"表示是抽象类-->
      <!--抽象类中可以定义一些公共属性。都是高三5班的-->
      <property name="className" value="高三5班"></property> 
     <!--抽象类中可以定义一些公共属性,大部分人年龄都是19-->
      <property name="age" value="19"></property>     
   </bean>

   <!--开始定义具体的人, parent="abstractPeople" 表示继承关系zhangsan这个bean继承abstractPeople这个bean-->
<!-- depends-on="autority"表示依赖关系即在初始化zhangsan之前需要先初始化autority这个bean-->
   <bean id="zhangsan" parent="abstractPeople" depends-on="autority">
      <property name="id" value="1"></property>
      <property name="name" value="张三"></property>
   </bean>
   
   <bean id="lisi" parent="abstractPeople">
      <property name="id" value="2"></property>
      <property name="name" value="李四"></property>
      <property name="age" value="20"></property>  <!--李四的年龄和其他人不一样,age的值可以重写为20-->
      <property name="dog" ref="dog"></property>  <!-- ref="dog" 表示引用关系dog这个属性引用dog这个bean,前面学过,不重复了 -->
   </bean>
    <!-- 定义一个权限类Authority-->
   <bean id="autority" class="com.java1234.service.Authority"></bean>
</beans>

②bean的作用范围(scope)

1,singleton Spring ioc 容器中仅有一个 Bean 实例,Bean 以单例的方式存在;(重点)
2,prototype 每次从容器中调用 Bean 时,都返回一个新的实例;(重点)
3,request 每次 HTTP 请求都会创建一个新的 Bean;
4,session 同一个 HTTP Session 共享一个 Bean;
5,global session 同一个全局 Session 共享一个 Bean,一般用于 Portlet 应用环境;(使用不多)
6,application 同一个 Application 共享一个 Bean;

详解如下:↓↓↓↓↓

代码1:bean.xml  关键字是   scope="singleton"  spring的默认值是singleton

<?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">
	<!-- scope="singleton" 默认是singleton-->
	<bean id="dog" class="com.java1234.entity.Dog" scope="singleton">
		<property name="name" value="jack"></property>
	</bean>
</beans>

代码2: bean类

package com.java1234.entity;

public class Dog {
	private String name;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
		
}

代码3:测试类

package com.java1234.test;

import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.java1234.entity.Dog;

public class T {

	private ApplicationContext ac;

	@Before
	public void setUp() throws Exception {
		ac=new ClassPathXmlApplicationContext("beans.xml");
	}

	@Test
	public void test1() {
		Dog dog=(Dog)ac.getBean("dog");
		Dog dog2=(Dog)ac.getBean("dog");
		System.out.println(dog==dog2);
	}
	

}

运行结果: bean.xml内容不同,输出结果不同
scope="singleton" 单例模式时,每次都取1个同一个dog对象,所以两次的dog对象相同,输出true
scope="prototype" 多例模式时,每次都会取1个新的dog对象,所以两个dog对象不同,输出false


网站公告

今日签到

点亮在社区的每一天
去签到