【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提倡”最少 ...
随机推荐
- PropertiesUtil.java
package com.vcredit.ddcash.batch.util; import java.io.BufferedReader;import java.io.File;import java ...
- 数据库---MySQL常用函数总结
一.数学函数 数学函数主要用于处理数字,包括整型.浮点数等. ABS(x) 返回x的绝对值 SELECT ABS(-1) -- 返回1 CEIL(x),CEILING(x) 返回大于或等于 ...
- IOS第13天(2,私人通讯录,plist存储,偏好设置,归档)
***************plist存储 // 当点点击保存的时候调用 //保存 - (IBAction)save:(id)sender { // 获取沙盒的根路径 // NSString *ho ...
- 关于 calloc 函数使用 与fun 函数
s=(float *) calloc (1,sizeof(float)); #include "stdio.h"#include "stdlib.h"void ...
- HDU 1160 FatMouse's Speed(要记录路径的二维LIS)
FatMouse's Speed Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- jquery 同id使用指针寻找具体每个id的值
this来判断<script language="javascript"> function t(obj) { alert(obj.value); } </scr ...
- Centos 6.5升级到Git2.1.2
安装需求 # yum install curl-devel expat-devel gettext-devel openssl-devel zlib-devel# yum install gcc pe ...
- 滚轮事件js
<!DOCTYPE html> <html> <head> <title>jane</title> <style>div ...
- tableView主从表在storyboard连线是 Selcetion Segue和Accessory Action的区别
当按住Ctorl这样连线时会出现,当选择Selection Segue下面的push时,点击cell的任何位置都会跳转到下一个tableView,当选择Accessory Action的下面的push ...
- MySql超新手入门
https://www.kancloud.cn/thinkphp/mysql-tutorial/36457