学习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");

你可能感兴趣的:(spring,Web,bean,log4j,xml)