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 ...
随机推荐
- wiki oi3117 高精度练习之乘法
题目描述 Description 给出两个正整数A和B,计算A*B的值.保证A和B的位数不超过500位. 输入描述 Input Description 读入两个用空格隔开的正整数 输出描述 Outpu ...
- Tour(KM算法)
Tour Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others) Total Submi ...
- Hive实现oracle的Minus函数
在Oracle中minus运算的主要功能是: 在进行两个表格或者两个查询结果的时候,返回在第一个表格/查询结果中与第二个表格/查询结果不同样的记录. 结果不同样的记录包括两种情况:A,B 表中某一行的 ...
- 素数环(C - 暴力求解)
素数环(暴力)(紫书194页) Description A ring is composed of n (even number) circles as shown in diagram. Put ...
- QT实现图片按钮(用qss切割图片,或者放三张图片)
我在网上找了很久,把他综合了一下 不说了关键代码来了:(这是一张图片切图的效果) void SetButtonStyle(QPushButton *button, QString imgsrc, in ...
- 全球在一个 level 上思考的价值观和想法是一样的(转)
近日,福布斯中文版总编辑周建工对话马云,谈到腾讯频繁的大笔收购,马云点评称腾讯收购的所有的案子,老百性都看得懂,这就错了.战略就像买股票一样,如果老太太都开始买股票了,一定有问题. 以下是对话内容,转 ...
- Eclipse下执行main函数报java.lang.NoClassDefFoundError的解决
今天执行eclipse下的一个java类,无论run还是debug,都报java.lang.NoClassDefFoundError.而且把main中函数都注释掉,执行还是报一样的错. 检查了一下这个 ...
- 关于apche无缘无故个启动不了,解决方法
1. 对于用户不小心把apache下的conf文件不小心给修改了,可那会导致,启动不了apache, 解决办法可以重新下载一个, 64为 32位 下载地址 http://www.veryhuo.c ...
- 一天一个类--ArrayList之一
今天开始打算将JDK7种的一些类的源码分析一下,笔者认为了解源码就是了解其实现过程,这是非常重要的,而不是简单的记住方法的使用,关键是了解其思想和目的这才是重要的.所以笔者决定首先将从一些容器下手.[ ...
- ZOJ 3492 模拟循环链表线性查找
WA了好几次最后找到错因是因为数组开小了! = = string whose length never exceeds 20 所以至少要开到21 = = ,我却一直开20 ╮(╯▽╰)╭ AC代码: ...