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. jQuery遍历table

    1. $("table").find("tr").each(function(){ $(this).find("td").each(func ...

  2. windows简单杀死进程的批处理程序

    新建一个txt文档,命令为taskkill.bat,复制下面的命令保存 @echo offtaskkill /F /IM vm* /Ttaskkill /F /IM apple* /Ttaskkill ...

  3. 【Oracle】ORA-01722:无效数字(控制文件最后一个字段)

    原因: 每一行物理数据最后都存在一个换行符. 如果integer或者number类型的字段位于控制文件的最后,最后其实都会有CR/LF的换行符,在用sqlldr导入时会把换行符也算作那个数字的一部分, ...

  4. UVa401 Palindromes

    #include <stdio.h>#include <string.h> char mirror(char c){    static const char m[] = &q ...

  5. BZOJ 1270: [BeijingWc2008]雷涛的小猫( dp )

    简单的dp.. dp(i,j) = max(dp(x,y))+cnt[i][j], (x,y)->(i,j)是合法路径. 设f(i)= max(dp(x,y))(1≤x≤N, 1≤y≤i), g ...

  6. 界面控件 - 滚动条ScrollBar(对滚动条消息和鼠标消息结合讲的不错)

    界面是人机交互的门户,对产品至关重要.在界面开发中只有想不到没有做不到的,有好的想法,当然要尝试着做出来.对滚动条的扩展,现在有很多类是的例子. VS2015的代码编辑是非常强大的,其中有一个功能可以 ...

  7. docker学习笔记7:发布镜像到docker hub上

    镜像创建好后,很重要的一个操作就是共享和发布.可以将自己创建的镜像发布到docker hub上,也可以发布到自己的私有docker hub上. 要想发布镜像到dokcer hub上,首先要在dokce ...

  8. php三元运算

    $a = 2; $a == 1 ? $test="企业" : ($a==2 ? $test="地区" : $test="其他地方"); ec ...

  9. 十进制字符串转成二进制(decimal to binary)

    题目:给一个十进制的字符串例如1.25, 将其转化为二进制字符串,这个例子的结果是1.01 = 1*2^0 + 0*2^(-1) + 1*2^(-2) = 1.25. 如果不能完整的用二进制表示,输出 ...

  10. C++标准库类型vector及迭代器iterator简介

    Vector是C++标准库类型,称为容器,一个容器中的所有对象必须是同一种类型的.与数组相比,其最大的优点就是动态增长.Vector是一个类模板,并不是数据类型,而vector<int>和 ...