Spring初学三(对spring作为容器的理解)

以前,一直不清楚spring容器的概念,现在终于理解了。

spring作为容器,其实最重要的就是配置文件xml。在里面创建好各种各样的bean,有属性注入的(注入字符串、list、map、set等集合)、函数注入、对象注入(将一个bean通过ref引用注入到)等,这一步只是创建好了bean,但是还没有放到容器中,要将其放到容器中才可以使用。容器有两种,分别是BeanFactory、上下文ApplicationContext。放入容器,就是将配置文件xml加载进来,具体加载方式如下表,这个时候并没有创建bean实例,在getBean时才真正创建了bean实例。

BeanFactory方式

主要是用XmlBeanFactory

加载方法有多种,分别是

org.springframework.core.io.ByteArrayResource字节给定的内容
org.springframework.core.io.ClassPathResource从classpath提取资源
org.springframework.core.io.DescriptiveResource资源描述符
org.springframework.core.io.FileSystemResource从文件系统提取资源
org.springframework.core.io.InputStreamResource从输入流提取资源
org.springframework.web.context.support.ServletContextResource从servlet上下文中取资源
org.springframework.core.io.UrlResource从给定的url中取资源
  

具体使用如下(以文件系统提取举例子):

 BeanFactory factory=new XmlBeanFactory(new FileSystemResource("c:/beans.xml"));

ApplicationContext方式

加载方法主要有三种,如下

org.springframework.context.support.ClassPathXmlApplicationContext从类路径中的xml文件载入bean资源
org.springframework.context.support.FileSystemXmlApplicationContext从文件系统中的xml文件载入bean资源
org.springframework.web.context.WebApplicationContext从web系统中的xml中载入Bean资源
具体使用如下(以类路径加载举例子)

 ApplicationContext ctx = new ClassPathXmlApplicationContext( "com/springinaction/springidol/spring-idol.xml");
   

下面通过几个例子来加强下理解

1.配置bean(各种注入如下)

 <bean id="baseSaxophonist"
      class="com.springinaction.springidol.Instrumentalist"
      abstract="true">
    <property name="song" value="Jingle Bells" />                                       //注入字符串
    <property name="instrument" ref="saxophone" />                                  //注入对象

  </bean>


<bean id="hank" class="com.springinaction.springidol.OneManBand">
    <property name="instruments">
      <!--
      <list>                                                                                                           //注入list
          <ref bean="guitar" />
          <ref bean="saxophone" />
          <ref bean="cymbal" />
          <ref bean="cymbal" />
      </list>
      -->
      <!--
      <props>                                                                                                       //注入props
        <prop key="GUITAR">STRUM STRUM STRUM</prop>
        <prop key="CYMBAL">CRASH CRASH CRASH</prop>
        <prop key="HARMONICA">HUM HUM HUM</prop>
      </props>
      -->
      <map>                                                                                                        //注入map
        <entry key="GUITAR" value="STRUM STRUM STRUM" />
      </map>
    </property>
  </bean>

<bean id="duke"
      class="com.springinaction.springidol.PoeticJuggler"
      autowire="constructor">                                                                        //自动注入,byname、bytype、by构造函数constructor、autodetect
<!--     <constructor-arg ref="sonnet29" />  -->
  </bean>


2.将bean放到容器中

 ApplicationContext ctx = new ClassPathXmlApplicationContext(
        "com/springinaction/springidol/spring-idol.xml");

3.实例化bean

TalentCompetition competition =   (TalentCompetition) ctx.getBean("springIdol");

通过反射机制,可以使用bean对应class里面的方法。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值