当前位置: 首页 > 编程笔记 >

浅谈Spring Context加载方式

乌修筠
2023-03-14
本文向大家介绍浅谈Spring Context加载方式,包括了浅谈Spring Context加载方式的使用技巧和注意事项,需要的朋友参考一下

Spring 加载方式

对于可执行文件方式,我们一般的加载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开始,开始使用注解的方式来进行spring 配置的注册

 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中的

<context:annotation-config/>

demoService 很简单,如下的方式

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

Web应用的初始化

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

web.xml 配置方式

利用spring 自带的Servlet 进行初始注册

  <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,就是以这种方式来初始化上下文内容的

  <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

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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持小牛知识库。

 类似资料:
  • 本文向大家介绍浅谈vue加载优化策略,包括了浅谈vue加载优化策略的使用技巧和注意事项,需要的朋友参考一下 vue.js是一个比较流行的前端框架,与react.js、angular.js相比来说,vue.js入手曲线更加流畅,不管掌握多少都可以快速上手。但是单页面应用也都有其弊病,有时候首屏加载慢的让人捏舌。今天我们以vue cli3.x来说一说如何行之有效的缓解此问题! 方法一 路由懒加载 首屏

  • 本文向大家介绍浅谈vue首屏加载优化,包括了浅谈vue首屏加载优化的使用技巧和注意事项,需要的朋友参考一下 本文介绍了浅谈vue首屏加载优化,分享给大家,具体如下: 库使用情况 vue vue-router axios muse-ui material-icons vue-baidu-map 未优化前 首先我们在正常情况下build 优化 1. 按需加载 当前流行的UI框架如iview,muse-

  • 本文向大家介绍浅谈Vue的加载顺序探讨,包括了浅谈Vue的加载顺序探讨的使用技巧和注意事项,需要的朋友参考一下 在Vuejs 1.0版本中,如果父子组件进行配合,它们的生命周期执行具有如下特点: 1. created总是先父后子 生命周期函数created总是按照从父到子的顺序依次执行,但是兄弟之间没有严格按照这样的顺序执行,估计是采用了异步函数,不仅如此,子组件在父组件中的插入顺序也是随机的,并

  • 本文向大家介绍浅谈Webpack 是如何加载模块的,包括了浅谈Webpack 是如何加载模块的的使用技巧和注意事项,需要的朋友参考一下 Webpack 在前端开发中作为模块打包工具非常受开发者的青睐,丰富的 loader 使它可以实现各种各样的功能。本文将通过 webpack 来打包一个 js 文件,看看 webpack 是如何加载各个模块的。 两个简单的源文件 为了方便分析 webpack 加载

  • 本文向大家介绍浅谈Volley加载不出图片的问题,包括了浅谈Volley加载不出图片的问题的使用技巧和注意事项,需要的朋友参考一下 问题分析:加载后台图片的时候,发现加载不出来,后来发现图片的url格式是: http://192.168.1.71/\carhome\shop\778c2bc3ec0a49e1969b24b3a8e62f31\detail\DSC04209.JPG 因为Volley请

  • 本文向大家介绍浅谈Vue SPA 首屏加载优化实践,包括了浅谈Vue SPA 首屏加载优化实践的使用技巧和注意事项,需要的朋友参考一下 写在前面 本文记录笔者在Vue SPA项目首屏加载优化过程中遇到的一些坑及优化方案! 我们以 vue-cli 工具为例,使用 vue-router 搭建SPA应用,UI框架选用 element-ui , ajax方案选用 axios, 并引入vuex ,使用 vu