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的更多相关文章

  1. Spring3.0 入门进阶(三):基于XML方式的AOP使用

    AOP是一个比较通用的概念,主要关注的内容用一句话来说就是"如何使用一个对象代理另外一个对象",不同的框架会有不同的实现,Aspectj 是在编译期就绑定了代理对象与被代理对象的关 ...

  2. Cxf + Spring3.0 入门开发WebService

    转自原文地址:http://sunny.blog.51cto.com/182601/625540/ 由于公司业务需求, 需要使用WebService技术对外提供服务,以前没有做过类似的项目,在网上搜寻 ...

  3. javascript入门进阶(一)

    javascript 入门进阶 这里主要讲解一下在入门阶段很难注意的一些知识点,不一定有用.但是会了总比不会强. 1.HTML为<script>标签准备的6个属性: -async:可选.表 ...

  4. 模块化之Spring3.0 web fragment和gradle构建项目

      1.背景 模块化开发很久以前就开始普及的概念.但是到了企业实际情况中,真正把模块化作为系统架构的核心的不多.或者说对模块化有这个意识,但是具体到底该如何实现,有些模糊,同时也许因为项目紧.任务中. ...

  5. spring3.0使用annotation完全代替XML

    @Service与@Component有什么不同?那天被问到这个问题,一时之间却想不起来,就利用这篇文章来纪录spring3.0中常用的annotation. 从spring2.5开始,annotat ...

  6. Spring3.0 与 MyBatis框架 整合小实例

    本文将在Eclipse开发环境下,采用Spring MVC + Spring + MyBatis + Maven + Log4J 框架搭建一个Java web 项目. 1. 环境准备: 1.1 创建数 ...

  7. ASP.NET Core 1.0 入门——Application Startup

    var appInsights=window.appInsights||function(config){ function r(config){t[config]=function(){var i= ...

  8. Omnet++ 4.0 入门实例教程

    http://blog.sina.com.cn/s/blog_8a2bb17d01018npf.html 在网上找到的一个讲解omnet++的实例, 是4.0下面实现的. 我在4.2上试了试,可以用. ...

  9. 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 ...

随机推荐

  1. Hadoop Security Authentication Terminology --Kerberos

    Hadoop Security Authentication Terminology --Kerberos What is kinit? Kinit -  obtain and cache Kerbe ...

  2. SWIFT学习笔记01

    1.Swift.用来推断option是不是nil,相当于OC的 if(option) if let name = option{ greeting = "if=====" }els ...

  3. css3属性——border-radius用法

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"/> <style> ...

  4. 欧拉函数K - Relatives

    欧拉函数是积性函数——若m,n互质,φ(mn)=φ(m)φ(n). 特殊性质:当n为奇数时,φ(2n)=φ(n), φ(x)=x(1-1/p1)(1-1/p2)(1-1/p3)(1-1/p4)…..( ...

  5. [Swust 549]--变位词(vector水过)

    Time limit(ms): 1000 Memory limit(kb): 65535   Description 输入N和一个要查找的字符串,以下有N个字符串,我们需要找出其中的所有待查找字符串的 ...

  6. 《算法导论》读书笔记之图论算法—Dijkstra 算法求最短路径

    自从打ACM以来也算是用Dijkstra算法来求最短路径了好久,现在就写一篇博客来介绍一下这个算法吧 :) Dijkstra(迪杰斯特拉)算法是典型的最短路径路由算法,用于计算一个节点到其他所有节点的 ...

  7. 基于Sql Server 2008的分布式数据库的实践(五)

    原文 基于Sql Server 2008的分布式数据库的实践(五) 程序设计 ------------------------------------------------------------- ...

  8. commoncrawl 源码库是用于 Hadoop 的自定义 InputFormat 配送实现

    commoncrawl 源码库是用于 Hadoop 的自定义 InputFormat 配送实现. Common Crawl 提供一个示例程序 BasicArcFileReaderSample.java ...

  9. 新买一款打印机hp5525N

    11900 RMB 彩色.激光.彩打. 让法国的工艺工程师给改成法语的了.

  10. Python中打印列表的序号和内容

    ==>the start 最近作业里要用到遍历打印出列表中的序号和内容,我刚开始用了个很笨的方法来写,后来老师说可以使用enumerate()函数,所以我就特意研究了下. 先看我之前用的笨方法: ...