spring配置类中调用有@Bean注解的方法时不是将该方法执行一遍,而是直接从ioc容器中获取bean
如下代码:
@Configuration
public class MainConfig {
@Bean
public Integer integer(){
return new Integer(10000);
}
public void f(){
// 此时的i应该是从ioc容器种获取,而不是执行一遍integer()方法获取了一个新的Integer对象。
// 因为spring对@Configuration注解的类有特别处理,使得调用将返回值放到容器的方法时直接从容器中获取
Integer i = integer();
System.out.println(i);
}
}