为了账号安全,请及时绑定邮箱和手机立即绑定

Spring Context加载方式

标签:
Spring

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



作者:David_jim
链接:https://www.jianshu.com/p/52623dde2ef2


点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
JAVA开发工程师
手记
粉丝
205
获赞与收藏
1007

关注作者,订阅最新文章

阅读免费教程

  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消