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>
由于功能都比较简单,通过调用方法以及配置文件应该就可以看出用法,所以只列出了部分文件.
如果需要完整事例,请下载附件