Spring IOC注入---set方式注入(容易理解)

IOC注入的方式有好几种,现在就来学习一下set方式注入~

可以注入的内容有:

1.基本类型(8中基本类型+字符串)的装配
2.对象类型的装配
3.集合的装配

现在就来讲讲怎么用set方式注入的吧~

1.基本类型的装配:

配置文件set.xml:

[java]  view plain  copy
 print ?
  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.     xmlns:context="http://www.springframework.org/schema/context"  
  5.     xsi:schemaLocation="http://www.springframework.org/schema/beans  
  6.            http://www.springframework.org/schema/beans/spring-beans-3.2.xsd  
  7.            http://www.springframework.org/schema/context  
  8.            http://www.springframework.org/schema/context/spring-context-3.2.xsd">  
  9.      <bean id="helloBean" class="com.x.spring.bean.HelloBean">  
  10.         <property name="name">  
  11.             <value>tom</value>  
  12.         </property>  
  13.             <property name="age" value="20">  
  14.         </property>  
  15.     </bean>  
  16.       
  17. </beans>  

id是Bean的唯一标识,要求在整个配置文件中要唯一,也可使用name属性,bean标签里面的id和name属性都可以用来标识这个配置的对象,但是id会帮我们检查给对象起的名字是否规范(名字不能重复、不能用数字开头、不能有空格等等),如果检查出来了那么就会报错。name属性不会帮检查这些东西。

property 对于所有用set方式来注入的必须该标签

value是对以基本类型,都用value(标签/属性)来注入,可以实现自动的数据类型转换


HelloBean类:

[java]  view plain  copy
 print ?
  1. public class HelloBean {  
  2.   
  3.     private String name;  
  4.     private int age;  
  5.     public String sayHello(){  
  6.         return "hello "+name +",your age is" + age;  
  7.     }  
  8.     public HelloBean() {  
  9.         super();  
  10.         // TODO Auto-generated constructor stub  
  11.     }  
  12.     public HelloBean(String name, int age) {  
  13.         super();  
  14.         this.name = name;  
  15.         this.age = age;  
  16.     }  
  17.     public String getName() {  
  18.         return name;  
  19.     }  
  20.     public void setName(String name) {  
  21.         this.name = name;  
  22.     }  
  23.     public int getAge() {  
  24.         return age;  
  25.     }  
  26.     public void setAge(int age) {  
  27.         this.age = age;  
  28.     }  
  29.       
  30. }  
测试类SetTest:

[java]  view plain  copy
 print ?
  1. public class SetTest {  
  2.   
  3.     public static void main(String[] args) {  
  4.         //基本类型的装配  
  5.         ApplicationContext ac =   
  6.                 new ClassPathXmlApplicationContext("set.xml");  
  7.             //获取容器的一个实例  
  8.             HelloBean hb = (HelloBean) ac.getBean("helloBean");  
  9.             System.out.println(hb.sayHello());  
  10.     }  
  11. }  


效果图:


2.对象类型的装配:

 (1)、<ref local=" "/> 用于涉及的对象的id在当前配置文件中(用于在本配置文件中配置了
的bean的引入同ref="..")

(2)、<ref bean=" "/>  用于涉及的对象的id不在本配置文件中(用于引用不在本配置文件中配置的bean)

(3)、使用property的ref属性引用

下面的是第三种方式的~

写了俩个xml文件:some.xml和other.xml

some.xml:

[java]  view plain  copy
 print ?
  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.     xmlns:context="http://www.springframework.org/schema/context"  
  5.     xsi:schemaLocation="http://www.springframework.org/schema/beans  
  6.            http://www.springframework.org/schema/beans/spring-beans-3.2.xsd  
  7.            http://www.springframework.org/schema/context  
  8.            http://www.springframework.org/schema/context/spring-context-3.2.xsd">  
  9.    <bean id="someBean" class="com.x.spring.bean.SomeBean">  
  10.         <property name="ob">  
  11.             <ref bean="otherBean" />  
  12.         </property>  
  13.     </bean>   
  14.       
  15.       
  16. </beans>  


other.xml:

[java]  view plain  copy
 print ?
  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.     xmlns:context="http://www.springframework.org/schema/context"  
  5.     xsi:schemaLocation="http://www.springframework.org/schema/beans  
  6.            http://www.springframework.org/schema/beans/spring-beans-3.2.xsd  
  7.            http://www.springframework.org/schema/context  
  8.            http://www.springframework.org/schema/context/spring-context-3.2.xsd">  
  9.    <bean id="otherBean" class="com.x.spring.bean.OtherBean">  
  10.         <property name="str1">  
  11.             <value>string1</value>  
  12.         </property>  
  13.     </bean>  
  14. </beans>  

SomeBean:

[java]  view plain  copy
 print ?
  1. public class SomeBean {  
  2.     private OtherBean ob;  
  3.     public void printInfo(){  
  4.         System.out.println("someBean "+ob);  
  5.     }  
  6.     public OtherBean getOb() {  
  7.         return ob;  
  8.     }  
  9.     public void setOb(OtherBean ob) {  
  10.         this.ob = ob;  
  11.     }  
  12. }  
OtherBean:

[java]  view plain  copy
 print ?
  1. public class OtherBean {  
  2.     private String str1;  
  3.     public String getStr1() {  
  4.         return str1;  
  5.     }  
  6.     public void setStr1(String str1) {  
  7.         this.str1 = str1;  
  8.     }  
  9.     public String toString(){  
  10.         return "OtherBean "+str1;  
  11.     }  
  12. }  

测试类SetTest:

[java]  view plain  copy
 print ?
  1. public class SetTest {  
  2.   
  3.     public static void main(String[] args) {  
  4.   
  5.             //对象类型的装配  
  6.                 String[] path = {"some.xml","other.xml"};  
  7.             ApplicationContext ac = new ClassPathXmlApplicationContext(path);  
  8.             SomeBean sb = (SomeBean) ac.getBean("someBean");  
  9.             sb.printInfo();  
  10.     }  
  11. }  

效果图:


在这里写一下第二种方式的~

只需要改一下配置文件some.xml:

[java]  view plain  copy
 print ?
  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.     xmlns:context="http://www.springframework.org/schema/context"  
  5.     xsi:schemaLocation="http://www.springframework.org/schema/beans  
  6.            http://www.springframework.org/schema/beans/spring-beans-3.2.xsd  
  7.            http://www.springframework.org/schema/context  
  8.            http://www.springframework.org/schema/context/spring-context-3.2.xsd">  
  9.     <bean id="someBean" class="com.x.spring.bean.SomeBean">  
  10.         <property name="ob" ref="otherBean">  
  11.           </property>  
  12.     </bean>   
  13.       
  14.       
  15. </beans>  


效果图就不贴了,和上面的一样~

3.集合的装配:

方式:配置元素<list> <set> <map> <props>

list.xml:

[java]  view plain  copy
 print ?
  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.     xmlns:context="http://www.springframework.org/schema/context"  
  5.     xsi:schemaLocation="http://www.springframework.org/schema/beans  
  6.            http://www.springframework.org/schema/beans/spring-beans-3.2.xsd  
  7.            http://www.springframework.org/schema/context  
  8.            http://www.springframework.org/schema/context/spring-context-3.2.xsd">  
  9.    <bean id="listBean" class="com.x.spring.bean.ListBean">  
  10.         <property name="listProperty">  
  11.              <list>  
  12.                 <value>list1</value>  
  13.                 <value>list1</value>  
  14.                 <value>list3</value>  
  15.              </list>  
  16.         </property>  
  17.         <property name="setProperty">  
  18.              <set>  
  19.                 <value>set1</value>  
  20.                 <value>set1</value>  
  21.                 <value>set3</value>  
  22.              </set>  
  23.         </property>  
  24.         <property name="mapProperty">  
  25.              <map>  
  26.                 <entry key="key1">  
  27.                   <value>value1</value>  
  28.                 </entry>  
  29.                 <entry key="key2">  
  30.                   <value>value2</value>  
  31.                 </entry>  
  32.              </map>  
  33.         </property>  
  34.         <property name="property">  
  35.              <props>  
  36.               <prop key="key1">prop1</prop>  
  37.               <prop key="key2">prop2</prop>  
  38.               <prop key="key3">prop3</prop>  
  39.              </props>  
  40.         </property>  
  41.     </bean>  
  42. </beans>  
ListBean类:

[java]  view plain  copy
 print ?
  1. public class ListBean {  
  2.   
  3.         private List listProperty;  
  4.         private Set setProperty;  
  5.         private Map mapProperty;  
  6.         private Properties property;  
  7.         public List getListProperty() {  
  8.             return listProperty;  
  9.         }  
  10.         public void setListProperty(List listProperty) {  
  11.             this.listProperty = listProperty;  
  12.         }  
  13.         public Set getSetProperty() {  
  14.             return setProperty;  
  15.         }  
  16.         public void setSetProperty(Set setProperty) {  
  17.             this.setProperty = setProperty;  
  18.         }  
  19.         public Map getMapProperty() {  
  20.             return mapProperty;  
  21.         }  
  22.         public void setMapProperty(Map mapProperty) {  
  23.             this.mapProperty = mapProperty;  
  24.         }  
  25.         public Properties getProperty() {  
  26.             return property;  
  27.         }  
  28.         public void setProperty(Properties property) {  
  29.             this.property = property;  
  30.         }  
  31.         public void printInfo(){  
  32.             System.out.println("listProperty:");  
  33.             System.out.println(listProperty);  
  34.             System.out.println("setProperty:");  
  35.             System.out.println(setProperty);  
  36.             Set set = mapProperty.entrySet();  
  37.             Iterator it = set.iterator();  
  38.             System.out.println("mapProperty:");  
  39.             while(it.hasNext()){  
  40.                  Map.Entry entry = (Entry) it.next();  
  41.                  System.out.println("Key " +entry.getKey() );  
  42.                  System.out.println("value "+entry.getValue());  
  43.             }  
  44.             System.out.println("props: ");  
  45.             Set set2 = property.entrySet();  
  46.             Iterator it2 = set2.iterator();  
  47.             while(it2.hasNext()){  
  48.                 Map.Entry entry= (Entry) it2.next();  
  49.                 System.out.println("key "+entry.getKey());  
  50.                 System.out.println("value "+entry.getValue());  
  51.             }  
  52.         }     
  53. }  
这个好像没什么讲的~

SetTest测试类:

[java]  view plain  copy
 print ?
  1. public class SetTest {  
  2.   
  3.     public static void main(String[] args) {  
  4.               
  5.         //集合的装配  
  6.         String path = "list.xml";  
  7.         ApplicationContext  ac =  
  8.             new ClassPathXmlApplicationContext(path);  
  9.         ListBean lb = (ListBean) ac.getBean("listBean");  
  10.         lb.printInfo();  
  11.   
  12.     }  
  13. }  
效果图:


写到这里set方式注入就写完了~

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值