浅谈Spring Context加载方式

所属分类: 软件编程 / java 阅读数: 19
收藏 0 赞 0 分享

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

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

更多精彩内容其他人还在看

Java数据类型的规则

这篇文章主要介绍了Java数据类型的规则的相关资料,非常不错,具有参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

Spring整合TimerTask实现定时任务调度

这篇文章主要介绍了Spring整合TimerTask实现定时任务调度的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

详解SpringMVC使用MultipartFile实现文件的上传

本篇文章主要介绍了SpringMVC使用MultipartFile实现文件的上传,本地的文件上传到资源服务器上,比较好的办法就是通过ftp上传。这里是结合SpringMVC+ftp的形式上传的,有兴趣的可以了解一下。
收藏 0 赞 0 分享

SpringMVC上传文件的三种实现方式

本篇文章主要介绍了SpringMVC上传文件的三种实现方式,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

微信公众帐号开发-自定义菜单的创建及菜单事件响应的实例

本篇文章主要介绍了微信公众帐号开发-自定义菜单的创建及菜单事件响应的实例,具有一定的参考价值,感兴趣的小伙伴们可以参考一下。
收藏 0 赞 0 分享

浅析Java中的继承与组合

本文将介绍组合和继承的概念及区别,并从多方面分析在写代码时如何进行选择。文中通过示例代码介绍的很详细,有需要的朋友可以参考借鉴,下面来一起看看吧。
收藏 0 赞 0 分享

利用反射获取Java类中的静态变量名及变量值的简单实例

下面小编就为大家带来一篇利用反射获取Java类中的静态变量名及变量值的简单实例。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

java启动线程的3种方式对比分析

这篇文章主要为大家对比分析了java启动线程的3种方式,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

SpringMVC上传和解析Excel方法

这篇文章主要介绍了SpringMVC上传和解析Excel方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

JAVA中String类与StringBuffer类的区别

这篇文章主要为大家详细介绍了JAVA中String类与StringBuffer类的区别,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享
查看更多