【Spring】初始化Spring IoC容器(非Web应用),并获取Bean
参考文章
Introduction to the Spring IoC container and beans
BeanFactory 和ApplicationContext(Bean工厂和应用上下文)
Spring ApplicationContext - Resource leak: 'context' is never closed
版本说明
- JDK 1.7
- Spring3.2.14
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>3.2.14.RELEASE</version>
</dependency>
Java项目中,启动Spring IoC、获取Bean
CLASS PATH下放一个最简单的Spring配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <bean id="demoBean" class="com.nicchagil.springapplication.No001initSpringIocAndGetABean.bean.DemoBean">
</bean> </beans>
需要由Spring托管的Bean Class
package com.nicchagil.springapplication.No001initSpringIocAndGetABean.bean;
public class DemoBean {
public String test() {
return "hello spring...";
}
}
调用类
package com.nicchagil.springapplication.No001initSpringIocAndGetABean; import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import com.nicchagil.springapplication.No001initSpringIocAndGetABean.bean.DemoBean; public class Call { public static void main(String[] args) {
ApplicationContext context =
new ClassPathXmlApplicationContext("spring.xml");
DemoBean d1 = context.getBean("demoBean", DemoBean.class);
DemoBean d2 = context.getBean("demoBean", DemoBean.class); /* execu the method*/
System.out.println(d1.test());
System.out.println(d2.test()); /* default singleton */
System.out.println("demoBean isSingleton ? -> " + context.isSingleton("demoBean"));
System.out.println(d1);
System.out.println(d2);
System.out.println("d1 == d2 -> " + (d1 == d2));
} }
日志
hello spring...
hello spring...
demoBean isSingleton ? -> true
com.nicchagil.springapplication.No001initSpringIocAndGetABean.bean.DemoBean@4857b188
com.nicchagil.springapplication.No001initSpringIocAndGetABean.bean.DemoBean@4857b188
d1 == d2 -> true
【Spring】初始化Spring IoC容器(非Web应用),并获取Bean的更多相关文章
- spring源码研究之IoC容器在web容器中初始化过程
转载自 http://ljbal.iteye.com/blog/497314 前段时间在公司做了一个项目,项目用了spring框架实现,WEB容器是Tomct 5,虽然说把项目做完了,但是一直对spr ...
- Spring.NET的IoC容器(The IoC container)——简介(Introduction)
简介 这个章节介绍了Spring Framework的控制反转(Inversion of Control ,IoC)的实现原理. Spring.Core 程序集是Spring.NET的 IoC 容器实 ...
- Spring框架学习[IoC容器高级特性]
1.通过前面4篇文章对Spring IoC容器的源码分析,我们已经基本上了解了Spring IoC容器对Bean定义资源的定位.读入和解析过程,同时也清楚了当用户通过getBean方法向IoC容器获取 ...
- IOC容器在web容器中初始化——(一)两种配置方式
参考文章http://blog.csdn.net/liuganggao/article/details/44083817,http://blog.csdn.net/u013185616/article ...
- Spring IOC 容器源码分析 - 创建原始 bean 对象
1. 简介 本篇文章是上一篇文章(创建单例 bean 的过程)的延续.在上一篇文章中,我们从战略层面上领略了doCreateBean方法的全过程.本篇文章,我们就从战术的层面上,详细分析doCreat ...
- Spring基础——在 IOC 容器中 Bean 之间的关系
一.在 Spring IOC 容器中 Bean 之间存在继承和依赖关系. 需要注意的是,这个继承和依赖指的是 bean 的配置之间的关系,而不是指实际意义上类与类之间的继承与依赖,它们不是一个概念. ...
- 比Spring简单的IoC容器
比Spring简单的IoC容器 Spring 虽然比起EJB轻量了许多,但是因为它需要兼容许多不同的类库,导致现在Spring还是相当的庞大的,动不动就上40MB的jar包, 而且想要理解Spring ...
- 使用Spring.NET的IoC容器
使用Spring.NET的IoC容器 0. 辅助类库 using System; using System.Collections.Generic; using System.Linq; using ...
- Spring IOC容器在Web容器中是怎样启动的
前言 我们一般都知道怎样使用spring来开发web应用后,但对spring的内部实现机制通常不是很明白.这里从源码角度分析下Spring是怎样启动的.在讲spring启动之前,我们先来看看一个web ...
- Spring框架及IOC容器
Spring是一个非常活跃的开源框架, 它是一个基于IOC和AOP来构架多层JavaEE系统的框架,它的主要目地是简化企业开发.Spring以一种非侵入式的方式来管理你的代码, Spring提倡”最少 ...
随机推荐
- CSS,bootstrap表格控制当td内容过长时用省略号表示,以及在不使用bootstrap时过长也用省略号表示
首先需要在table中设置table-layout:fixed; <table style="table-layout:fixed"></table> 然后 ...
- 表单的enctype property
enctype 属性规定在发送到服务器之前应该如何对表单数据进行编码. 默认地,表单数据会编码为 "application/x-www-form-urlencoded".就是说,在 ...
- IP地址的分类与寻址
IP地址:有一种标识符,被TCP/IP协议簇的IP层用来标识 连接到因特网的设备.IP协议的第4版IPv4地址是32位地址,是连接地址,定义了每一个连接到因特网上的设备(可以认为是主机的别名),而不是 ...
- java final方法的作用
1. 不想让别人修改方法实现. 2. 在方法调用时使用内嵌调用. 3. 有效的“关闭”动态绑定,这样编译器就可以为final方法调用生成更有效的代码. Java编程思想: “然而,大多数情况下,这样做 ...
- matlab练习程序(SUSAN检测)
matlab练习程序(SUSAN检测) SUSAN算子既可以检测角点也可以检测边缘,不过角点似乎比不过harris,边缘似乎比不过Canny.不过思想还是有点意思的. 主要思想就是:首先做一个和原图像 ...
- Windows内核 字符串基本操作
驱动程序中字符串操作涉及到ASCII字符串.宽字符串,还有DDK定义的ANSI_STRING数据结构和UNICODE_STRING数据结构. 1)ASCII字符串和宽字符串 在应用程序中使用两种字符: ...
- smaller programs should improve performance
COMPUTER ORGANIZATION AND ARCHITECTURE DESIGNING FOR PERFORMANCE NINTH EDITION In this section, we l ...
- jquery实现nav的下拉
<script type="text/javascript"> $(function(){ //tab_flag 根据此标记判断是否由父级nav指向了nav_list ...
- LightOj 1213 - Fantasy of a Summation(推公式 快速幂)
题目链接:http://lightoj.com/volume_showproblem.php?problem=1213 #include <stdio.h> int cases, case ...
- raspberry pi
1. Expend System (sudo raspi-config) 2. Change keybaord layout to 104 key (US) 3. Change update sour ...