设为首页 加入收藏

TOP

Ⅱ.spring的点点滴滴--对象(一)
2019-09-03 03:10:10 】 浏览:56
Tags:.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

补充javaalias 当配置文件设置如下的时候, 在程序里面可以通过ctx.getBean("PersonDaoAlias");别名来实例化

 <beans>
    <alias name="PersonDao" alias="PersonDaoAlias" />
    <bean id="PersonDao"  class="springdemo.
首页 上一页 1 2 3 下一页 尾页 1/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇IOS设计模式的六大设计原则之接口.. 下一篇Ⅰ.Spring的点点滴滴--序章

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目