学习spring依赖注入--备忘

1.需要导入的JAR包

      ../dist/spring.jar
      ../lib/jakarta-commons/commons-logging.jar
      ../lib/log4j/log4j-1.2.14.jar

2.写配置文件

       applicationContext.xml   //可参考samples/jpetstore/war/WEB-INF/applicationContext.xml

       log4j.properties

3.构造函数注入

       <constructor-arg ref="#"/> //#要注入的bean的name

4.setter方法注入

       <property name="*" ref="#">  //*为setter方法的参数名

5.int,String,List,Set,Map的注入

       <property name="intname" value="11"/>  //要注入的参数的name
       <property name="Stringnm" value="www"/>

       <property name="listname">
              <list>
                       <value>list1</value>
                       <value>list2</value>
               </list>
       </property>

       <property name="mapname">
               <map>
                      <entry key="1" value="map"/>
                      <entry key="2" value="map2"/>
               </map>
       </property>

6.Date值的注入,属性编辑器

        a.新建类DateEditor,继承propertyEditorSupport

        b.重写方法setAsText(String arg0)

        c.写配置文件

 

      备忘:<bean id="customEditorConfigurer" class="org.springframework.beans.factory.config.CustomEditorConfigurer">
  <property name="customEditors">
   <map>
    <entry key="java.util.Date">
     <bean class="com.ht.spring.DateEditor">
      <property name="format" value="yyyy-MM-dd"/>   //setter方法注入日期格式
     </bean>
    </entry>
   </map>
  </property>
 </bean>

 

7.使用beanfactory

BeanFactory factory = new ClassPathXmlApplicationContext("applicationContext-*.xml");

Object o = factory.getBean("beanname");

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值