• Spring中使用要点集合


    1、InitializingBean和init-method方法
    Spring的InitializingBean为bean提供了定义初始化方法的方式。InitializingBean是一个接口,它仅仅包含一个方法:afterPropertiesSet()。实现这个接口,在afterPropertiesSet()中编写初始化代码。
    使用Spring提供的init-method的功能来执行一个bean 子定义的初始化方法。写一个java class,这个类不实现任何Spring的接口。定义一个没有参数的方法init(),然后配置一下<bean id="init_class_2" class="com.netease.test.spring.init.InitClass2" init-method="init”/>
    使用第一种则会加入Spring的侵入,第二种方式使用反射的方式去实现,性能较差一丢丢。建议用第二种。

    2、集成spring到web项目中
    针对maven建的web项目
    首先添加maven依赖到pom文件中,大部分情况下spring-webmvc模块即可,其他的context、oxm、core等都会被加进来:

    <properties>
          <spring.version>3.2.0.RELEASE</spring.version>
    </properties>
    <!--spring modules-->
      <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>${spring.version}</version>
      </dependency>


    第一种方式:使用ContextLoaderListener,在web.xml中加入如下配置
        

        <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>
                classpath:applicationContext.xml</param-value>
        </context-param>
        <listener>
            <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
        </listener>


    第二种方式:使用DispatcherServlet,在web.xml中加入配置

         <servlet>
            <servlet-name>springmvc</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <init-param>
                <param-name>contextConfigLocation</param-name>
                <param-value>classpath:springmvc-servlet.xml</param-value>
            </init-param>
            <load-on-startup>1</load-on-startup>
        </servlet>
        <servlet-mapping>
            <servlet-name>springmvc</servlet-name>
            <url-pattern>/</url-pattern>
        </servlet-mapping>

    注:多个spring配置中间用英文的,号隔开

  • 相关阅读:
    java.lang.NoSuchMethodError: org.springframework.core.io.ResourceEditor错误
    http://blog.sina.com.cn/s/blog_6145ed810102vr8k.html
    异或巧用:Single Number
    Highcharts:X轴分组堆叠图
    Vs2012在Linux开发中的应用(5):项目属性的定义
    BZOJ 1005 明明的烦恼 Prufer序列+组合数学+高精度
    Python 点滴 I
    easyUI 验证控件应用、自己定义、扩展验证 手机号码或电话话码格式
    InnoDB: Error: io_setup() failed with EAGAIN after 5 attempts
    Java设计模式-设计模式的六种原则
  • 原文地址:https://www.cnblogs.com/nexiyi/p/spring_app_point.html
Copyright © 2020-2023  润新知