最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

淺談Spring Context加載方式

 更新時(shí)間:2018年05月30日 14:44:31   作者:David_jim  
這篇文章主要介紹了淺談Spring Context加載方式,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧

Spring 加載方式

對(duì)于可執(zhí)行文件方式,我們一般的加載Spring 配置的方式是

ClassPathXmlApplicationContext

 public static void main(String[] args) {
    ClassPathXmlApplicationContext xmlApplicationContext = new ClassPathXmlApplicationContext("classpath:spring-context.xml");
    DemoService demoService = (DemoService) xmlApplicationContext.getBean("demoService");
    String text = demoService.hello();
    System.out.println(text);
  }
<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-3.2.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.2.xsd"
    default-autowire="byName" default-lazy-init="false">

  <!-- 采用注釋的方式配置bean -->
  <context:annotation-config/>
  <!-- 配置要掃描的包 -->
  <context:component-scan base-package="com.jin.lesson.context"/>
</beans>

從spring 3.0開(kāi)始,開(kāi)始使用注解的方式來(lái)進(jìn)行spring 配置的注冊(cè)

 public static void main(String[] args) {
    AnnotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext();
    // 告訴要掃描的包,通常是應(yīng)用的根目錄的Application類
    annotationConfigApplicationContext.scan(Main.class.getPackage().getName());
    // 刷新上下文,使用得相應(yīng)的bean注冊(cè)成功
    annotationConfigApplicationContext.refresh();
    // 通過(guò)名稱的方式獲取相應(yīng)的DemoService
    DemoService demoService = (DemoService) annotationConfigApplicationContext.getBean("demoService");
    String text = demoService.hello();
    System.out.println(text);
  }

demoService是定義的一個(gè)Service的名稱,xml配置的方式也是可以設(shè)定好是否采用注解的方式進(jìn)行掃描,如1中的

<context:annotation-config/>

demoService 很簡(jiǎn)單,如下的方式

@Service(value = "demoService")
public class DemoService {
  public String hello() {
    return "hello world";
  }
}

Web應(yīng)用的初始化

  1. web.xml配置方式
  2. 注解的方式

web.xml 配置方式

利用spring 自帶的Servlet 進(jìn)行初始注冊(cè)

  <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:spring/spring-context.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
    <async-supported>true</async-supported>
  </servlet>
  <servlet-mapping>
    <servlet-name>SpringMVC</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>

利用 Listener進(jìn)行注冊(cè) ,像Spring+structs,就是以這種方式來(lái)初始化上下文內(nèi)容的

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

注解的方式

也是利用Servlet的方式來(lái)配置初始化參數(shù),不過(guò)這次里要用基于注解的類AnnotationConfigWebApplicationContext,同時(shí)要注冊(cè)Servlet

 @Override
  public void onStartup(ServletContext servletContext) throws ServletException {
    ServletRegistration.Dynamic dispatcher = servletContext.addServlet("dispatcher", DispatcherServlet.class);
    dispatcher.setInitParameter("contextConfigLocation", getClass().getName());
    dispatcher.setInitParameter("contextClass", AnnotationConfigWebApplicationContext.class.getName());
    dispatcher.addMapping("/*");
    dispatcher.setLoadOnStartup(1);
  }

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論

澎湖县| 海盐县| 扶风县| 喜德县| 普兰店市| 阜阳市| 泽州县| 南投县| 万安县| 台州市| 阳江市| 莎车县| 宜兴市| 广饶县| 云南省| 扎赉特旗| 崇义县| 阿尔山市| 塔城市| 高陵县| 广丰县| 佳木斯市| 池州市| 安福县| 江达县| 治县。| 哈尔滨市| 澄迈县| 合肥市| 平陆县| 东兰县| 阳高县| 浪卡子县| 北辰区| 黑山县| 叶城县| 迁安市| 镇赉县| 介休市| 翁源县| 长白|