SpringContext加載方式的示例分析-創(chuàng)新互聯(lián)

這篇文章主要為大家展示了“Spring Context加載方式的示例分析”,內(nèi)容簡(jiǎn)而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領(lǐng)大家一起研究并學(xué)習(xí)一下“Spring Context加載方式的示例分析”這篇文章吧。

蘭溪網(wǎng)站建設(shè)公司創(chuàng)新互聯(lián),蘭溪網(wǎng)站設(shè)計(jì)制作,有大型網(wǎng)站制作公司豐富經(jīng)驗(yàn)。已為蘭溪1000+提供企業(yè)網(wǎng)站建設(shè)服務(wù)。企業(yè)網(wǎng)站搭建\外貿(mào)網(wǎng)站建設(shè)要多少錢,請(qǐng)找那個(gè)售后服務(wù)好的蘭溪做網(wǎng)站的公司定做!

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);
  }

以上是“Spring Context加載方式的示例分析”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!

新聞名稱:SpringContext加載方式的示例分析-創(chuàng)新互聯(lián)
本文網(wǎng)址:http://bm7419.com/article48/dseoep.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供商城網(wǎng)站域名注冊(cè)、自適應(yīng)網(wǎng)站、定制開(kāi)發(fā)網(wǎng)站排名、網(wǎng)站策劃

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

成都網(wǎng)站建設(shè)公司