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

编程入门 行业动态 更新时间:2024-10-27 15:26:44

Spring的两种依赖注入方式:setter<a href=https://www.elefans.com/category/jswz/34/1771314.html style=方法注入与构造方法注入"/>

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=""  
  3.        xmlns:xsi=""  
  4.        xsi:schemaLocation="  
  5.            .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=""  
  3.        xmlns:xsi=""  
  4.        xsi:schemaLocation="  
  5.            .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,运行结果如下:

                                                                                                                    





更多推荐

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

本文发布于:2024-03-10 03:42:31,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1726916.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:方法   两种   方式   Spring   setter

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!