1 public static void main(String[] args) {
 2         // 通过Java配置来实例化Spring容器
 3         AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class);  
 4         
 5         // 在Spring容器中获取Bean对象
 6         UserService userService = context.getBean(UserService.class);
 7         
 8         // 调用对象中的方法
 9         List<User> list = userService.queryUserList();
10         for (User user : list) {
11             System.out.println(user.getUsername() + ", " + user.getPassword() + ", " + user.getPassword());
12         }
13         
14         
15         // 销毁该容器
16         context.destroy();
17     }

由于应用上下文是一个ResourceLoader(即I/O操作),它消耗的资源需要在某个时候释放。

加上下面代码即可。

context.close();

 

posted on 2018-01-20 11:19  Time。时光  阅读(233)  评论(0编辑  收藏  举报