首页 > 代码库 > 峰Spring4学习(5)bean之间的关系和bean的作用范围

峰Spring4学习(5)bean之间的关系和bean的作用范围

一、bean之间的关系:

技术分享

1)继承:

People.java实体类:

技术分享
package com.cy.entity;


public class People {
    private int id;
    private String name;
    private int age;
    private String className;
    
    
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public String getClassName() {
        return className;
    }
    public void setClassName(String className) {
        this.className = className;
    }
    
    
    @Override
    public String toString() {
        return "People [id=" + id + ", name=" + name + ", age=" + age
                + ", className=" + className + "]";
    }
    
}
View Code

beans.xml配置文件:

<?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"
        default-autowire="byName">
    
    <bean id="abstractPeople" class="com.cy.entity.People" abstract="true">
        <property name="className" value="高三(1)班"></property>
        <property name="age" value="18"></property>
    </bean>
     
     <bean id="zhangsan" parent="abstractPeople">
         <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>
     </bean>
</beans>

测试代码:

技术分享
@Before
    public void setUp() throws Exception {
         ac=new ClassPathXmlApplicationContext("beans.xml");
    }
    
    @Test
    public void test() {
        People zhangsan = (People) ac.getBean("zhangsan");
        System.out.println(zhangsan); 
        
        People lisi = (People) ac.getBean("lisi");
        System.out.println(lisi); 
    }
View Code

技术分享

2)依赖:

spring中加载bean的方式默认是按照bean配置的顺序加载的;

例如,查看zhangsan之前,必须要有权限,要先获取权限;

People.java:

技术分享
package com.cy.entity;


public class People {
    private int id;
    private String name;
    private int age;
    private String className;
    
    public People(){
        System.out.println("People初始化..");
    }
    
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public String getClassName() {
        return className;
    }
    public void setClassName(String className) {
        this.className = className;
    }
    
    
    @Override
    public String toString() {
        return "People [id=" + id + ", name=" + name + ", age=" + age
                + ", className=" + className + "]";
    }
    
}
View Code

com.cy.service.Authority:

技术分享
package com.cy.service;

public class Authority {
    public Authority(){
        System.out.println("获取权限..");
    }
}
View Code

beans.xml配置:

<?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"
        default-autowire="byName">
    
    <bean id="abstractPeople" class="com.cy.entity.People" abstract="true">
        <property name="className" value="高三(1)班"></property>
        <property name="age" value="18"></property>
    </bean>
     
     <bean id="zhangsan" parent="abstractPeople">
         <property name="id" value="1"></property>
         <property name="name" value="张三"></property>
     </bean>
     
     <bean id="lisi" parent="abstractPeople" depends-on="authority">
         <property name="id" value="2"></property>
         <property name="name" value="李四"></property>
         <property name="age" value="20"></property>
     </bean>
     
     <bean id="authority" class="com.cy.service.Authority"></bean>
</beans>

测试代码:

技术分享
@Test
    public void test() {
        People zhangsan = (People) ac.getBean("zhangsan");
        System.out.println(zhangsan); 
        
        People lisi = (People) ac.getBean("lisi");
        System.out.println(lisi); 
    }
View Code

配置了depend on属性之后,加载lisi这个bean必须要先加载authority这个bean,所以bean的加载顺序变了,打印如下:

技术分享

3)引用:

就是这样子:

<bean id="lisi" parent="abstractPeople">
        <property name="id" value="2"></property>
        <property name="name" value="李四"></property>
        <property name="age" value="20"></property>
        <property name="dog" ref="dog"></property>
    </bean>

 

 

二、bean作用范围:

技术分享

默认是单例的:

<bean id="dog" class="com.java1234.entity.Dog" scope="singleton">
  <property name="name" value="http://www.mamicode.com/jack"></property>
</bean>

多例的配置:

<bean id="dog" class="com.java1234.entity.Dog" scope="prototype">
  <property name="name" value="http://www.mamicode.com/jack"></property>
</bean>

 

峰Spring4学习(5)bean之间的关系和bean的作用范围