property和constructor-arg的使用
2013-05-04 18:00
274 查看
转载自:ITEYE.com via:zzy7182
http://zzy7182.iteye.com/blog/1153473
一:依赖注入的方式
constructor-arg:通过构造函数注入。
property:通过setxx方法注入。
二:constructor-arg的简单使用
java代码
Java代码
![](https://oscdn.geek-share.com/Uploads/Images/Content/201909/19/35aed0ddc54386f0536a34e55281c695.png)
public class Man {
private String name ;
private int age;
private List hobby;
private Map friends;
private Set set;
private boolean ifMarried;
public Man() {
}
public Man(String name, int age,List hobby,Map friends,Set set,boolean ifMarried){
this.name = name;
this.age = age;
this.hobby = hobby;
this.friends = friends;
this.set = set;
this.ifMarried = ifMarried;
}
public String getInfo(){
String info = "姓名:"+this.name+"\n年龄:"+this.age+"\n爱好:"+this.hobby+"\n朋友:"+this.friends+"\n婚否:"+this.ifMarried+"\n其他的:"+this.set;
return info;
}
xml配置文件
Java代码
![](https://oscdn.geek-share.com/Uploads/Images/Content/201909/19/35aed0ddc54386f0536a34e55281c695.png)
<bean id="man" class="com.spring.test.man.Man">
<constructor-arg value="zzy" index="0" >
</constructor-arg>
<constructor-arg value="10" index="1">
</constructor-arg>
<constructor-arg>
<list>
<value>movie</value>
<value>music</value>
</list>
</constructor-arg>
<constructor-arg>
<set>
<value>Lady is GaGa</value>
<value>GaGa is Lady</value>
</set>
</constructor-arg>
<constructor-arg>
<map>
<entry key="liuhua" value="man"></entry>
<entry key="xujinglei" value="female"></entry>
</map>
</constructor-arg>
<constructor-arg index="5" value="0">
</constructor-arg>
</bean>
最后一个参数ifMarried是一个boolean类型的参数,在配置的时候可以是true/false或者0/1;
二:property的简单使用
java代码:
Java代码
![](https://oscdn.geek-share.com/Uploads/Images/Content/201909/19/35aed0ddc54386f0536a34e55281c695.png)
public class Doctor {
private String name;
private String sex;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public void init(){
System.out.println("88888888888");
}
public void init(String name,String sex){
this.name = name;
this.sex = sex;
}
}
xml配置文件:
Java代码
![](https://oscdn.geek-share.com/Uploads/Images/Content/201909/19/35aed0ddc54386f0536a34e55281c695.png)
<bean id="doctor" class="com.spring.test.man.Doctor" init-method="init">
<property name="name" value="doctor"></property>
<property name="sex" value="i don't know"></property>
lt;/bean>
在这里我配置了一个init-method="init"表示在容易实例化这个doctor的时候,调用一个Doctor类的init方法,本来还以为可以通过这个init方法来注入要注入的信息,但是尝试过后才知道这个init方法是不能带参数的。
http://zzy7182.iteye.com/blog/1153473
一:依赖注入的方式
constructor-arg:通过构造函数注入。
property:通过setxx方法注入。
二:constructor-arg的简单使用
java代码
Java代码
![](https://oscdn.geek-share.com/Uploads/Images/Content/201909/19/35aed0ddc54386f0536a34e55281c695.png)
public class Man {
private String name ;
private int age;
private List hobby;
private Map friends;
private Set set;
private boolean ifMarried;
public Man() {
}
public Man(String name, int age,List hobby,Map friends,Set set,boolean ifMarried){
this.name = name;
this.age = age;
this.hobby = hobby;
this.friends = friends;
this.set = set;
this.ifMarried = ifMarried;
}
public String getInfo(){
String info = "姓名:"+this.name+"\n年龄:"+this.age+"\n爱好:"+this.hobby+"\n朋友:"+this.friends+"\n婚否:"+this.ifMarried+"\n其他的:"+this.set;
return info;
}
xml配置文件
Java代码
![](https://oscdn.geek-share.com/Uploads/Images/Content/201909/19/35aed0ddc54386f0536a34e55281c695.png)
<bean id="man" class="com.spring.test.man.Man">
<constructor-arg value="zzy" index="0" >
</constructor-arg>
<constructor-arg value="10" index="1">
</constructor-arg>
<constructor-arg>
<list>
<value>movie</value>
<value>music</value>
</list>
</constructor-arg>
<constructor-arg>
<set>
<value>Lady is GaGa</value>
<value>GaGa is Lady</value>
</set>
</constructor-arg>
<constructor-arg>
<map>
<entry key="liuhua" value="man"></entry>
<entry key="xujinglei" value="female"></entry>
</map>
</constructor-arg>
<constructor-arg index="5" value="0">
</constructor-arg>
</bean>
最后一个参数ifMarried是一个boolean类型的参数,在配置的时候可以是true/false或者0/1;
二:property的简单使用
java代码:
Java代码
![](https://oscdn.geek-share.com/Uploads/Images/Content/201909/19/35aed0ddc54386f0536a34e55281c695.png)
public class Doctor {
private String name;
private String sex;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public void init(){
System.out.println("88888888888");
}
public void init(String name,String sex){
this.name = name;
this.sex = sex;
}
}
xml配置文件:
Java代码
![](https://oscdn.geek-share.com/Uploads/Images/Content/201909/19/35aed0ddc54386f0536a34e55281c695.png)
<bean id="doctor" class="com.spring.test.man.Doctor" init-method="init">
<property name="name" value="doctor"></property>
<property name="sex" value="i don't know"></property>
lt;/bean>
在这里我配置了一个init-method="init"表示在容易实例化这个doctor的时候,调用一个Doctor类的init方法,本来还以为可以通过这个init方法来注入要注入的信息,但是尝试过后才知道这个init方法是不能带参数的。
相关文章推荐
- property和constructor-arg的使用
- spring 学习之 bean 的注入方式 property和constructor-arg的使用方式
- [JAVA][Spring]Spring2 constructor-arg和property的使用示例
- Spring - constructor-arg和property的使用示例
- property和constructor-arg的使用
- property和constructor-arg的使用
- property和constructor-arg的使用
- property和constructor-arg的使用
- spring - property和constructor-arg的使用
- Spring - constructor-arg和property的使用示例
- property和constructor-arg的使用
- Spring - constructor-arg和property的使用示例
- spring - property和constructor-arg的使用
- Spring - constructor-arg和property的使用示例
- Spring - constructor-arg和property的使用示例
- Spring - constructor-arg和property的使用示例
- property和constructor-arg的使用
- property和constructor-arg的使用
- property和constructor-arg的使用
- property和constructor-arg的使用