一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

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

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加載方式,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

spring 加載方式

對于可執行文件方式,我們一般的加載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的方式來配置初始化參數,不過這次里要用基于注解的類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

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 国产午夜免费不卡精品理论片 | 朝鲜女人free性xxe | 人人最怕九月羊 | 亚洲精品视频免费在线观看 | 冰山美人调教耻辱h | 男同桌脱我奶罩吸我奶作文 | 免费看a片毛片 | 日韩国产欧美成人一区二区影院 | 免费全看男女拍拍拍的视频 | 91tv在线 | 欧美老肥妇bbbw | 2019nv天堂香蕉在线观看 | 青丝视频免费版在线看 | 桃乃木香奈ipx在线播放 | 亚州春色 | 国产xx肥老妇视频奂费 | 非洲黑女人性xxxx | 嫩草在线视频www免费观看 | 女人张开腿 让男人桶个爽 免费观看 | 亚洲精品一区二区三区中文字幕 | 精品一区在线 | 特黄a大片免费视频 | 国产午夜精品福利 | 亚洲激情网站 | 午夜精品亚洲 | 久久毛片网站 | 91精品国产综合久久精品 | 国产精品短视频 | 色综合色狠狠天天综合色hd | 饭冈加奈子黑人解禁在线播放 | 韩日视频在线观看 | 粉嫩高中生第一次不戴套 | 美女用屁股把人吞进肚子 | voyeur多毛厕所 | 欧美日韩亚洲国内综合网香蕉 | 日本日日黄 | 暖暖 免费 高清 日本 中文 | 无人在线高清观看 | 小小水蜜桃3视频在线观看 小鸟酱喷水 | 99午夜高清在线视频在观看 | 欧美va在线 |