Ⅱ.spring的点点滴滴--对象

承接上文

对象的各种实例化


.net篇(环境为vs2012+Spring.Core.dll

修改原来的PersonDao对象为

    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对象为

    <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");别名来实例化

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

修改原来的PersonDao对象为

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对象为

<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都有的共同点

  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属性来引用其他对象,或者重新创建一个对象如下:

    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")

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

  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中内部类注入失败

上一篇:[055] SSL 3.0曝出Poodle漏洞的解决方式-----开发人员篇


下一篇:深入理解php的MySQL连接类