zoukankan      html  css  js  c++  java
  • Spring各种类型数据的注入

    直接上代码:

    一个MessageBean类

    package com.henu.spring;
    import java.util.*;
    public class MessageBean {
    
        private String username; //用户名
        private String fileDir;  //上传路径
        private Set<String> types;//允许上传类型
        private List<String> hbms;
        private Set<String> city;
        private Map<String, String> books;
        private Properties props;
        
        //注入一个字符串,分析之后给其赋值
        public void setTypes(String str){
            String[] arr = str.split(",");
            types = new HashSet<String>();
            for (String string : arr) {
                types.add(string);
            }
        }
        
        public void setUsername(String username) {
            this.username = username;
        }
        public void setFileDir(String fileDir) {
            this.fileDir = fileDir;
        }
        public void setCity(Set<String> city) {
            this.city = city;
        }
        public void setHbms(List<String> hbms) {
            this.hbms = hbms;
        }
        public void setBooks(Map<String, String> books) {
            this.books = books;
        }    
        public void setProps(Properties props) {
            this.props = props;
        }
        
        public void show(){
            System.out.println("用户名:" + username);
            System.out.println("上传路径" + fileDir);
            
            System.out.println("--hbm文件如下--");
            for (String string : hbms) {
                System.out.println(string);
            }
            
            System.out.println("--city如下--");
            for (String string : city) {
                System.out.println(string);
            }
            
            System.out.println("--图书信息显示--");
            Set set = books.keySet();
            for (Object object : set) {
                System.out.println(object+"   "+books.get(object));
            }
            
            System.out.println("--props参数如下--");
            Set<String> key = props.stringPropertyNames();
            Iterator<String> iterator = key.iterator();
            while(iterator.hasNext()){
                String string = (String) iterator.next();
                System.out.println(string+"   "+props.getProperty(string));
            }
            
            System.out.println("--允许上传文件类型--");
            for (String string : types) {
                System.out.println(string);
            }
        }
    }

    一个测试类

    /**
     * 
     */
    package com.henu.spring;
    
    import org.junit.Test;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    /**
     * @author Administrator
     *
     */
    public class TestInjection {
        @Test
        public void test1(){
            ApplicationContext context = 
                new ClassPathXmlApplicationContext("/applicationContext.xml");
            MessageBean messageBean = (MessageBean) context.getBean("messageBean");
            messageBean.show();
        }
    }

    一个配置文件

    <?xml version="1.0" encoding="UTF-8"?>
    <beans
    xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-3.1.xsd">
           
            <!-- 各种数据类型的注入 -->
            <bean id="messageBean" class="com.henu.spring.MessageBean">
                <property name="username" value="root"></property>
                <property name="fileDir" value="D:\images"></property>
                <property name="types" value="jdp,gif,ipeg"></property>
                
                <property name="hbms">
                    <list>
                        <value>lengzuai nei hou !</value>
                         <value>lengnei nei hou !</value>
                    </list>
                </property>
                <property name="city">
                    <set>
                        <value>郑州</value>
                         <value>开封</value>
                    </set>
                </property>
                <property name="books">
                    <map>
                        <entry key="1" value="红楼梦"></entry>
                        <entry key="2" value="三国演义"></entry>
                        <entry key="3" value="西游记"></entry>
                        <entry key="4" value="水浒传"></entry>
                    </map>
                </property>
                <property name="props">
                    <props>
                        <prop key="hibernate.show_sql">
                            <!-- value -->
                            true
                        </prop>
                        <prop key="hibernate.format_sql">
                            true
                        </prop>
                        <prop key="hibernate.hibernate_sql">
                            true
                        </prop>
                    </props>
                </property>
            </bean>
            </beans>

    运行结果图

  • 相关阅读:
    Redis学习笔记(十四)Sentinel(哨兵)(上)
    Redis学习笔记(十三) 复制(下)
    Redis学习笔记(十二) 复制(上)
    搞懂推荐系统中的评价指标NDCG(CG、DCG、IDCG)
    pytorch入门3.2构建分类模型再体验(批处理)
    pytorch入门3.1构建分类模型再体验(模型和训练)
    pytorch入门3.0构建分类模型再体验(准备数据)
    一条一条的读《推荐系统实战》2.0
    pytorch入门2.2构建回归模型初体验(开始训练)
    pytorch入门2.1构建回归模型初体验(模型构建)
  • 原文地址:https://www.cnblogs.com/qzhc/p/11255501.html
Copyright © 2011-2022 走看看