proxyBeanMethods默认为true,表示cglib会为@Configuration生成一个代理类,方法之间调用会从IOC容器去获取实例对象。因此而在list1中调用bashMap方法时,会通过代理方法从IOC容器中去获取,这样就是单例的。运行的时候,控制台只打印了一次“bashMap调用”就证明了这一点
但是如果将proxyBeanMethods设为false,则表示不生成代理,那么list1中调用bashMap,会再生成一个对象而不是从IOC容器中获取,这样能提高性能,也造成了多例。
public class ProxyBeanMethodConfig {
@Bean
public Map<String, Object> bashMap() {
System.out.println("bashMap 调用了");
Map<String, Object> resultMap = new HashMap<>();
resultMap.put("key", "this is bashMap");
return resultMap;
}
@Bean
public List<String> list1() {
System.out.println("list1 调用了");
Map<String, Object> map = bashMap();
List<String> mapList = map.keySet().stream().map(row -> map.get(row).toString()).collect(Collectors.toList());
List<String> resultList = new ArrayList<>();
resultList.add("this is list1");
resultList.addAll(mapList);
return resultList;
}
@Bean
public List<String> list2() {
System.out.println("list2 调用了");
Map<String, Object> map = bashMap();
List<String> mapList = map.keySet().stream().map(row -> map.get(row).toString()).collect(Collectors.toList());
List<String> resultList = new ArrayList<>();
resultList.add("this is list2");
resultList.addAll(mapList);
return resultList;
}
}
@Configuration(proxyBeanMethods = true)
@Configuration(proxyBeanMethods = false)