组件注册-@Scope-设置组件作用域

star2017 1年前 ⋅ 1571 阅读
package com.debuggg.test1.main3;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;

@Configuration
public class MainConfig {

    /**
     * 作者 ZYL
     * 功能描述 : @Scope注解可以传入以下4个参数
     * @see org.springframework.beans.factory.config.ConfigurableBeanFactory#SCOPE_PROTOTYPE
     * @see org.springframework.beans.factory.config.ConfigurableBeanFactory#SCOPE_SINGLETON
     * @see org.springframework.web.context.WebApplicationContext#SCOPE_REQUEST
     * @see org.springframework.web.context.WebApplicationContext#SCOPE_SESSION
     * 
     * 4个参数分别代表的意思:
     * 
     * prototype:多实例的:ioc容器启动并不会调用方法创建对象放在容器中,
     *              每次获取bean的时候才会调用方法创建对象
     * singleton:单实例的(default):ioc容器启动会调用方法创建对象放到ioc容器中,以后每次获取就是直接从容器(map.get())中拿,
     * request:同一次请求创建一个实例 (不常用)
     * session:同一个session创建一个实例(不常用)
     * 日期 2020-04-08 12:17
     * 参数
     * 返回值 com.debuggg.test1.main3.Person
     */
    @Scope("prototype")
    @Bean
    public Person person(){
        return new Person("张三",18);
    }
}
更多内容请访问:IT源点

相关文章推荐

全部评论: 0

    我有话说: