spring的依赖注入三种方式
2016-10-31 16:20
429 查看
spring框架为我们提供了三种注入方式,分别是set注入,构造方法注入,接口注入。接口注入不作要求,下面介绍前两种方式。
1,set注入
采用属性的set方法进行初始化,就成为set注入。
1)给普通字符类型赋值。
[java] view
plain copy
print?
public class User{
privateString username;
publicString getUsername() {
returnusername;
}
publicvoid setUsername(String username) {
this.username= username;
}
}
我们只需要提供属性的set方法,然后去属性文件中去配置好让框架能够找到applicationContext.xml文件的beans标签。标签beans中添加bean标签, 指定id,class值,id值不做要求,class值为对象所在的完整路径。bean标签再添加property
标签,要求,name值与User类中对应的属性名称一致。value值就是我们要给User类中的username属性赋的值。
[html] view
plain copy
print?
<bean id="userAction"class="com.lsz.spring.action.User" >
<span style="white-space:pre"> </span><property name="username" value="admin"></property>
</bean>
2)给对象赋值
同样提供对象的set方法
[java] view
plain copy
print?
public class User{
private UserService userservice;
public UserServicegetUserservice() {
returnuser;
}
public void setUserservice(UserService userservice){
this.userservice= userservice;
}
}
配置文件中要增加UserService的bean标签声明及User对象对UserService引用。
[html] view
plain copy
print?
<!--对象的声明-->
<bean id="userService" class="com.lsz.spring.service.UserService"></bean>
<bean id="userAction"class="com.lsz.spring.action.User" >
<property name="userservice" ref="userService"></property>
</bean>
这样配置,框架就会将UserService对象注入到User类中。
3)给list集合赋值
同样提供set方法
[java] view
plain copy
print?
public class User{
privateList<String> username;
publicList<String> getUsername() {
returnusername;
}
publicvoid setUsername(List<String> username) {
this.username= username;
}
}
[html] view
plain copy
print?
<bean id="userAction"class="com.lsz.spring.action.User" >
<propertynamepropertyname="username">
<list>
<value>zhang,san</value>
<value>lisi</value>
<value>wangwu</value>
</list>
</property>
</bean>
4)给属性文件中的字段赋值
[java] view
plain copy
print?
public class User{
privateProperties props ;
publicProperties getProps() {
returnprops;
}
publicvoid setProps(Properties props) {
this.props= props;
}
}
[html] view
plain copy
print?
<bean>
<propertynamepropertyname="props">
<props>
<propkeypropkey="url">jdbc:oracle:thin:@localhost:orl</prop>
<propkeypropkey="driverName">oracle.jdbc.driver.OracleDriver</prop>
<propkeypropkey="username">scott</prop>
<propkeypropkey="password">tiger</prop>
</props>
</property>
</bean>
<prop>标签中的key值是.properties属性文件中的名称
注意:
无论给什么赋值,配置文件中<property>标签的name属性值一定是和对象中名称一致。
2构造方法注入
1)构造方法一个参数
[java] view
plain copy
print?
public class User{
privateString usercode;
publicUser(String usercode) {
this.usercode=usercode;
}
}
[html] view
plain copy
print?
<bean id="userAction"class="com.lsz.spring.action.User">
<constructor-argvalueconstructor-argvalue="admin"></constructor-arg>
</bean>
2)构造函数有两个参数时
当参数为非字符串类型时,在配置文件中需要制定类型,如果不指定类型一律按照字符串类型赋值。
当参数类型不一致时,框架是按照字符串的类型进行查找的,因此需要在配置文件中制定是参数的位置
[html] view
plain copy
print?
<constructor-argvalueconstructor-argvalue="admin"index="0"></constructor-arg>
<constructor-argvalueconstructor-argvalue="23" type="int"index="1"></constructor-arg>
这样制定,就是构造函数中,第一个参数为string类型,第二个参数为int类型
1,set注入
采用属性的set方法进行初始化,就成为set注入。
1)给普通字符类型赋值。
[java] view
plain copy
print?
public class User{
privateString username;
publicString getUsername() {
returnusername;
}
publicvoid setUsername(String username) {
this.username= username;
}
}
我们只需要提供属性的set方法,然后去属性文件中去配置好让框架能够找到applicationContext.xml文件的beans标签。标签beans中添加bean标签, 指定id,class值,id值不做要求,class值为对象所在的完整路径。bean标签再添加property
标签,要求,name值与User类中对应的属性名称一致。value值就是我们要给User类中的username属性赋的值。
[html] view
plain copy
print?
<bean id="userAction"class="com.lsz.spring.action.User" >
<span style="white-space:pre"> </span><property name="username" value="admin"></property>
</bean>
2)给对象赋值
同样提供对象的set方法
[java] view
plain copy
print?
public class User{
private UserService userservice;
public UserServicegetUserservice() {
returnuser;
}
public void setUserservice(UserService userservice){
this.userservice= userservice;
}
}
配置文件中要增加UserService的bean标签声明及User对象对UserService引用。
[html] view
plain copy
print?
<!--对象的声明-->
<bean id="userService" class="com.lsz.spring.service.UserService"></bean>
<bean id="userAction"class="com.lsz.spring.action.User" >
<property name="userservice" ref="userService"></property>
</bean>
这样配置,框架就会将UserService对象注入到User类中。
3)给list集合赋值
同样提供set方法
[java] view
plain copy
print?
public class User{
privateList<String> username;
publicList<String> getUsername() {
returnusername;
}
publicvoid setUsername(List<String> username) {
this.username= username;
}
}
[html] view
plain copy
print?
<bean id="userAction"class="com.lsz.spring.action.User" >
<propertynamepropertyname="username">
<list>
<value>zhang,san</value>
<value>lisi</value>
<value>wangwu</value>
</list>
</property>
</bean>
4)给属性文件中的字段赋值
[java] view
plain copy
print?
public class User{
privateProperties props ;
publicProperties getProps() {
returnprops;
}
publicvoid setProps(Properties props) {
this.props= props;
}
}
[html] view
plain copy
print?
<bean>
<propertynamepropertyname="props">
<props>
<propkeypropkey="url">jdbc:oracle:thin:@localhost:orl</prop>
<propkeypropkey="driverName">oracle.jdbc.driver.OracleDriver</prop>
<propkeypropkey="username">scott</prop>
<propkeypropkey="password">tiger</prop>
</props>
</property>
</bean>
<prop>标签中的key值是.properties属性文件中的名称
注意:
无论给什么赋值,配置文件中<property>标签的name属性值一定是和对象中名称一致。
2构造方法注入
1)构造方法一个参数
[java] view
plain copy
print?
public class User{
privateString usercode;
publicUser(String usercode) {
this.usercode=usercode;
}
}
[html] view
plain copy
print?
<bean id="userAction"class="com.lsz.spring.action.User">
<constructor-argvalueconstructor-argvalue="admin"></constructor-arg>
</bean>
2)构造函数有两个参数时
当参数为非字符串类型时,在配置文件中需要制定类型,如果不指定类型一律按照字符串类型赋值。
当参数类型不一致时,框架是按照字符串的类型进行查找的,因此需要在配置文件中制定是参数的位置
[html] view
plain copy
print?
<constructor-argvalueconstructor-argvalue="admin"index="0"></constructor-arg>
<constructor-argvalueconstructor-argvalue="23" type="int"index="1"></constructor-arg>
这样制定,就是构造函数中,第一个参数为string类型,第二个参数为int类型
相关文章推荐
- Spring依赖注入的三种方式
- Spring依赖注入的三种方式
- spring的依赖注入三种方式
- spring Ioc 依赖注入的三种方式:构造函数注入、setter方法注入和接口注入
- Spring的三种依赖注入方式
- SSH深度历险(八) 剖析SSH核心原理+Spring依赖注入的三种方式
- 【SSH系列】深入浅出spring IOC中三种依赖注入方式
- 深入浅出spring IOC中三种依赖注入方式
- 深入浅出spring IOC中三种依赖注入方式
- spring的依赖注入三种方式
- Spring依赖注入的三种方式
- Spring依赖注入的三种方式
- 【Spring】依赖注入三种方式
- spring的依赖注入三种方式
- Spring配置依赖注入的三种方式
- 【Spring】依赖注入三种方式
- spring依赖注入的好处以及三种注入的方式
- SSH深度历险(八) 剖析SSH核心原理+Spring依赖注入的三种方式
- spring的依赖注入三种方式
- Spring的依赖注入(DI)三种方式