Ⅱ.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>
- 嵌套类型的对象实例化
在PersonDao中再添加一个类child(也就是内部类)<object name="child" type="SpringBase.PersonDao+child,SpringBase"></object>用+号表示内部类
ContextRegistry.GetContext("test").GetObject("child")- 当对象是一个泛型的时候
public class VarClass<T> { }<object name="var"
type="SpringBase.VarClass<int>,SpringBase"></object>因为在xml文件里面<符号是敏感符号所以用html的
<来代替,
工厂模式创建泛型也就是把方法上的泛型进行赋予类型a(),
那么factory-method为a<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不支持泛型检查
java和csharp都有的共同点
- 当存在有参构造函数的时候,直接实例化会失败,有2中方案
- 要么再次声明一个空的构造函数
- 要么通过实例化参数来,因为默认的时候是用空构造的,
name对应的是哪个参数,value也就是值啦,
其中objects添加的属性是为了让标签能在vs中智能提示,
value="hello" index="0"和java一样用index来设置参数而不用name也是一样的
- csharp:
<object name="PersonDao" type="PersonDaoAlias"
singleton="false">
<constructor-arg value="hello" index="0" />
</object>- java:
<bean id="PersonDao" class="springdemo.PersonDao"
singleton="false">
<constructor-arg value="hello" index="0" />
</bean>当构造函数一个参数是一个对象的时候同PersonchildDao对象,
用ref属性来引用其他对象,或者重新创建一个对象如下:
- 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>- 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属性没什么用可以直接去掉
调用方式都是几乎一样的csharp的ContextRegistry.GetContext().GetObject("name")
或者java的 new FileSystemXmlApplicationContext("classpath:bean.xml").getBean("name")
节点的lazy-init,属性值为false,容器在初始化的时候就会创建它们。将该属性设置为true,
就可将对象的创建为使用时创建
- csharp:控制singleton对象的创建时机当对象不是singleton时会报错
- java:没有singleton限制
静态工厂方法创建对象,
调用的是PersonDao类里面的静态方法factoryInstance来创建实例,
其中factory-method表示的是静态对象的方法,
因为方法有参数所以用constructor-arg来赋值
- csharp:
<object name="staticfactoryDao" type="PersonDaoAlias"
factory-method="factoryInstance">
<constructor-arg value="hello" index="0" />
</object>- java:
<bean id="staticfactoryDao" class="springdemo.PersonDao"
factory-method="factoryInstance">
<constructor-arg value="hello" index="0" />
</bean>- 实例工厂方法创建对象和静态唯一的区别是多
了一个属性[factory-object或者factory-bean],表示实例的对象
- csharp:
<bean id="factoryDao" factory-method="CreateInstance"
factory-object="PersonDao">
<constructor-arg value="hello" index="0" />
</bean>- java:
<bean id="factoryDao" factory-method="CreateInstance"
factory-bean="PersonDao">
<constructor-arg value="hello" index="0" />
</bean>- property是给对象的属性赋值,如上我在属性和构造函数中同时为name赋值,
最后的值为属性中赋予的值,在java中要写setter才行,csharp有默认的- 设置null值,比如上面的构造函数Child为null,只是设置null标签即可
- 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>- 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>- 设置为空值,以下两种都可以达到效果
<property name="name" value=""></property><property name="name"><value/></property>property标签内是必须要有value属性或者标签的
遇到2个问题
- csharp中泛型加内部类实现不了
- java中内部类注入失败
- 下一篇:Ⅲ.spring的点点滴滴--赋值
- 上一篇:Ⅰ.Spring的点点滴滴--序章
- 本文链接地址:Ⅱ.spring的点点滴滴--对象
Ⅱ.spring的点点滴滴--对象的更多相关文章
- Ⅲ.spring的点点滴滴--赋值
承接上文 对象的赋值(调用方式都一样不再阐述) .net篇(环境为vs2012+Spring.Core.dll v1.31) public class PropertyDemo{ public Sys ...
- Ⅰ.Spring的点点滴滴--序章
spring是一个轻量级的控制反转(IoC)和面向切面(AOP)的容器框架 .net篇(环境为vs2012+Spring.Core.dll) 新建一个控制台 using Spring.Context; ...
- Ⅹ.spring的点点滴滴--IObjectPostProcessor(对象后处理器)
承接上文 IObjectPostProcessor(对象后处理器) 前提是实现接口的对象注册给当前容器 C#: 通过继承AbstractObjectFactory对象的AddObjectPostPro ...
- Ⅷ.spring的点点滴滴--抽象对象和子对象
承接上文 抽象对象和子对象 .net篇(环境为vs2012+Spring.Core.dll v1.31) public class parent { public string Name { get; ...
- Ⅴ.spring的点点滴滴--引用其他对象或类型的成员
承接上文 引用其他对象或类型的成员 .net篇(环境为vs2012+Spring.Core.dll v1.31) public class Person { public string Name { ...
- Ⅶ.spring的点点滴滴--自定义对象行为
承接上文 自定义对象行为 .net篇(环境为vs2012+Spring.Core.dll v1.31) public class lifeCycle : Spring.Objects.Factory. ...
- Ⅳspring的点点滴滴--方法和事件
承接上文 方法和事件 .net篇(环境为vs2012+Spring.Core.dll v1.31) public abstract class MethodDemo { protected abstr ...
- XI.spring的点点滴滴--IObjectFactoryPostProcessor(工厂后处理器)
承接上文 IObjectFactoryPostProcessor(工厂后处理器)) 前提是实现接口的对象注册给当前容器 直接继承的对象调用这个postProcessBeanFactory方法,参数为工 ...
- Ⅸ.spring的点点滴滴--IObjectFactory与IFactoryObject的杂谈
承接上文 ObjectFactory与IFactoryObject的杂谈 .net篇(环境为vs2012+Spring.Core.dll v1.31) public class parent { pu ...
随机推荐
- android捕获ListView中每个item点击事件
转自:http://www.cnblogs.com/pswzone/archive/2012/03/10/2389275.html package com.wps.android; import ...
- [Bhatia.Matrix Analysis.Solutions to Exercises and Problems]PrI.6.1
Given a basis $U=(u_1,\cdots,u_n)$ not necessarily orthonormal, in $\scrH$, how would you compute th ...
- Hessian介绍
Hessian是什么 Hessian类似Web Service,是一种高效简洁的远程调用框架. Hessian的主页:http://hessian.caucho.com/ 有关网上的对Hess ...
- POJ2104 K-th Number 划分树 模板题啊
/*Source Code Problem: 2104 User: 96655 Memory: 14808K Time: 1282MS Language: G++ Result: Accepted S ...
- 设计模式_Interpreter_解释器模式
形象例子: 俺有一个<泡MM真经>,上面有各种泡MM的攻略,比如说去吃西餐的步骤.去看电影的方法等等,跟MM约会时,只要做一个Interpreter,照着上面的脚本执行就可以了.解释器模式 ...
- 在App里面添加App Store中App链接的解决方法
详见stackoverflow.com/questions/433907/how-to-link-to-apps-on-the-app-store http://developer.apple.com ...
- Leetcode OJ : Repeated DNA Sequences hash python solution
Total Accepted: 3790 Total Submissions: 21072 All DNA is composed of a series of nucleotides abb ...
- Hive基础介绍
HIVE结构 Hive 是建立在 Hadoop 上的数据仓库基础构架.它提供了一系列的工具,可以用来进行数据提取转化加载(ETL),这是一种可以存储.查询和分析存储在 Hadoop 中的大规模数据的机 ...
- 怎么创建MongoDB数据库
MongoDB didn’t provides any command to create “database“. Actually, you don’t need to create it manu ...
- dynamic调用时报RuntimeBinderException:“object”未包含“xxx”的定义 错误
情况如下:两个项目项目A命名空间 Test.PA 匿名类型所在 项目B命名空间 Test.PB 在Test.PB 中通过dynamic关键字调用Test.PA中匿名类型时报上述错误 解决办法 在项 ...