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. 16个值得个人站长做的广告联盟[转自cnzz]

    建站也有好多年了,也建了几个站,有些微波收入, 反复测试了挺多广告联盟, 下面介绍一下: 1.googleadsense联盟: 推荐指数:☆☆☆☆☆ Google广告联盟是现在信誉最好的广告提供商之一 ...

  2. rac 实例被莫名重启的案例分析

    详见原文博客链接地址: Rac 实例被莫名重启一列分析

  3. WebLogic(12C)——Server

    上篇博客介绍了Weblogic的安装.Domain的创建,以及怎样进入管理控制台. WebLogic Server安装教程 1.Server(server)概念 2,创建Server(server) ...

  4. JavaScript螺纹的问题和答案

    要求: JavaScript是单线程的,有任务队列.比方使用setTimeou(func,secs)来在secs毫秒后向任务队列加入func.可是,setTimeout后面跟一个死循环,那么死循环导致 ...

  5. c#关于委托和事件

    using System; using System.Collections.Generic; using System.Text; namespace Delegate {     // 热水器   ...

  6. MYSQL - php 使用 localhost 无法连接数据库

    php 使用 localhost 无法连接数据库,而使用127.0.0.1却能连接成功. 可能原因: 系统hosts文件未提供127.0.0.1到localhost的解析.解决方法(以win7系统为例 ...

  7. Hibernate MySQL 数据库 使用别名 报 Column * Not Found

    使用Hibernate 查询MySQL数据表的时候报 Column Not Found ,原因是MySQL的驱动不支持别名, 解决方案如下,在连接参数中加上 useOldAliasMetadataBe ...

  8. django1.6读书笔记一

    reporter是Article中的一个外键,我们可以有多篇文章指向同一个reporter,然后通过使用article_set.all()就可以返回其所有的headline了,也可以添加条件来筛选. ...

  9. docker学习笔记:修改无法启动的容器中的内容

    我们可能会碰到这样的一个问题,在容器执行过程中,修改了容器的内容(如配置文件信息),但因为修改出了问题.导致容器关闭后,无法启动. 这事需要重新修改配置文件. 正常情况下可以通过 docker exe ...

  10. 基于visual Studio2013解决C语言竞赛题之0513字符拷贝

     题目 解决代码及点评 /************************************************************************/ /* 13. 将字符数 ...