zoukankan      html  css  js  c++  java
  • 【spring set注入 注入集合】 使用set注入的方式注入List集合和Map集合/将一个bean注入另一个Bean

    Dao层代码:

    1 package com.it.dao;
    2 
    3 public interface SayHell {
    4         public void sayHello();
    5 }
    View Code

    Dao的Impl实现层:

      1 package com.it.dao.impl;
      2 
      3 import java.util.List;
      4 import java.util.Map;
      5 
      6 import com.it.dao.SayHell;
      7 
      8 /**
      9  * Spring如何知道setter方法?如何将值注入进去的呢?其实方法名是要遵守约定的,setter注入的方法名要遵循“JavaBean getter/setter 方法命名约定”:
     10  * 
     11    JavaBean:是本质就是一个POJO类,但具有一下限制:
     12          该类必须要有公共的无参构造器,如public HelloImpl4() {};
     13          属性为private访问级别,不建议public,如private String message;
     14          属性必要时通过一组setter(修改器)和getter(访问器)方法来访问;
     15      setter方法,以“set” 开头,后跟首字母大写的属性名,如“setMesssage”,简单属性一般只有一个方法参数,方法返回值通常为“void”;
     16      getter方法,一般属性以“get”开头,对于boolean类型一般以“is”开头,后跟首字母大写的属性名,如“getMesssage”,“isOk”;
     17                   还有一些其他特殊情况,比如属性有连续两个大写字母开头,如“URL”,则setter/getter方法为:“setURL”和“getURL”,其他一些特殊情况请参看“Java Bean”命名规范。
     18  * @return
     19  */
     20 public class SayHelloImpl4 implements SayHell {
     21     
     22     private List<String> listValue;
     23     
     24     private Map<String,Integer> mapValue;
     25     
     26     private int fistBleed;
     27     
     28     private Map<String,SayHelloImpl4> firstMap;
     29     
     30     
     31     
     32 
     33     public Map<String, SayHelloImpl4> getFirstMap() {
     34         return firstMap;
     35     }
     36 
     37 
     38     public void setFirstMap(Map<String, SayHelloImpl4> firstMap) {
     39         this.firstMap = firstMap;
     40     }
     41 
     42 
     43     public int getFistBleed() {
     44         return fistBleed;
     45     }
     46 
     47 
     48     public void setFistBleed(int fistBleed) {
     49         this.fistBleed = fistBleed;
     50     }
     51 
     52 
     53     public List<String> getListValue() {
     54         return listValue;
     55     }
     56 
     57 
     58     public void setListValue(List<String> listValue) {
     59         this.listValue = listValue;
     60     }
     61 
     62     
     63 
     64     public Map<String, Integer> getMapValue() {
     65         return mapValue;
     66     }
     67 
     68 
     69     public void setMapValue(Map<String, Integer> mapValue) {
     70         this.mapValue = mapValue;
     71     }
     72 
     73 
     74     @Override
     75     public void sayHello() {
     76         int i=1;
     77         for (String string : listValue) {
     78             System.out.println(i+"、"+string);
     79             i++;
     80         }
     81 
     82     }
     83     
     84     public void sayMapGood(){
     85         int size = mapValue.size();
     86         if(size!=0){
     87             for(int a=1; a<=size ;a++){
     88                 System.out.println(a+"、"+mapValue.get(String.valueOf(a)));
     89             }
     90         }
     91     }    
     92     
     93     public void sayMapGood2(){
     94         int size = firstMap.size();
     95         if(size!=0){
     96             System.out.println(firstMap.get("1").fistBleed);
     97         }
     98     }
     99 
    100 }
    View Code

    配置文件setList.xml:

     1 <?xml version="1.0" encoding="UTF-8"?>  
     2 <beans  
     3 xmlns="http://www.springframework.org/schema/beans"  
     4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
     5 xmlns:context="http://www.springframework.org/schema/context"  
     6 xsi:schemaLocation="  
     7 http://www.springframework.org/schema/beans        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
     8 http://www.springframework.org/schema/context                http://www.springframework.org/schema/context/spring-context-3.0.xsd">  
     9 
    10 
    11      
    12     <!-- 测试1  使用set注入方法       注入集合类型   Spring不仅能注入简单类型数据,还能注入集合(Collection、无序集合Set、有序集合List)类型、数组(Array)类型、字典(Map)类型数据、Properties类型数据 -->
    13     <bean id="listBean" class="com.it.dao.impl.SayHelloImpl4">
    14         <property name="listValue">
    15             <list><!-- <list value-type="java.lang.String"> 如果是set集合就是用<set>标签,可以在此指定泛型,若不指定,则默认是String类型的 -->
    16                 <value>德玛西亚</value>
    17                 <value>艾欧尼亚</value>
    18                 <value>暗影岛</value>
    19                 <value>德莱文明</value>
    20             </list>
    21         </property>
    22     </bean>
    23     
    24     <!-- Spring容器目前能对各种基本类型把配置的String参数转换为需要的类型,
    25             如果转换出错,将抛出相应的异常,
    26             默认的<value>中间的值均是String类型的
    27          Spring类型转换系统对于boolean类型进行了容错处理,除了可以使用“true/false”标准的Java值进行注入,还能使用“yes/no”、“on/off”、“1/0”来代表“真/假” -->
    28     
    29     <!-- 测试2 使用set注入方法     map -->
    30     <bean id="mapBean" class="com.it.dao.impl.SayHelloImpl4">
    31         <property name="mapValue">
    32             <map key-type="java.lang.String" value-type="java.lang.Integer">
    33                 <entry>
    34                     <key>
    35                         <value>1</value>
    36                     </key>
    37                     <value>
    38                         123
    39                     </value>
    40                 </entry>
    41                 <entry key="2" value="456"></entry>
    42                 <entry key="3" value="789"></entry>
    43             </map>
    44         </property>
    45     </bean>
    46     
    47     
    48     <!-- 测试3  一个bean注入到另一个bean中 -->
    49     <bean id="intBean" class="com.it.dao.impl.SayHelloImpl4">
    50         <property name="fistBleed">
    51             <value type="java.lang.Integer">998</value>
    52         </property>
    53     </bean>
    54     
    55     <bean id="mapBean2" class="com.it.dao.impl.SayHelloImpl4">
    56         <property name="firstMap">
    57             <map key-type="java.lang.String" value-type="com.it.test.SayHelloImpl4">
    58                 <entry>
    59                     <key>
    60                         <value>1</value>
    61                     </key>
    62                     <ref bean="intBean"/>
    63                     
    64                 </entry>
    65             </map>
    66         </property>
    67     </bean>
    68     
    69     
    70     
    71     
    72     
    73 </beans>  
    View Code

    实现层 测试类:SayHelloTest4.java

     1 package com.it.test;
     2 
     3 import org.junit.Test;
     4 import org.springframework.context.ApplicationContext;
     5 import org.springframework.context.support.FileSystemXmlApplicationContext;
     6 
     7 import com.it.dao.SayHell;
     8 import com.it.dao.impl.SayHelloImpl4;
     9 
    10 public class SayHelloTest4 {
    11     
    12     ApplicationContext applicationContext =new FileSystemXmlApplicationContext("resources/setList.xml");
    13     @Test
    14     public void testListSet(){
    15         SayHell sayHell =  applicationContext.getBean("listBean",SayHell.class);
    16         sayHell.sayHello();
    17         
    18         SayHelloImpl4 sayHell4 =  (SayHelloImpl4) applicationContext.getBean("mapBean");
    19         sayHell4.sayMapGood();
    20         
    21         
    22         SayHelloImpl4 sayHell42 =  (SayHelloImpl4) applicationContext.getBean("mapBean2");
    23         sayHell42.sayMapGood2();
    24     }    
    25 }    
    View Code

    实现效果:

  • 相关阅读:
    HanTTS简单文档
    一张包含所有颜色的图片
    shiro整合SpringMVC基于xml
    设计模式-享元模式
    关于volatile关键字实现的个人理解
    重量级锁
    轻量级锁
    偏向锁
    Seata概念的总结
    苹果手机怎么把资源库的APP放到桌面
  • 原文地址:https://www.cnblogs.com/sxdcgaq8080/p/5680101.html
Copyright © 2011-2022 走看看