• 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>

    运行结果图

  • 相关阅读:
    网站搜索功能lucene
    RabbitMQ消息队列
    zookeeper
    RPC+SOA+dubbo
    石英定时任务-quartz
    通用mapper、图片上传、nginx
    通用mapper和分类实现
    后台商品管理功能实现
    构建框架
    海量数据的并发处理
  • 原文地址:https://www.cnblogs.com/qzhc/p/11255501.html
Copyright © 2020-2023  润新知