Spring中Bean的作用域和自动装配方式

Bean的作用域

Spring中bean的作用域共有singleton、prototype、request、session、application、websocket六种

Spring中Bean的作用域和自动装配方式_第1张图片

其中后四种都是用在Web应用程序中的,主要介绍前两种singleton(单例)和prototype(原型)

Bean的作用域范围为singleton时,所有实例共享一个对象。

Spring的默认配置为scope = “singleton”,以下两种配置的效果是一样的:

默认配置



    
    

scope = “singleton”



    
    

测试类及输出结果:

import indi.stitch.pojo.User;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MyTest {
    
    @Test
    public void test2() {
        ApplicationContext context = new ClassPathXmlApplicationContext("namespace.xml");
        User user = context.getBean("user", User.class);
        User user2 = context.getBean("user", User.class);
        System.out.println(user == user2);
    }
}

Spring中Bean的作用域和自动装配方式_第2张图片

scope = “prototype”



    
    

测试类及输出结果:

import indi.stitch.pojo.User;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MyTest {
    @Test
    public void test2() {
        ApplicationContext context = new ClassPathXmlApplicationContext("namespace.xml");
        User user = context.getBean("user", User.class);
        User user2 = context.getBean("user", User.class);
        System.out.println(user == user2);
    }
}

Spring中Bean的作用域和自动装配方式_第3张图片

Bean的自动装配

Spring中Bean的自动装配基于autowired标签实现

首先创建实体类People、Cat、Dog,People和Cat、Dog是组合关系,People中定义了依赖于Cat、Dog的属性

People实体类

package indi.stitch.pojo;
public class People {
    private Cat cat;
    private Dog dog;
    public Cat getCat() {
        return cat;
    }
    public void setCat(Cat cat) {
        this.cat = cat;
    }
    public Dog getDog() {
        return dog;
    }
    public void setDog(Dog dog) {
        this.dog = dog;
    }
    @Override
    public String toString() {
        return "People{" +
                "cat=" + cat +
                ", dog=" + dog +
                '}';
    }
}

Cat实体类

package indi.stitch.pojo;
public class Cat {
    public void shout() {
        System.out.println("miao~");
    }
}

Dog实体类

package indi.stitch.pojo;
public class Dog {
    public void shout() {
        System.out.println("wang~");
    }
}

通过name自动装配



    
    
    
    

测试类及输出结果:

import indi.stitch.pojo.People;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MyTest {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        People people = context.getBean("people", People.class);
        people.getCat().shout();
        people.getDog().shout();
    }
}

输出结果

Spring中Bean的作用域和自动装配方式_第4张图片

通过type自动装配



    
    
    
    

测试类和结果和上面相同

import indi.stitch.pojo.People;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MyTest {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        People people = context.getBean("people", People.class);
        people.getCat().shout();
        people.getDog().shout();
    }
}

输出结果

Spring中Bean的作用域和自动装配方式_第5张图片

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

你可能感兴趣的