激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - 淺談Spring Context加載方式

淺談Spring Context加載方式

2021-05-06 11:33David_jim Java教程

這篇文章主要介紹了淺談Spring Context加載方式,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

spring 加載方式

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

classpathxmlapplicationcontext

?
1
2
3
4
5
6
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);
 }
?
1
2
3
4
5
6
7
8
9
10
11
12
13
<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開始,開始使用注解的方式來進行spring 配置的注冊

?
1
2
3
4
5
6
7
8
9
10
11
public static void main(string[] args) {
   annotationconfigapplicationcontext annotationconfigapplicationcontext = new annotationconfigapplicationcontext();
   // 告訴要掃描的包,通常是應用的根目錄的application類
   annotationconfigapplicationcontext.scan(main.class.getpackage().getname());
   // 刷新上下文,使用得相應的bean注冊成功
   annotationconfigapplicationcontext.refresh();
   // 通過名稱的方式獲取相應的demoservice
   demoservice demoservice = (demoservice) annotationconfigapplicationcontext.getbean("demoservice");
   string text = demoservice.hello();
   system.out.println(text);
 }

demoservice是定義的一個service的名稱,xml配置的方式也是可以設定好是否采用注解的方式進行掃描,如1中的

?
1
<context:annotation-config/>

demoservice 很簡單,如下的方式

?
1
2
3
4
5
6
@service(value = "demoservice")
public class demoservice {
  public string hello() {
    return "hello world";
  }
}

web應用的初始化

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

web.xml 配置方式

利用spring 自帶的servlet 進行初始注冊

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<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進行注冊 ,像spring+structs,就是以這種方式來初始化上下文內容的

?
1
2
3
4
5
6
7
8
<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,同時要注冊servlet

?
1
2
3
4
5
6
7
8
@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);
 }

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。

原文鏈接:https://www.jianshu.com/p/52623dde2ef2

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 姑娘第四集免费看视频 | 欧美日韩成人一区二区 | 关键词| 免费看欧美一级特黄a毛片 九色com | 亚洲精久久 | 中文字幕欧美亚洲 | 国产在线色 | 手机国产乱子伦精品视频 | chinese xxxxhd videos麻豆 | 中国videos露脸hd| 国产精品99久久久久久久 | 91 在线免费观看 | 毛片观看网址 | 草草久久久| 亚洲码无人客一区二区三区 | 亚洲成人久久精品 | 久久99精品久久久久久秒播放器 | 中文字幕综合 | 久久精品一二三区白丝高潮 | 久久久久久久久久综合 | 欧美成人精品不卡视频在线观看 | 免费看搡女人无遮挡的视频 | 久久久久久久久成人 | 国产一区二区三区四区五区加勒比 | 视频在线中文字幕 | 免费毛片播放 | 欧美一级一区二区三区 | 成人做爰s片免费看网站 | 毛片视频在线免费观看 | 国产一级桃视频播放 | 九色在线78m | 三级xxxx | 99国产精品白浆在线观看免费 | 成人免费在线视频播放 | 在线看小早川怜子av | 91精品国产网站 | 三级xxxx| 久久国产精品久久久久 | 亚洲精品午夜国产va久久成人 | 成人在线a | 欧美日韩国产成人在线 |