Spring的两种依赖注入方式:setter方法注入与构造方法注入

 Spring的两种依赖注入方式:setter注入与构造方法注入,这两种方法的不同主要就是在xml文件下对应使用property和constructor-arg属性, 例如:

property属性:<property name="id" value="123"></property>(其中name的值为原类中的属性名)

constructor-arg属性:<constructor-arg index="0" value="456"></constructor-arg>(其中index的值为0~n-1,n代表构造函数中的输入参数的数量)


1.setter方法注入

   setter方法注入即是创建一个普通的JavaBean类,为需要注入的属性通过对应的setter方法即可,如:

(1)创建一个Id类:

[java]  view plain  copy
  1. package com.loster.li;  
  2.   
  3. public class Id {  
  4.     private int id;  
  5.     private String name;  
  6.     public int getId() {  
  7.         return id;  
  8.     }  
  9.     public void setId(int id) {  
  10.         this.id = id;  
  11.     }  
  12.     public String getName() {  
  13.         return name;  
  14.     }  
  15.     public void setName(String name) {  
  16.         this.name = name;  
  17.     }  
  18. }  
(2)创建配置文件Id_Bean.xml
[html]  view plain  copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.        xsi:schemaLocation="http://www.springframework.org/schema/beans  
  5.            http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">  
  6.              
  7.     <bean id="id" class="com.loster.li.Id">  
  8.     <property name="id" value="123"></property>  
  9.     <property name="name" value="xiaoli"></property>  
  10.     </bean>  
  11. </beans>  
(3)编写测试类IdTest.java,并运行:
[java]  view plain  copy
  1. package com.loster.li;  
  2.   
  3. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  4.   
  5. public class IdTest {  
  6.     public static void main(String[] args){  
  7.         ClassPathXmlApplicationContext context = new   
  8.                 ClassPathXmlApplicationContext("com/loster/li/Id_Bean.xml");  
  9.           
  10.         Id id = (Id)context.getBean("id");  
  11.         System.out.println(id.getId());  
  12.         System.out.println(id.getName());  
  13.     }  
  14. }  
    运行结果如下:



2.构造方法注入

(1)将上面的Id.class修改为:

[java]  view plain  copy
  1. package com.loster.li;  
  2.   
  3. public class Id {  
  4.     private int id;  
  5.     private String name;  
  6.     public Id(int id,String name){  
  7.         this.id = id;  
  8.         this.name = name;  
  9.     }  
  10.     public int getId() {  
  11.         return id;  
  12.     }  
  13.     public void setId(int id) {  
  14.         this.id = id;  
  15.     }  
  16.     public String getName() {  
  17.         return name;  
  18.     }  
  19.     public void setName(String name) {  
  20.         this.name = name;  
  21.     }  
  22. }  
(2)将上面的Id_Bean.xml修改为:

[html]  view plain  copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.        xsi:schemaLocation="http://www.springframework.org/schema/beans  
  5.            http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">  
  6.              
  7.     <bean id="id" class="com.loster.li.Id">  
  8.     <constructor-arg index="0" value="456"></constructor-arg>  
  9.     <constructor-arg index="1" value="dawang"></constructor-arg>  
  10.     </bean>  
  11. </beans>  
(3)再次运行IdTest.java,运行结果如下:

                                                                                                                    





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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值