• spring知识点


    (3) Spring的组成

        ① Spring Core:核心容器,BeanFactory提供了组件生命周期的管理,组件的创建,装配,销毁等功能

        SpringContext:ApplicationContext,扩展核心容器,提供事件处理、国际化等功能。它提供了一些企业级服务的功能,提供了JNDI,EJB,RMI的支持。

        ② Spring AOP:提供切面支持

        ③ Spring DAO:提供事务支持,JDBC,DAO支持

        ④ Spring ORM:对流行的O/R Mapping封装或支持

        ⑤ Spring Web:提供Web应用上下文,对Web开发提供功能上的支持,如请求,表单,异常等。

        ⑥ Spring Web MVC:全功能MVC框架,作用等同于Struts。

    Spring可以对集合类型进行注入包括:Set集合,properties属性集合,Map集合以及List集合

    注入方式如下:

    package com.test;  
      
    import java.util.ArrayList;  
    import java.util.HashMap;  
    import java.util.HashSet;  
    import java.util.Map;  
    import java.util.Properties;  
    import java.util.Set;  
    import java.util.List;  
      
    public class UserServiceImplement implements IUserService {  
      
        public Set<String> getS() {  
            return s;  
        }  
      
        public void setS(Set<String> s) {  
            this.s = s;  
        }  
      
        public Map<String, String> getM() {  
            return m;  
        }  
      
        public void setM(Map<String, String> m) {  
            this.m = m;  
        }  
      
        public Properties getP() {  
            return p;  
        }  
      
        public void setP(Properties p) {  
            this.p = p;  
        }  
      
        public List<String> getL() {  
            return l;  
        }  
      
        public void setL(List<String> l) {  
            this.l = l;  
        }  
      
        private Set<String> s = new HashSet<String>();  
        private Map<String, String> m = new HashMap<String, String>();  
        private Properties p = new Properties();  
        private List<String> l = new ArrayList<String>();  
      
        public void saveUser() {  
            System.out.println("Set集合注入");  
            for (String str : s) {  
                System.out.println(str);  
            }  
      
            System.out.println("------------------------------");  
            System.out.println("Map集合注入");  
            for (String str : m.values()) {  
                System.out.println(str);  
            }  
      
            System.out.println("------------------------------");  
            System.out.println("Properties属性集合注入");  
            for (Object str : p.values()) {  
                System.out.println(str);  
            }  
      
            System.out.println("------------------------------");  
            System.out.println("List集合注入");  
            for (String str : l) {  
                System.out.println(str);  
            }  
        }  
    } 

     要注意的是:这些集合属性也必须要有对应的setter方法

    <?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:context="http://www.springframework.org/schema/context"  
        xsi:schemaLocation="http://www.springframework.org/schema/beans   
               http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  
               http://www.springframework.org/schema/context  
               http://www.springframework.org/schema/context/spring-context-2.5.xsd">  
        <context:annotation-config />  
      
        <bean id="userservice" class="com.test.UserServiceImplement">  
            <property name="s">  
                <set>  
                    <value>SetValue1</value>  
                    <value>SetValue2</value>  
                    <value>SetValue3</value>  
                </set>  
            </property>  
      
            <property name="m">  
                <map>  
                    <entry key="MapKey1" value="MapValue1"></entry>  
                    <entry key="MapKey2" value="MapValue2"></entry>  
                    <entry key="MapKey3" value="MapValue3"></entry>  
                </map>  
            </property>  
      
            <property name="p">  
                <props>  
                    <prop key="PropertiesKey1">PropertiesValue1</prop>  
                    <prop key="PropertiesKey2">PropertiesValue2</prop>  
                    <prop key="PropertiesKey3">PropertiesValue3</prop>  
                </props>  
            </property>  
      
            <property name="l">  
                <list>  
                    <value>ListValue1</value>  
                    <value>ListValue2</value>  
                    <value>ListValue3</value>  
                </list>  
            </property>  
        </bean>  
    </beans>  

    测试类:

    package com.test;  
      
    import org.springframework.context.ApplicationContext;  
    import org.springframework.context.support.ClassPathXmlApplicationContext;  
      
    public class Test {  
      
        public static void main(String[] args) {  
            ApplicationContext ctx = new ClassPathXmlApplicationContext(  
                    "com/test/bean.xml");  
            IUserService us = (IUserService) ctx.getBean("userservice");  
            us.saveUser();  
        }  
    }  

     摘要: 我们常在Spring的Bean中注入各种基本类型的值和对象引用,如果需要注入List,Map,Set和数组等集合类型时,又该如何配置呢?

    先定义一个测试类,由于本文将要介绍注入各种集合时如何配置,故这个类包含各种集合,类名和属性名不好取,没有特殊含义:

    public class Test {
        private List<String> listTest;
        private Map<String, Object> mapTest;
        private Set setTest;
        private String[] arrayTest;
        private Properties propertiesTest;
        //下面是各个属性的setter,此处省略
        //......
    }

        Test类中,分别定义了List,Map,Set,Array等5种集合类型的属性,下面在Spring配置文件中,分别为这些类型的属性注入值:

    <bean id="test" class="com.abc.Test">
        <!-- List类型的属性 -->
        <property name="listTest">
            <!-- 为list类型的属性注入值时,使用<list>元素 -->
            <list>
                <!-- 只要类型满足,每个<value>,<ref>,<bean>都可以配置一个list的元素 -->
                <value>ListA</value>
                <value>ListB</value>
                <value>ListC</value>
            </list>
        </property>
        
        <!-- Map类型的属性 -->
        <property name="mapTest">
            <!-- 为map类型的属性注入值时,使用<map>元素 -->
            <map>
                <!-- 每一个<entry>都为<map>配置一个K-V对,同样, -->
                <entry key="key1" value="value1" />
                <!-- 下面这个value指向了在此Spring文件中定义的另一个叫object的Bean -->
                <entry key="key2" value-ref="object" />
            </map>
        </property>
        
        <!-- Set类型的属性 -->
        <property name="setTest">
            <!-- 为set类型的属性注入值时,使用<set>元素 -->
            <set>
                <!-- 只要类型满足,每个<value>,<ref>,<bean>都可以配置一个list的元素 -->
                <value>SetA</value>
                <!-- 下面是一个嵌套Bean的定义。关于什么是嵌套Bean,请看:http://my.oschina.net/itblog/blog/204378 -->
                <bean class="com.abc.OtherBean1" />
                <!-- 下面引用了此Spring文件中定义的另一个Bean -->
                <ref local="com.abc.OtherBean2" />
            </set>
        </property>
        
        <!-- Properties类型的属性 -->
        <property name="propertiesTest">
            <props>
                <!-- 每个<prop>元素指定一个属性项,其中key指定属性名 -->
                <prop key="prop1">value1</prop>
                <prop key="prop2">value2</prop>
            </props>
        </property>
        
        <!-- 数组类型的属性 -->
        <property name="arrayTest">
            <!-- 为数组类型的属性注入值时,使用<list>元素 -->
            <list>
                <!-- 只要类型满足,每个<value>,<ref>,<bean>都可以配置一个list的元素 -->
                <value>Array1</value>
                <bean class="com.abc.OtherBean1" />
                <ref local="com.abc.OtherBean2" />
            </list>
        </property>
    </bean>
  • 相关阅读:
    sqlserver 数据查询效率优化
    上海亲戚朋友游套餐
    C# List集合去重操作注意点
    一个高级开发的基本工作职责和能力要求
    对于程序员的经验能力和薪资待遇基本概括
    一个项目团队的最低配置
    【算法学习笔记】51. 贪心法 区间排序问题 SJTU OJ 1360 偶像丁姐的烦恼
    【算法学习笔记】50.字符串处理 SJTU OJ 1361 丁姐的周末
    【算法学习笔记】49.暴力穷举 BFS 剪枝 SJTU OJ 1357 相邻方案
    【算法学习笔记】48.递归/显式栈 SJTU OJ 1358 分割树
  • 原文地址:https://www.cnblogs.com/panxuejun/p/5898526.html
Copyright © 2020-2023  润新知