Ⅱ.spring的点点滴滴--对象(一)

2014-11-24 08:14:41 · 作者: · 浏览: 1
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对象为
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.net
        http://www.springframework.net/xsd/spring-objects.xsd" >
type="SpringBase.PersonDao+child,SpringBase">
singleton="true"
lazy-init="true">
factory-method="factoryInstance">
factory-object="PersonDao">
嵌套类型的对象实例化 在PersonDao中再添加一个类child(也就是内部类)
用+号表示内部类
ContextRegistry.GetContext("test").GetObject("child")
当对象是一个泛型的时候
public class VarClass { }
type="SpringBase.VarClass&;lt;int>,SpringBase">
因为在xml文件里面<符号是敏感符号所以用 html的<来代替, 工厂模式创建泛型也就是把方法上的泛型进行赋予类型a(), 那么factory-method为a<int,int> 当泛型为内部类的时候,实例化失败,未知原因
java篇(环境为Maven+Jdk1.7+IntelliJ IDEA 12.1.4)
补充java的alias 当配置文件设置如下的时候, 在程序里面可以通过ctx.getBean("PersonDaoAlias");别名来实例化
修改原来的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对象为
singleton="true"
lazy-init="true">
factory-method="factoryInstance">
1.不能使用泛型注入,泛型是在编译期类型检查用的所以jvm在运行期根本不管是不是generic, 对他来说编译后的类文件和没有泛型的没区别.spring不支持泛型检查
java和csharp都有的共同点
当存在有参构造函数的时候,直接实例化会失败,有2中方案
要么再次声明一个空的构造函数
要么通过实例化参数来,因为默认的时候是用空构造的, name对应的是哪个参数,value也就是值啦, 其中objects添加的属性是为了让标签能在vs中智能提示, value="hello" index="0"和java一样用index来设置参数而不用name也是一样的
csharp:
singleton="false">
java:
<;bean id="PersonDao" class="springdemo.PersonDao"
singleton="false">;
<;constructor-arg value="hello" index="0" />
<;/bean>
当构造函数一个参数是一个对象的时候同PersonchildDao对象, 用ref属性来引用其他对象,或者重新创建一个对象如下:
csharp:
singleton="true"
lazy-init="true">
java:
singleton="true"