Spring源码解析(三)——组件注册3

@Scope设置组件作用域

import com.ken.domain.Person;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;

@Configuration
public class MainConfig {

	/**
	 * singleton: 定义该bean是单例模式,ioc容器启动会调用方法创建对象放到ioc容器中,以后每次获取直接从容器中
	 * prototype:每次调用都会创建一个新的bean实列。ioc容器启动并不会创建实例,而是在每次获取的时候去创建对象
	 * request:同一次请求创建一个实例
	 * session:同一个session创建一个实例
	 * @return
	 */
	@Scope("prototype")
	@Bean(value = "person")
	public Person person() {
		return new Person("lisi", 20);
	}
}

 

@Lazy懒加载。针对于singleton,@Lazy使得,ioc容器启动的时候,不去创建bean,而是,在使用到bean的时候再创建。

import com.ken.domain.Person;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;

@Configuration
public class MainConfig {
	@Lazy
	@Bean(value = "person")
	public Person person() {
		System.out.println("chuangjain");
		return new Person("lisi", 20);
	}
}

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值