读者福利
分享一份自己整理好的Java面试手册,还有一些面试题pdf
不要停下自己学习的脚步
@Service
@Scope
public class OrderService {
public void sayMyName() {
System.out.println("MyName: " + this);
}
}
public static void main(String[] args) {
SpringUtils.getSpringContext();
SpringUtils.getBeansByClassType(OrderService.class).sayMyName();
SpringUtils.getBeansByClassType(OrderService.class).sayMyName();
SpringUtils.getBeansByClassType(OrderService.class).sayMyName();
}
debug日志:
DEBUG 2015-01-14 11:13:05,622 org.springframework.beans.factory.support.AbstractBeanFactory: Returning cached instance of singleton bean ‘orderService’
MyName: com.honzh.socket.server.business.test.OrderService@152bdc7
DEBUG 2015-01-14 11:13:05,622 org.springframework.beans.factory.support.AbstractBeanFactory: Returning cached instance of singleton bean ‘orderService’
MyName: com.honzh.socket.server.business.test.OrderService@152bdc7
DEBUG 2015-01-14 11:13:05,622 org.springframework.beans.factory.support.AbstractBeanFactory: Returning cached instance of singleton bean ‘orderService’
MyName: com.honzh.socket.server.business.test.OrderService@152bdc7
结论:service对象在单例情况下只有1个。
当参数为多例时:
@Service
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public class OrderService {
public void sayMyName() {
System.out.println("MyName: " + this);
}
}
public static void main(String[] args) {
SpringUtils.getSpringContext();
SpringUtils.getBeansByClassType(OrderService.class).sayMyName();
SpringUtils.getBeansByClassType(OrderService.class).sayMyName();
SpringUtils.getBeansByClassType(OrderService.class).sayMyName();
}
DEBUG 2015-01-14 11:15:30,648 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory: Finished creating instance of bean ‘orderService’
MyName: com.honzh.socket.server.business.test.OrderService@cab854
DEBUG 2015-01-14 11:15:30,648 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory: Creating instance of bean ‘orderService’
DEBUG 2015-01-14 11:15:30,648 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory: Finished creating instance of bean ‘orderService’
MyName: com.honzh.socket.server.business.test.OrderService@10bbd42
DEBUG 2015-01-14 11:15:30,648 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory: Creating instance of bean ‘orderService’
DEBUG 2015-01-14 11:15:30,648 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory: Finished creating instance of bean ‘orderService’
MyName: com.honzh.socket.server.business.test.OrderService@1b6772a
结论:prototype状态下,service在获取时会重新加载一个新的对象。
附上SpringUtils类,该类是为了通过applicationContext文件,加载对应的spring组件。
private static ApplicationContext factory;
最后
码字不易,觉得有帮助的可以帮忙点个赞,让更多有需要的人看到
又是一年求职季,在这里,我为各位准备了一套Java程序员精选高频面试笔试真题,来帮助大家攻下BAT的offer,题目范围从初级的Java基础到高级的分布式架构等等一系列的面试题和答案,用于给大家作为参考
以下是部分内容截图
f00ff254613a03fab5e56a57acb)收录**