• 初生牛犊之spring(一)


       作为三大架构之一的spring,鄙人怀着无比激动的心情,学习了一下这神奇的架构,顺便总结一下今天学习的spring各知识点。

       1.spring基本配置文件:

     <!--Bean的配置文档-->

    <?xml version="1.0" encoding="UTF-8"?>

    <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd">

        <beans>

         <!-- id用来唯一标识Bean,class用来表示该Bean的来源-->

        <bean id="HelloWorld" class="cn.Test.Helloworld">     <!--或者这样 <bean name="HelloWorld" class="cn.Test.Helloworld"> 但id属性符合XML ID规定-->

          <!-- 将其word属性通过依赖注入值为Spring-->

             <property name="word">        

                 <value>Spring</value>   

             </property>  

         </bean>

     <beans>

    配置文件中的其他简单属性:

         1)singleton ,  non-singleton

    如  <bean id="HelloWorld" class="cn.Test.Helloworld" singleton="true" >  // 该属性规定客户端每次向BeanFactory请求时,只返回一个实例,即单例模式。spring默认为singleton模式

         <bean id="HelloWorld" class="cn.Test.Helloworld" non-singleton="true" >  //该模式下,对这个Bean的每次请求都会创建一个新的Bean实例

         2)可以在<property>中直接定义<bean>

    如:<property name="date">

               <bean id="date" class="java.util.Date">

          </property>

         3)对于null处理:有两种:

    如:1.<property name="date">

                <value>null</value>

           </property>

         2.<property name="date">

               <null/>

           </property>

        4)depends-on属性

    Beand depends-on属性可以用来在初始化使用这个Bean之前,强制执行一个或者多个Bean的初始化。

    如:<bean id="HelloWorld" class="cn.Spring.HelloTest.HelloWorld" depends-on="date">  <!--date为一个Bean-->

    2.依赖注入的3种实现方式:

      1)接口注入(通过java动态机制,不需要配置。省略)

      2)Set方法注入

            条件:java类当中提供setXxx方法。

            配置:如1.spring配置文件,就是实现set方法注入

      3)构造注入

    如: HelloWorld类

    public class HelloWorld{

       public String name=null;

      //添加构造方法  

        public Helloworld(String name){

              this.name=name;

        }

    }

    配置文件:

     <bean id="HelloWorld" class="cn.Test.Helloworld">

          <construtctor-arg index="0">  <!--construtctor用来表示是通过构造方式来注入参数的     index="0" 表示构造方法中的第一个参数,如果只有一个参数,则可以不用设置 -->

                <value>HelloWorld</value>

         </construtctor-arg>

    </bean>

  • 相关阅读:
    异步无刷新上传文件而且上传文件能够带上參数
    利用BADI WORKORDER_INFOSYSTEM在COOIS中加入自己定义列办事处
    Printf可变參数使用
    评大北农今日复牌公告
    iOS Sprite Kit教程之申请和下载证书
    UVa 12587 Reduce the Maintenance Cost(Tarjan + 二分 + DFS)
    Python: scikit-image Blob detection
    linux命令ps aux|grep xxx详解
    复制和重命名表格--修改表注释
    md5 破解网站
  • 原文地址:https://www.cnblogs.com/yaoxing92/p/2983244.html
Copyright © 2020-2023  润新知