• spring注入不同数据类型


    spring注入不同数据类型

    1.先来个实体类,里面包含各种数据类型,其他代码参考spring框架IOC设值注入

      1 package com.domain;
      2 
      3 import java.util.List;
      4 import java.util.Map;
      5 import java.util.Properties;
      6 import java.util.Set;
      7 
      8 /**
      9  * 
     10  * @author Mr
     11  * 各种各样的数据类型
     12  */
     13 public class TestEntity {
     14     //特殊的类型1
     15     private String specialCharacter1;
     16     //特殊的类型2
     17     private String specialCharacter2;
     18     //javaBean类型
     19     private User innerBean;
     20     //List类型
     21     private List<String> list;
     22     //数组类型
     23     private String [] array;
     24     //Set集合类型
     25     private Set<String> set;
     26     //Map类型
     27     private Map<String, String> map;
     28     //properties
     29     private Properties props;
     30     //空字符串
     31     private String emptyValue;
     32     //空字符串
     33     private String nullValue="init value";
     34     public String getSpecialCharacter1() {
     35         return specialCharacter1;
     36     }
     37     public void setSpecialCharacter1(String specialCharacter1) {
     38         this.specialCharacter1 = specialCharacter1;
     39     }
     40     public String getSpecialCharacter2() {
     41         return specialCharacter2;
     42     }
     43     public void setSpecialCharacter2(String specialCharacter2) {
     44         this.specialCharacter2 = specialCharacter2;
     45     }
     46     public User getInnerBean() {
     47         return innerBean;
     48     }
     49     public void setInnerBean(User innerBean) {
     50         this.innerBean = innerBean;
     51     }
     52     public List<String> getList() {
     53         return list;
     54     }
     55     public void setList(List<String> list) {
     56         this.list = list;
     57     }
     58     public String[] getArray() {
     59         return array;
     60     }
     61     public void setArray(String[] array) {
     62         this.array = array;
     63     }
     64     public Set<String> getSet() {
     65         return set;
     66     }
     67     public void setSet(Set<String> set) {
     68         this.set = set;
     69     }
     70     public Map<String, String> getMap() {
     71         return map;
     72     }
     73     public void setMap(Map<String, String> map) {
     74         this.map = map;
     75     }
     76     public Properties getProps() {
     77         return props;
     78     }
     79     public void setProps(Properties props) {
     80         this.props = props;
     81     }
     82     public String getEmptyValue() {
     83         return emptyValue;
     84     }
     85     public void setEmptyValue(String emptyValue) {
     86         this.emptyValue = emptyValue;
     87     }
     88     public String getNullValue() {
     89         return nullValue;
     90     }
     91     public void setNullValue(String nullValue) {
     92         this.nullValue = nullValue;
     93     }
     94     
     95     //来个方法显示相关
     96     public void showValue(){
     97         
     98         System.out.println("特殊的类型1:"+this.specialCharacter1);
     99         System.out.println("特殊的类型1:"+this.specialCharacter2);
    100         System.out.println("javaBean类型:"+this.innerBean.getUname());
    101         System.out.println("List类型:"+this.list);
    102         System.out.println("数组类型:"+this.array[0]);
    103         System.out.println("Set集合类型:"+this.set);
    104         System.out.println("Map类型:"+this.map);
    105         System.out.println("properties类型:"+this.props);
    106         System.out.println("空字符串类型:"+this.emptyValue);
    107         System.out.println("空字符串类型:"+this.nullValue);
    108     }
    109     
    110 }

    2.写spring配置文件

     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:p="http://www.springframework.org/schema/p"
     5     xmlns:aop="http://www.springframework.org/schema/aop"
     6     xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
     7         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">
     8     <!-- 各种各样的数据类型 -->
     9     <bean id="entitys" class="com.domain.TestEntity">
    10         <!-- 特殊的类型1 -->
    11         <property name="specialCharacter1">
    12             <value><![CDATA[P&G]]></value>
    13         </property>
    14         <!-- 特殊的类型2 -->
    15         <property name="specialCharacter2">
    16             <value>P&amp;G</value>
    17         </property>
    18         <!-- 实体类 -->
    19         <property name="innerBean">
    20             <!-- 不要写id 但要写class-->
    21             <bean class="com.domain.User">
    22                 <property name="uname">
    23                     <value>张三丰</value>
    24                 </property>
    25             </bean>
    26         </property>
    27         <!-- list类型 -->
    28         <property name="list">
    29             <list>
    30             <value>乒乓球</value>
    31             <value>排球球</value>
    32             <value>羽毛球</value>
    33             </list>
    34         </property>
    35         <!-- 数组类型 -->
    36         <property name="array">
    37             <list>
    38             <value>苹果</value>
    39             <value>香蕉</value>
    40             </list>
    41         </property>
    42         <!-- Set集合类型 -->
    43         <property name="set">
    44         <list>
    45             <value>电视机</value>
    46             <value>冰柜</value>
    47         </list>
    48         </property>
    49         <!-- Map类型  键值对的形式-->
    50         <property name="map">
    51             <map>
    52                 <entry>
    53                     <key>
    54                     <!-- 这是key的名字 -->
    55                         <value>football</value>
    56                     </key>
    57                     <!-- 这是key对应的值 -->
    58                     <value>足球</value>
    59                 </entry>
    60                 <entry>
    61                     <key>
    62                         <value>basketball</value>
    63                     </key>
    64                     <value>蓝球</value>
    65                 </entry>
    66             </map>
    67         </property>
    68         <!-- properties -->
    69         <property name="props">
    70         <props>
    71             <prop key="beijing">北京</prop>
    72             <prop key="shanghai">上海</prop>
    73         </props>
    74         </property>
    75         <!-- 空字符串  返回空 没有任何东西-->
    76         <property name="emptyValue">
    77             <value></value>
    78         </property>
    79         <!-- 空字符串 返回null -->
    80         <property name="nullValue">
    81         <null></null>
    82         </property>
    83     </bean>
    84     
    85     
    86     
    87 </beans>

    3.写测试类

     1 package com.test;
     2 
     3 import org.springframework.context.ApplicationContext;
     4 import org.springframework.context.support.ClassPathXmlApplicationContext;
     5 
     6 import com.biz.IUserBiz;
     7 import com.domain.TestEntity;
     8 import com.domain.User;
     9 
    10 /**
    11  * 
    12  * @author Mr
    13  * aop测试类
    14  */
    15 public class Test {
    16 
    17     public static void main(String[] args) {
    18         //解析配置文件
    19         ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
    20         
    21         TestEntity te = (TestEntity) ac.getBean("entitys");
    22         te.showValue();
    23     }
    24 
    25 }

    4.效果图

  • 相关阅读:
    numpy库:常用基本
    高考的结束,新的开始
    Hello World!
    第一篇随笔
    Linux命令之文件与用户权限
    看山不是山,看水不是水
    Python基础篇【第1篇】: Python基础
    css居中
    JS正则表达式(一)
    小问题总结
  • 原文地址:https://www.cnblogs.com/myhzb/p/7536631.html
Copyright © 2020-2023  润新知