欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

淺談Spring Context加載方式

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

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開始,開始使用注解的方式來進(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();
    // 通過名稱的方式獲取相應(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,就是以這種方式來初始化上下文內(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的方式來配置初始化參數(shù),不過這次里要用基于注解的類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)文章

  • Idea中Springboot熱部署無效問題解決

    Idea中Springboot熱部署無效問題解決

    這篇文章主要介紹了Idea中Springboot熱部署無效問題解決,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-11-11
  • Java中BigDecimal使用注意避坑指南

    Java中BigDecimal使用注意避坑指南

    Java在java.math包中提供的API類BigDecimal,用來對(duì)超過16位有效位的數(shù)進(jìn)行精確的運(yùn)算,下面這篇文章主要給大家介紹了關(guān)于Java中BigDecimal使用注意避坑的相關(guān)資料,需要的朋友可以參考下
    2023-02-02
  • 配置IDEA中java項(xiàng)目配置swagger全過程

    配置IDEA中java項(xiàng)目配置swagger全過程

    這篇文章主要介紹了配置IDEA中java項(xiàng)目配置swagger全過程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-05-05
  • Java8使用Stream流實(shí)現(xiàn)List列表查詢、統(tǒng)計(jì)、排序以及分組

    Java8使用Stream流實(shí)現(xiàn)List列表查詢、統(tǒng)計(jì)、排序以及分組

    List的Stream流操作可以簡(jiǎn)化我們的代碼,減少程序運(yùn)行的壓力,應(yīng)對(duì)上面的問題,下面這篇文章主要給大家介紹了關(guān)于Java8使用Stream流實(shí)現(xiàn)List列表查詢、統(tǒng)計(jì)、排序以及分組的相關(guān)資料,需要的朋友可以參考下
    2023-06-06
  • Mybatis的特點(diǎn)及優(yōu)點(diǎn)

    Mybatis的特點(diǎn)及優(yōu)點(diǎn)

    Mybatis 本是apache的一個(gè)開源項(xiàng)目iBatis, 2010年這個(gè)項(xiàng)目由apache software foundation 遷移到了google code,并且改名為MyBatis。mybatis有哪些特點(diǎn)和優(yōu)點(diǎn)呢?通過本文一起學(xué)習(xí)吧
    2016-12-12
  • JPA配置詳解之jpaProperties用法

    JPA配置詳解之jpaProperties用法

    這篇文章主要介紹了JPA配置詳解之jpaProperties用法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-11-11
  • JAVA語法糖原理你知道嗎

    JAVA語法糖原理你知道嗎

    語法糖(Syntactic sugar),也叫做糖衣語法,是英國(guó)科學(xué)家發(fā)明的一個(gè)術(shù)語,通常來說使用語法糖能夠增加程序的可讀性,從而減少程序代碼出錯(cuò)的機(jī)會(huì).這篇文章主要介紹了Java 中的語法糖知識(shí),需要的朋友可以參考下
    2021-09-09
  • Java之類加載機(jī)制案例講解

    Java之類加載機(jī)制案例講解

    這篇文章主要介紹了Java之類加載機(jī)制案例講解,本篇文章通過簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-07-07
  • Linux中JDK安裝配置教程

    Linux中JDK安裝配置教程

    這篇文章主要為大家詳細(xì)介紹了Linux中JDK安裝配置教程,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-02-02
  • java 鍵盤輸入一個(gè)數(shù),輸出數(shù)組中指定元素的示例

    java 鍵盤輸入一個(gè)數(shù),輸出數(shù)組中指定元素的示例

    今天小編就為大家分享一篇java 鍵盤輸入一個(gè)數(shù),輸出數(shù)組中指定元素的示例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2018-07-07

最新評(píng)論