Ⅱ.spring的點點滴滴--對象

承接上文

對象的各類實例化


.net篇(環境爲vs2012+Spring.Core.dll

修改原來的PersonDao對象爲 html

public class PersonDao : IPersonDao{
        public string name;
        private child chlid;
        public PersonDao(string Name){
            this.name = Name;
        }
        public PersonDao(string Name,child Child){
            this.name = Name;
            this.chlid = Child;
        }
        public void sayhello(){
            Console.WriteLine(this.name);
        }
        public class child { }
        public IPersonDao CreateInstance(string name) {
            return new PersonDao(name);
        }
        public static IPersonDao factoryInstance(string name) {
            return new PersonDao(name);
        }
    }

修改原來的app.config對象爲 java

<spring>
    <typeAliases>
       <alias name="PersonDaoAlias" type="SpringBase.PersonDao,SpringBase" />
    </typeAliases>
     <context name="test">       
      <resource uri="config://spring/objects" />
      <!--<resource uri="file://objects.xml" />-->
    </context>   
    <objects xmlns="http://www.springframework.net"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://www.springframework.net
        http://www.springframework.net/xsd/spring-objects.xsd" >      
      <object name="child" 
              type="SpringBase.PersonDao+child,SpringBase"></object>
      <object name="PersonDao" type="PersonDaoAlias"  singleton="false">
        <constructor-arg  value="hello" index="0" />
        <property name="name" value="aa"></property>
      </object>
      <object name="PersonchildDao" type="PersonDaoAlias" 
            singleton="true"
            lazy-init="true">
        <constructor-arg value="hello" index="0"  />
        <constructor-arg name="Child" ref="child" />
      </object>
      <object name="staticfactoryDao" type="PersonDaoAlias" 
          factory-method="factoryInstance">
        <constructor-arg  value="hello" index="0" />
      </object>
      <object name="factoryDao"  factory-method="CreateInstance"
          factory-object="PersonDao">
        <constructor-arg  value="hello" index="0" />
      </object>      
    </objects>
  </spring>
  1. 嵌套類型的對象實例化 在PersonDao中再添加一個類child(也就是內部類)
    <object name="child" type="SpringBase.PersonDao+child,SpringBase"></object>
    +號表示內部類
    ContextRegistry.GetContext("test").GetObject("child")
  2. 當對象是一個泛型的時候
    public class VarClass<T> { }
    <object name="var" 
     type="SpringBase.VarClass&lt;int>,SpringBase"></object>
    由於在xml文件裏面<符號是敏感符號因此用html的&lt;來代替, 工廠模式建立泛型也就是把方法上的泛型進行賦予類型a () , 那麼factory-methoda&lt;int,int> 當泛型爲內部類的時候,實例化失敗,未知緣由

java篇(環境爲Maven+Jdk1.7+IntelliJ IDEA 12.1.4

補充java的alias 當配置文件設置以下的時候, 在程序裏面能夠經過ctx.getBean("PersonDaoAlias");別名來實例化 spring

<beans>
    <alias name="PersonDao" alias="PersonDaoAlias" />
    <bean id="PersonDao"  class="springdemo.PersonDao"  singleton="false" ></bean>
</beans>

修改原來的PersonDao對象爲 app

package springdemo;
public class PersonDao implements IPersonDao {
    private String name;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public void sayhello() {
        System.out.println(this.name);
    }
    private child chlid;
    public PersonDao(String Name) {
        this.name = Name;
    }
    public PersonDao(String Name, child Child) {
        this.name = Name;
        this.chlid = Child;
    }
    public IPersonDao CreateInstance(String name) {
        return new PersonDao(name);
    }
    public static IPersonDao factoryInstance(String name) {
        return new PersonDao(name);
    }
}
class child  { }

修改原來的bean.xml對象爲 jvm

<beans>
    <alias name="PersonDao" alias="PersonDaoAlias" />
    <bean id="child" class="springdemo.child"/>
    <bean id="PersonDao" class="springdemo.PersonDao"  singleton="false">
        <constructor-arg  value="hello" index="0" />
        <property name="name" value="aa"/>
    </bean>
    <bean id="PersonchildDao" class="PersonDaoAlias"
            singleton="true"
            lazy-init="true">
        <constructor-arg value="hello" index="0"  />
        <constructor-arg index="1" ref="child" />
    </bean>
    <bean id="staticfactoryDao" class="springdemo.PersonDao"   lazy-init="false"
            factory-method="factoryInstance">
        <constructor-arg  value="hello" index="0" />
    </bean>
    <bean id="factoryDao"  factory-method="CreateInstance" factory-bean="PersonDao">
        <constructor-arg  value="hello" index="0" />
    </bean>
</beans>

1.不能使用泛型注入,泛型是在編譯期類型檢查用的因此jvm在運行期根本無論是否是generic, 對他來講編譯後的類文件和沒有泛型的沒區別.spring不支持泛型檢查 函數


javacsharp都有的共同點 this

  1. 當存在有參構造函數的時候,直接實例化會失敗,有2中方案
    1. 要麼再次聲明一個空的構造函數
    2. 要麼經過實例化參數來,由於默認的時候是用空構造的, name對應的是哪一個參數,value也就是值啦, 其中objects添加的屬性是爲了讓標籤能在vs中智能提示, value="hello" index="0"java同樣用index來設置參數而不用name也是同樣的
      1. csharp
        <object name="PersonDao" type="PersonDaoAlias"  
             singleton="false">
        <constructor-arg  value="hello" index="0" />
        </object>
      2. java
        <bean id="PersonDao" class="springdemo.PersonDao"  
           singleton="false">
        <constructor-arg  value="hello" index="0" />
        </bean>
  2. 當構造函數一個參數是一個對象的時候同PersonchildDao對象, 用ref屬性來引用其餘對象,或者從新建立一個對象以下:spa

    1. csharp
      <object name="PersonchildDao" type="PersonDaoAlias" 
            singleton="true"
            lazy-init="true">
        <constructor-arg value="hello" index="0"  />
        <constructor-arg name="Child" >
          <object type="SpringBase.PersonDao+child,SpringBase"></object>
        </constructor-arg>
      </object>
    2. java
      <bean id="PersonchildDao" class="PersonDaoAlias"
            singleton="true"
            lazy-init="true">
        <constructor-arg value="hello" index="0"  />
        <constructor-arg index="1" ref="child" />
      </bean>
      由於裏面的name屬性沒什麼用能夠直接去掉
  3. 調用方式都是幾乎同樣的csharpContextRegistry.GetContext().GetObject("name").net

    或者java new FileSystemXmlApplicationContext("classpath:bean.xml").getBean("name")code

  4. 節點的lazy-init,屬性值爲false,容器在初始化的時候就會建立它們。將該屬性設置爲true, 就可將對象的建立爲使用時建立

    1. csharp:控制singleton對象的建立時機當對象不是singleton時會報錯
    2. java:沒有singleton限制
  5. 靜態工廠方法建立對象, 調用的是PersonDao類裏面的靜態方法factoryInstance來建立實例, 其中factory-method表示的是靜態對象的方法, 由於方法有參數因此用constructor-arg來賦值

    1. csharp
      <object name="staticfactoryDao" type="PersonDaoAlias" 
          factory-method="factoryInstance">
        <constructor-arg  value="hello" index="0" />
      </object>
    2. java
      <bean id="staticfactoryDao" class="springdemo.PersonDao" 
            factory-method="factoryInstance">
        <constructor-arg  value="hello" index="0" />
      </bean>
  6. 實例工廠方法建立對象和靜態惟一的區別是多 了一個屬性[factory-object或者factory-bean],表示實例的對象
    1. csharp
      <bean id="factoryDao"  factory-method="CreateInstance" 
         factory-object="PersonDao">
        <constructor-arg  value="hello" index="0" />
      </bean>
    2. java
      <bean id="factoryDao"  factory-method="CreateInstance" 
         factory-bean="PersonDao">
        <constructor-arg  value="hello" index="0" />
      </bean>
  7. property是給對象的屬性賦值,如上我在屬性和構造函數中同時爲name賦值, 最後的值爲屬性中賦予的值,在java中要寫setter才行,csharp有默認的
  8. 設置null值,好比上面的構造函數Child爲null,只是設置null標籤便可
    1. csharp
      <object name="PersonchildDao" type="PersonDaoAlias" 
            singleton="true"
            lazy-init="true">
        <constructor-arg value="hello" index="0"  />
        <constructor-arg name="Child" >
            <null/>
        </constructor-arg>
      </object>
    2. java
      <bean id="PersonchildDao" class="PersonDaoAlias"
            singleton="true"
            lazy-init="true">
        <constructor-arg value="hello" index="0"  />
        <constructor-arg index="1"><null/></constructor-arg>
      </bean>
  9. 設置爲空值,如下兩種均可以達到效果
    <property name="name" value=""></property>
    <property name="name"><value/></property>
    property標籤內是必需要有value屬性或者標籤的

遇到2個問題

  1. csharp中泛型加內部類實現不了
  2. java中內部類注入失敗

相關文章
相關標籤/搜索