【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提倡”最少 ...
随机推荐
- Mariadb 10.1 joiner节点加入报错WSREP: Failed to prepare for incremental state transfer
Mariadb 10.1 galera cluster 集群joiner 节点加入集群 会出现这种报错,导致mysql一直点点点,这里我贴出报错.2016年04月19日13:34:58 2016-04 ...
- jquery-validation 使用
jquery-validation 使用 一.用前必备 官方网站:http://bassistance.de/jquery-plugins/jquery-plugin-validation/ API: ...
- MySQL配置文件改变了datadir值
从Noinstall Zip Archive中安装MySQL正在从Noinstall软件包安装MySQL的用户可以使用这个说明来手动安装MySQL.从Zip archive 中安装MySQL的 步骤如 ...
- PHP 生成随机字符串与唯一字符串
说明:生成随机字符串用到的方法有 mt_rand() 生成唯一字符串用到的方法有 md5(),uniqid(),microtime() 代码: <?php /* * 生成随机字符串 * @par ...
- C#记录程序耗时的方法
用于准确测量运行时间的方法类: System.Diagnostics.Stopwatch 具体使用方法: using System.Diagnostics; Stopwatch stopwatch = ...
- ElasticSearch集群未连接 无法发现节点(windows环境)以及windows环境下设置服务 不能自动启动的问题
1.无法发现节点的错误: 试验了很多情况,但是总是无法加入集群,后来尝试了一下步骤,问题解决: 1.删除所有数据,重启:无效: 2.统一配置,全部重启,无效: 3.关闭所有防火墙,全部重启,无效: … ...
- C# jquery webservices 跨域调用的问题解决方案
前台代码: <script src="js/jquery-1.9.1.min.js" type="text/javascript"></scr ...
- Unit04 - 继承的意义(下) 、 访问控制 、 static和final
Unit04 - 继承的意义(下) . 访问控制 . static和final 1.方法的重写(Override):重新写.覆盖 1)发生在父子类中,方法名称相同,参数列表相同,方法体不同 2 ...
- 委托(delegate)的三种调用方式:同步调用,异步调用,异步回调(转载)
下面为即将被调用的方法: public delegate int AddHandler(int a,int b); public class 加法类 { public static int Add(i ...
- Gradle Cheat Sheet
加快编译速度 使用 gradle 2.4 及以上版本 ~/.gradle/gradle.properties 加入如下配置 org.gradle.daemon=true org.gradle.jvma ...