Spring3.0 入门进阶(1):从配置文件装载Bean
Spring 已经盛行多年,目前已经处于3.0阶段,关于Spring的概念介绍性的东西网上已经很多,本系列博客主要是把一些知识点通过代码的方式总结起来,以便查阅.
作为入门,本篇主要介绍Bean的加载
工具类
package com.eric.introduce; import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import com.eric.introduce.di.ConferenceRoom;
import com.eric.introduce.di.DummyConfig;
import com.eric.introduce.di.Knight;
import com.eric.introduce.di.Performer;
import com.eric.introduce.di.RescueDamselQuest; /**
* @author Eric
*
*/
public class IntroduceUtil { private static final String CONFIG_FILE = "com/eric/introduce/introduce.xml";
private static ApplicationContext context = new ClassPathXmlApplicationContext(
CONFIG_FILE); /**
* 通过ClassPathXmlApplication获得Bean实例
*/
public static void demonstrateDIByClassContent() { Knight knight = (Knight) context.getBean("knight");
knight.embarkOnQuest();
} /**
* 默认情况下Context总是返回同一个对象
*/
public static void demonstrateSingleStonClassContent() {
Knight knight1 = (Knight) context.getBean("knight");
Knight knight2 = (Knight) context.getBean("knight");
System.out.println(knight1);
System.out.println(knight2); } /**
* Spring中的singleSton只是正对一个context而言,不同的上下文会有不同的实例
*/
public static void demonstrateSingleStonInContent() {
ApplicationContext anotherContext = new ClassPathXmlApplicationContext(
CONFIG_FILE);
Knight knight1 = (Knight) context.getBean("knight");
Knight knight2 = (Knight) anotherContext.getBean("knight");
System.out.println(knight1);
System.out.println(knight2); } /**
* 通过factoryMethod 加载Bean
*
**/
public static void demonstrateLoadBeanByFactory() {
DummyConfig dummyConfig = (DummyConfig) context.getBean("config");
System.out.println(dummyConfig.getConfigurationMap());
} /**
* 如果通过factoryMethod的方式加载Bean,即使是不同的Context,任然返回的是同一个Bean实例
*
**/
public static void demonstrateLoadBeanByFactory2() {
ApplicationContext anotherContext = new ClassPathXmlApplicationContext(
CONFIG_FILE);
DummyConfig dummyConfig = (DummyConfig) context.getBean("config");
DummyConfig dummyConfig2 = (DummyConfig) anotherContext
.getBean("config");
System.out.println(dummyConfig);
System.out.println(dummyConfig2);
} /**
* 但Bean定义了Scope设置为Prototype后,每次会实例化不同的实例
*/
public static void demonstratePrototypeBean() {
RescueDamselQuest quest1 = (RescueDamselQuest) context
.getBean("prototypeQuest");
RescueDamselQuest quest2 = (RescueDamselQuest) context
.getBean("prototypeQuest");
System.out.println(quest1);
System.out.println(quest2); } /**
* 验证init以及destory方法
*
**/
public static void demonstrateInitAndDestory() {
ConferenceRoom conferenceRoom = (ConferenceRoom) context
.getBean("conferenceRoom");
conferenceRoom.use();
} /**
* 验证了注入集合的几种方法
*/
public static void demostrateInjectionPropertieWays() {
Performer performer = (Performer) context
.getBean("instrumentPerformer");
performer.performer();
performer.eatFruit();
performer.printProperties();
} }
配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"> <bean id="knight" class="com.eric.introduce.di.BraveKnight">
<constructor-arg ref="quest"></constructor-arg>
</bean>
<bean id="quest" class="com.eric.introduce.di.RescueDamselQuest" />
<bean id="minstrel" class="com.eric.introduce.aop.Minstrel" />
<bean id="config" class="com.eric.introduce.di.DummyConfig"
factory-method="getInstance" /> <bean id="prototypeQuest" class="com.eric.introduce.di.RescueDamselQuest"
scope="prototype" /> <bean id="conferenceRoom" class="com.eric.introduce.di.ConferenceRoom"
init-method="beforeUse" destroy-method="afterUse" lazy-init="true" /> <bean id="guitar" class="com.eric.introduce.di.Guitar" />
<bean id="apple" class="com.eric.introduce.di.Apple" />
<bean id="orange" class="com.eric.introduce.di.Orange" /> <bean id="instrumentPerformer" class="com.eric.introduce.di.InstrumentPerformer">
<property name="name" value="Eric" />
<property name="age" value="26" />
<property name="instrument" ref="guitar" />
<property name="privateInstrument">
<bean class="com.eric.introduce.di.Piano"></bean>
</property>
<property name="favFruit">
<list>
<ref bean="apple" />
<ref bean="orange" />
</list>
</property>
<property name="properties">
<props>
<prop key="SPORT">FOOTBALL</prop>
<prop key="CITY">WUHAN</prop>
</props>
</property>
</bean>
<aop:config>
<aop:aspect ref="minstrel">
<aop:pointcut id="embark" expression="execution(* *.embarkOnQuest(..))" />
<aop:before pointcut-ref="embark" method="singleBeforeQuest"></aop:before>
<aop:after pointcut-ref="embark" method="singleAfterQuest" />
</aop:aspect>
</aop:config>
</beans>
由于功能都比较简单,通过调用方法以及配置文件应该就可以看出用法,所以只列出了部分文件.
如果需要完整事例,请下载附件
Spring3.0 入门进阶(1):从配置文件装载Bean的更多相关文章
- Spring3.0 入门进阶(三):基于XML方式的AOP使用
AOP是一个比较通用的概念,主要关注的内容用一句话来说就是"如何使用一个对象代理另外一个对象",不同的框架会有不同的实现,Aspectj 是在编译期就绑定了代理对象与被代理对象的关 ...
- Cxf + Spring3.0 入门开发WebService
转自原文地址:http://sunny.blog.51cto.com/182601/625540/ 由于公司业务需求, 需要使用WebService技术对外提供服务,以前没有做过类似的项目,在网上搜寻 ...
- javascript入门进阶(一)
javascript 入门进阶 这里主要讲解一下在入门阶段很难注意的一些知识点,不一定有用.但是会了总比不会强. 1.HTML为<script>标签准备的6个属性: -async:可选.表 ...
- 模块化之Spring3.0 web fragment和gradle构建项目
1.背景 模块化开发很久以前就开始普及的概念.但是到了企业实际情况中,真正把模块化作为系统架构的核心的不多.或者说对模块化有这个意识,但是具体到底该如何实现,有些模糊,同时也许因为项目紧.任务中. ...
- spring3.0使用annotation完全代替XML
@Service与@Component有什么不同?那天被问到这个问题,一时之间却想不起来,就利用这篇文章来纪录spring3.0中常用的annotation. 从spring2.5开始,annotat ...
- Spring3.0 与 MyBatis框架 整合小实例
本文将在Eclipse开发环境下,采用Spring MVC + Spring + MyBatis + Maven + Log4J 框架搭建一个Java web 项目. 1. 环境准备: 1.1 创建数 ...
- ASP.NET Core 1.0 入门——Application Startup
var appInsights=window.appInsights||function(config){ function r(config){t[config]=function(){var i= ...
- Omnet++ 4.0 入门实例教程
http://blog.sina.com.cn/s/blog_8a2bb17d01018npf.html 在网上找到的一个讲解omnet++的实例, 是4.0下面实现的. 我在4.2上试了试,可以用. ...
- Jbpm4.4+hibernate3.5.4+spring3.0.4+struts2.1.8整合例子(附完整的请假流程例子,jbpm基础,常见问题解决)
Jbpm4.4+hibernate3.5.4+spring3.0.4+struts2.1.8 整合例子(附完整的请假流程例子). 1.jbpm4.4 测试环境搭建 2.Jbpm4.4+hibernat ...
随机推荐
- nginx日志每日定时写入Hdfs
#!/bin/bash hadoop_home=/opt/hadoop-2.4.0 tw_nginx_log_file=/home/chiline.com.all/access_com_tw.log ...
- 误mlogc.c:32:23: error: curl/curl.h: No such file or directory
出现以下错误: mlogc.c:32:23: error: curl/curl.h: No such file or directory mlogc.c:1091: error: expected ' ...
- redis研究记录
1 redis安装 wget http://download.redis.io/redis-stable.tar.gz tar xvzf redis-stable.tar.gz cd redis-st ...
- AsyncQueryHandler处理数据
参考:http://blog.csdn.net/hfreeman2011/article/details/8555474和http://blog.csdn.net/dragondog/article/ ...
- 【QT相关】Image Viewer Example
结合QLable和QScrollArea显示一个图片. QLable典型用于用户展示文本,但是也能展示图片.QScrollArea提供了一个滚动视图,如果子控件超过了框架限制,QScrollArea自 ...
- C++ cout 如何保留小数输出
参考 : http://upliu.net/how-cout-out-2-precision.html 大家都知道用 C 语言中 printf () 函数可以非常方便控制保留 几位小数输出 不过在 C ...
- Codeforces Round #253 (Div. 2), problem: (B)【字符串匹配】
简易字符串匹配,题意不难 #include <stdio.h> #include <string.h> #include <math.h> #include < ...
- Base64编码的java实现
Java本身是提供了Base64编码的工具包的,做项目的时候自己实现了个,在这里记录一下: /** Base64编码数组 */ private static final String base64En ...
- Spring+EhCache缓存实例(详细讲解+源码下载)(转)
一.ehcahe的介绍 EhCache 是一个纯Java的进程内缓存框架,具有快速.精干等特点,是Hibernate中默认的CacheProvider.Ehcache是一种广泛使用的开源Java分布式 ...
- [置顶] Android开发之XML文件的解析
Android系统开发之XML文件的解析 我们知道Http在网络传输中的数据组织方式有三种分别为:XML方式.HTML方式.JSON方式.其中XML为可扩展标记语言,如下: <?xml vers ...