承接上文

对象的各种实例化


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

Ⅱ.spring的点点滴滴--对象的更多相关文章

  1. Ⅲ.spring的点点滴滴--赋值

    承接上文 对象的赋值(调用方式都一样不再阐述) .net篇(环境为vs2012+Spring.Core.dll v1.31) public class PropertyDemo{ public Sys ...

  2. Ⅰ.Spring的点点滴滴--序章

    spring是一个轻量级的控制反转(IoC)和面向切面(AOP)的容器框架 .net篇(环境为vs2012+Spring.Core.dll) 新建一个控制台 using Spring.Context; ...

  3. Ⅹ.spring的点点滴滴--IObjectPostProcessor(对象后处理器)

    承接上文 IObjectPostProcessor(对象后处理器) 前提是实现接口的对象注册给当前容器 C#: 通过继承AbstractObjectFactory对象的AddObjectPostPro ...

  4. Ⅷ.spring的点点滴滴--抽象对象和子对象

    承接上文 抽象对象和子对象 .net篇(环境为vs2012+Spring.Core.dll v1.31) public class parent { public string Name { get; ...

  5. Ⅴ.spring的点点滴滴--引用其他对象或类型的成员

    承接上文 引用其他对象或类型的成员 .net篇(环境为vs2012+Spring.Core.dll v1.31) public class Person { public string Name { ...

  6. Ⅶ.spring的点点滴滴--自定义对象行为

    承接上文 自定义对象行为 .net篇(环境为vs2012+Spring.Core.dll v1.31) public class lifeCycle : Spring.Objects.Factory. ...

  7. Ⅳspring的点点滴滴--方法和事件

    承接上文 方法和事件 .net篇(环境为vs2012+Spring.Core.dll v1.31) public abstract class MethodDemo { protected abstr ...

  8. XI.spring的点点滴滴--IObjectFactoryPostProcessor(工厂后处理器)

    承接上文 IObjectFactoryPostProcessor(工厂后处理器)) 前提是实现接口的对象注册给当前容器 直接继承的对象调用这个postProcessBeanFactory方法,参数为工 ...

  9. Ⅸ.spring的点点滴滴--IObjectFactory与IFactoryObject的杂谈

    承接上文 ObjectFactory与IFactoryObject的杂谈 .net篇(环境为vs2012+Spring.Core.dll v1.31) public class parent { pu ...

随机推荐

  1. Entity Framework 数据生成选项DatabaseGenerated

    在EF中,我们建立数据模型的时候,可以给属性配置数据生成选项DatabaseGenerated,它后有三个枚举值:Identity.None和Computed. Identity:自增长 None:不 ...

  2. java并发之CountDownLatch、Semaphore和CyclicBarrier

    JAVA并发包中有三个类用于同步一批线程的行为,分别是CountDownLatch.Semaphore和CyclicBarrier. CountDownLatch Java之CountDownLatc ...

  3. 0、IOS8:Xcode6 playground

    一.Playground介绍 Playground是Xcode6中自带的Swift代码开发环境.俗话说“功欲善其事,必先利其器”.以前在Xcode5中编写脚本代码,例如编写JS,其编写过程很痛苦,Xc ...

  4. Apache-AB压力测试实例

    一 AB背景介绍 Apache附带的压力测试工具apache bench--简称ab,非常容易使用,并且完全可以摸你各种条件对Web服务器发起测试请求.ab可以直接在Web服务器本地发起测试请求,这对 ...

  5. C++模拟键盘鼠标消息

    #include <Windows.h> /* * === FUNCTION ======================================================= ...

  6. JDBCTemplate.java

    package com.pk.xjgs.util; import java.sql.Connection; import java.sql.SQLException; import java.util ...

  7. PHP与MySQL动态网站开发1

    PHP内嵌在HTML中,置于 <?php ?> 标签内 一般php文件扩展名.php 在body结算标签之前 对于远程服务器,可以用ftp工具传程序 打印语句 echo'Hello Wor ...

  8. jquery chart plugin

    jquery flot http://www.jqueryflottutorial.com/ jquery jqplot http://www.jqplot.com/ highcharts中文网 : ...

  9. DataRow数组 转 datatable

    DataTable tmpdt = dt.Clone(); DataRow[] drs = dt.Select("legnbr="+legNbr); ) { tmpdt = drs ...

  10. Swift对面向对象提供了良好的支持,下面介绍几个其独有的特性。

    Swift对面向对象提供了良好的支持,下面介绍几个其独有的特性. 懒加载属性 Swift在语言层面上提供了类中懒加载属性的支持,使用lazy作为关键字: class Renderer { lazy v ...