spring框架学习(二)依赖注入

3 篇文章 0 订阅

spring框架为我们提供了三种注入方式,分别是set注入,构造方法注入,接口注入。接口注入不作要求,下面介绍前两种方式。


1set注入

  采用属性的set方法进行初始化,就成为set注入。

    1)给普通字符类型赋值。

[java]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. public class User{  
  2.    privateString username;  
  3.    
  4.    publicString getUsername() {  
  5.        returnusername;  
  6.    }  
  7.    publicvoid setUsername(String username) {  
  8.       this.username= username;  
  9.    }  
  10. }  


   我们只需要提供属性的set方法,然后去属性文件中去配置好让框架能够找到applicationContext.xml文件beans标签。标签beans中添加bean标签, 指定idclass值,id值不做要求,class值为对象所在的完整路径。bean标签再添加property 标签,要求,name值与User类中对应的属性名称一致。value值就是我们要给User类中的username属性赋的值。

[html]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. <bean id="userAction"class="com.lsz.spring.action.User" >  
  2. <span style="white-space:pre">  </span><property name="username" value="admin"></property>  
  3. </bean>  

   2)给对象赋值

 同样提供对象的set方法

[java]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. public class User{  
  2.      private UserService userservice;  
  3.      public UserServicegetUserservice() {  
  4.           returnuser;  
  5.      }  
  6.      public void setUserservice(UserService userservice){  
  7.          this.userservice= userservice;  
  8.      }  
  9. }  

   配置文件中要增加UserServicebean标签声明及User对象对UserService引用。

[html]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. <!--对象的声明-->  
  2. <bean id="userService" class="com.lsz.spring.service.UserService"></bean>  
  3.    
  4. <bean id="userAction"class="com.lsz.spring.action.User" >  
  5.    <property name="userservice" ref="userService"></property>  
  6. </bean>  

  这样配置,框架就会将UserService对象注入到User类中。

 

  3)给list集合赋值

 同样提供set方法

[java]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. public class User{  
  2.     privateList<String> username;  
  3.     publicList<String> getUsername() {  
  4.         returnusername;  
  5.     }  
  6.     publicvoid setUsername(List<String> username) {  
  7.         this.username= username;  
  8.     }  
  9. }  

[html]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. <bean id="userAction"class="com.lsz.spring.action.User" >  
  2.      <propertynamepropertyname="username">  
  3.            <list>  
  4.                <value>zhang,san</value>  
  5.                <value>lisi</value>  
  6.                <value>wangwu</value>                                  
  7.                </list>  
  8.     </property>  
  9. </bean>  


 

  4)给属性文件中的字段赋值

[java]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. public class User{  
  2.     privateProperties props ;  
  3.     publicProperties getProps() {  
  4.         returnprops;  
  5.     }  
  6.     publicvoid setProps(Properties props) {  
  7.         this.props= props;  
  8.     }  
  9. }  

[html]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. <bean>  
  2.     <propertynamepropertyname="props">  
  3.         <props>  
  4.            <propkeypropkey="url">jdbc:oracle:thin:@localhost:orl</prop>  
  5.            <propkeypropkey="driverName">oracle.jdbc.driver.OracleDriver</prop>  
  6.            <propkeypropkey="username">scott</prop>  
  7.            <propkeypropkey="password">tiger</prop>  
  8.         </props>  
  9.     </property>  
  10. </bean>  


<prop>标签中的key值是.properties属性文件中的名称


注意:

  无论给什么赋值,配置文件中<property>标签的name属性值一定是和对象中名称一致。

 

 

2构造方法注入

   1)构造方法一个参数

 

[java]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. public class User{  
  2.     privateString usercode;  
  3.     publicUser(String usercode) {  
  4.         this.usercode=usercode;  
  5.     }  
  6. }  

[html]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. <bean id="userAction"class="com.lsz.spring.action.User">                          
  2.     <constructor-argvalueconstructor-argvalue="admin"></constructor-arg>                          
  3. </bean>  


   2)构造函数有两个参数时

  当参数为非字符串类型时,在配置文件中需要制定类型,如果不指定类型一律按照字符串类型赋值。

  当参数类型不一致时,框架是按照字符串的类型进行查找的,因此需要在配置文件中制定是参数的位置

 

[html]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. <constructor-argvalueconstructor-argvalue="admin"index="0"></constructor-arg>                  
  2. <constructor-argvalueconstructor-argvalue="23" type="int"index="1"></constructor-arg>  
  3.    

  这样制定,就是构造函数中,第一个参数为string类型,第二个参数为int类型

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值