• spring 安装


    引用:http://hi.baidu.com/y0h0001/item/8790ca2a144dd4a4af48f517

    2 Spring的安装
        解压spring-framework-2.5.6-with-dependencies.zip文件,在dist目录下有一个spring.jar文件,在modules目录下有很多jar文件,一般可以把dist目录中的spring.jar文件或dist/modules目录中的全部jar文件复制到Web项目的WEB-INF/lib目录中,也可以只复制spring.jar文件,因为spring.jar文件中已经包含了所有支持功能所需要的类,而不必再加入个别的jar文件。

        spring.jar是包含有完整发布的单个jar文件,spring.jar中包含了除spring-mock.jar里所包含的内容外的所有jar包的内容,因为只有在开发环境下才会使用sprint-mock.jar来进行辅助测试。正式系统不使用这些类。

        除了spring.jar文件外,Spring还包含13个独立的jar文件,各个独立的jar文件对应Spring的各个组件,用户可以根据自己的需要来组合适合自己的jar包,而不必要引入整个spring.jar文件。

        1.spring-core.jar
        这个jar文件包含了Spring框架最基本的核心工具类,Spring的其它组件都要用到这个包里的类,因此它是其它组件的基本核心,当然,用户也可以在自己的应用系统中使用这些工具类。

        2.spring-aop.jar
        这个文件包含了Spring AOP编程的支持类,如果采用AOP编程,必须使用此文件。

        3.spring-beans.jar
        Spring设计的核心是org.springframework.beans包,其设计目的是与JavaBean组件一起协同使用。

        4.spring-webmvc.jar
        Spring对于Web应用开发的MVC模式的支持类。

    16.2.3 Spring的配置
        Spring配置任何基于J2EE的Web应用很简单,只需要简单地添加Spring的ContextLoaderListener到Web 应用的WEB-INF/web.xml文件中,代码如下:

        <listener>
            <listener-class>
                 org.springframework.web.context.ContextLoaderListener
            </listener-class>
        </listener>


        这是一个Servlet监听器,它会在Web应用启动时自动被启动,然后根据默认的配置文件(applicationContext.xml)来创建Spring框架的上下文环境(运行环境),在默认的情况下,它会查找WEB-INF/applicationContext.xml配置文件,也可以用contextConfigLocation参数来指定其它的配置文件,代码如下:

        <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/classes/bean.xml,/WEB-INF/classes/DBContext.xml</param-value>
        </context-param>
        <listener>
            <listener-class>
                 org.springframework.web.context.ContextLoaderListener
            </listener-class>
        </listener>


        <param-value>元素是用空格或逗号分隔的一系列路径。Spring框架的配置使用了这个listener和默认的contextConfigLocation参数,Spring让依赖性的绑定变的非常简单,applicationContext.xml文件的基本内容如下:

    <?xml version="1.0" encoding="gb2312" ?>
    <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
    "http://www.springframework.org/dtd/spring-beans.dtd">
    <beans>
        使用<bean>元素配置Bean对象
    </beans>


        在IoC容器中拼装JavaBean叫做装配Bean,装配Bean的核心步骤就是告诉IoC容器你需要什么样的Bean,以及IoC容器如何使用依赖注入将它们配合在一起。

        理论上,Bean装配可以从任何资源获得,包括属性文件、XML文件、关系数据库等,但XML文件是最常用的Spring应用系统配置源。Spring中的几种对象工厂都支持使用XML来装配Bean,包括XmlBeanFactory、ClassPathXmlApplicationContext、FileSystemXmlApplicationContext、XmlWebApplicationContext等。

        基本的 Spring 配置包括下面几个方面:

        1.添加一个<bean>元素。
        2.设置<bean>元素的属性:
            (1)手动设置:通过setter方法或者构造函数。
            (2)自动设置:由IoC容器自己设置。

        其中bean的属性即为bean里的成员变量(bean的setter方法),这些成员变量的值可以通过setter方法来设置,例如,某个属性为name,则setter方法为setName(String name),或者通过构造函数来设置属性的值。setter方法和构造函数都可以在XML文件中进行配置,从而让Spring容器来自动装配bean。

        1.添加一个Bean
        使用<bean>元素添加一个bean,下面是一个例子:

        <beanid="mybean"class="包名.类名"singleton="false"init-method="initMethod"
              destroy-method="destroy-method"autowire="自动类型">
        </bean>

        id:该bean的名字,将来在程序中使用context.getBean("mybean")来得到这个<bean>元素定义的Bean的一个实例。这个属性是必须的。

        class:创建bean用的类,这个属性是必须的。

        singleton:默认为true,既单子模式,每次获取的都是同一个实例,如果设置为false,则为原型模式,每次获取的都是新创建的实例。这个属性不是必须的。

        init-method:在bean被实例化后要调用的方法。这个属性不是必须的。

        destroy-method:bean从容器中被删除之前要调用的方法,这个属性不是必须的。

        autowire:bean是否为自动装配,可以有多种自动装配方式,如果设置了这个属性,表示采用自动装配,没有设置这个属性表示手动装配。这个属性不是必须的。

        2.通过setter方法手动设置Bean的属性
        使用<property>元素为bean手动装配属性。

        (1)属性值是基本类型数据
            <beanid="mybean"class="包名.类名">
                <property name="属性名">
                    <value>属性的初值</value>
                </property>
            </bean>

        (2)引用其它bean(属性值是另外一个bean)
            <beanid="mybean"class="包名.类名"/>
            <beanid="mybean1"class="包名.类名">
                <property name="属性名">
                    <ref bean="mybean" />
                </property>
            </bean>

        也可以将 <ref bean> 修改为 <bean class="包名.类名"/>,但是结果有一点差异,使用<ref bean >时,得到的对象实例是经过Spring装配好的对象实例,而使用<bean class>得到的对象实例是没有经过Spring装配的对象实例。

        (3)下面是几种集合属性的装配:
        1).装配List类型的属性(为List类型的属性指定3个值)
            <property name="属性名">
                <list>
                    <value>值</value>
                    <ref bean="bean名"/>
                    <value>其它值</value>
                </list>
            </property>

        2).装配Set类型的属性(为Set类型的属性指定3个值)
            <property name="属性名">
                <set>
                    <value>值</value>
                    <ref bean="bean名"/>
                    <value>其它值</value>
                </set>
            </property>
        
        3).装配Map类型的属性(为Map类型的属性指定2键/值对)
            <property name="属性名">
                <map>
                    <entry key="键1">
                        <value>值1</value>
                    </entry>
                    <entry key="键2">
                        <ref bean="bean名"/>
                    </entry>
                </map>
            </property>

        4).装配Properties类型的属性(键/值对属性)
            <property name="属性名">
                <props>
                    <prop key="键1">值1</prop>
                    <prop key="键2">值2</prop>
                </props>
            </property>

        例题 16-1配置Bean属性
        JavaBean程序TestProperty.java

        package ch16;
        import java.util.*;
        public class TestProperty{
            private Properties prop;
            private List list;
            private Set set;
            private Map map;
            public void setProp(Properties prop) { this.prop = prop;}
            public void setList(List list) {this.list = list;}
            public void setSet(Set set) {this.set = set;}
            public void setMap(Map map) {this.map = map;}
            public Properties getProp() { return this.prop;}
            public List getList() {return this.list;}
            public Set getSet() {return this.set;}
            public Map getMap() {return this.map;}
        }

        在Spring框架中配置该JavaBean的XML代码如下:
        <bean id="myproperty" class="ch16.TestProperty">
            <property name="list">
                <list>
                    <value>王广发</value>
                    <value>李回回</value>
                </list>
            </property>
            <property name="set">
                <set>
                    <value>高中</value>
                    <value>大学</value>
                </set>
            </property>
            <property name="map">
                <map>
                    <entry key="name"><value>孙利</value></entry>
                    <entry key="age"><value>20</value></entry>
                </map>
            </property>
            <property name="prop">
                <props>
                    <prop key="address">中国北京</prop>
                    <prop key="id">001</prop>
                </props>
            </property>
        </bean>

        测试该配置是否成功的JSP页面TestProperty.jsp:
    <%@ page contentType="text/html;charset=gb2312"%>
    <%@ page import="org.springframework.context.ApplicationContext"%>
    <%@ page import="org.springframework.web.context.support.WebApplicationContextUtils"%>
    <%@ page import="ch16.*,java.util.*" %>
    <b>测试Spring配置</b>
    <%
    ApplicationContext context=WebApplicationContextUtils.getWebApplicationContext(application);
    TestProperty bean=(TestProperty)context.getBean("myproperty");

    List list=bean.getList();
    out.println("<br>List 属性的值<br>");
    for(int i=0;i<list.size();i++)
        out.println(list.get(i)+"<br>");

    Set set=bean.getSet();
    out.println("<br>Set 属性的值<br>");
    Iterator it=set.iterator();
    while(it.hasNext()) {
        out.println(it.next()+"<br>");
    }

    Map map=bean.getMap();
    out.println("<br>Map 属性的值<br>");
    it=map.keySet().iterator();
    while(it.hasNext()) {
        String key=(String)it.next();
        out.println(key+"="+map.get(key)+"<br>");
    }

    Properties prop=bean.getProp();
    out.println("<br>Properties 属性的值<br>");
    it=prop.keySet().iterator();
    while(it.hasNext()) {
        String key=(String)it.next();
        out.println(key+"="+prop.getProperty(key)+"<br>");
    }
    %>

        (4)设置null值
        将一个属性设置为null值,需要使用<null/>元素,如果不设置,则属性为默认值(在实例化时)而不是null值。
            <property name="属性名"><null/></property>

        3.通过构造函数手动设置Bean的属性
        当JavaBean类具有下面所示的构造函数时:

            package abc;
            public classMyBean{
                public MyBean(Stringarg1,MyBean1arg2,Stringarg3){}
            }

        可以在XML文件中这样配置该Bean:
            <beanid="mybean"class="abc.MyBean">
                <constructor-arg index="0">
                    <value>字符串值</value>
                </constructor-arg>
                <constructor-arg index="1">
                    <ref bean="bean名"/>
                </constructor-arg>
                <constructor-arg index="2">
                    <value>字符串值</value>
                </constructor-arg>
            </bean>

        其中,index属性是参数的序号,从左向右,从0开始。

  • 相关阅读:
    会话执行存储过程,等待被阻塞,Kill session场景模拟
    会话断开数据保存情况
    Linux关闭透明大页配置
    ORA16019搭建DG设置归档线程参数报错
    如何将openssl、uuid和crypto的库文件放到本地指定库目录
    cscope中这样生成cscope.files
    使用kdesvn完成tags和branch功能
    ubuntu中设定ibus自启动
    使用kdesvn提交文件出现Aborting commit:'.lcd1602.ko.cmd' remains in conflict错误提示
    error: dereferencing pointer to incomplete type的解决办法
  • 原文地址:https://www.cnblogs.com/sode/p/2690702.html
Copyright © 2020-2023  润新知