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

Tomcat部署Spring项目

标签:
Spring

虽然Spring Boot火了一段时间,Spring在旧的项目中还是有不少的用武之地,在Tomcat中部署Spring也是比较基础的技能。

web.xml

web.xml是tomcat servlet容器部署的描述文件,在每个需要使用tomcta部署的应用中都需要有这个文件。

常见标签:
<context-param>
<servlet>
<filter>
<listner>

参考:http://wiki.metawerx.net/wiki/Web.xml

ContextLoaderListener

在旧的项目中,我们常看到ContextLoaderListener的这种用法:

<context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            classpath:applicationContext.xml        </param-value>
    </context-param>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

它有两种用途:

  1. 绑定ApplicationContext的生命周期到ServletContext

  2. 自动的创建ApplicationContext,因此你不需要精确的指定去创建ApplicatiionContext,这是一种更方便的用法。

参考:https://stackoverflow.com/questions/11815339/role-purpose-of-contextloaderlistener-in-spring

实际上,我们还有几种其它的用法,并非要明确的指定ContextLoaderListener
另外用法1:

  1. 先在xml文件中指定一个Servlet,指定其name,然后在web.xml同目录下创建一个其名称后缀加上-servlet的文件。

如:

 <servlet>
        <servlet-name>dispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

然后创建dispatcherServlet-servlet.xml,其与web.xml在同一目录

另外用法2:

<servlet>
        <servlet-name>dispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath*:applicationContext.xml,classpath*:applicationContext-web.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

一个错误

还有今天一直碰到的一个错误,没来得及解决


1000

image.png

Spring的项目中依赖了etcd,bean的配置文件里一些bean的配置比如redis的连接时动态配置的,而动态配置的属性要在生效之前从etcd中拉取配置。

老的项目代码写的有点乱,启动的时候没有加载etcd的配置,etcd相关的代码没有执行,弄了半天没弄好。

碰其它的代码有时候难免有些问题...

最后

今天碰了下一个比较老的项目,关于Tomcat的一些内容有些忘记了,简单记录一下。

点击查看更多内容
TA 点赞

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

评论

作者其他优质文章

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

关注作者,订阅最新文章

阅读免费教程

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

100积分直接送

付费专栏免费学

大额优惠券免费领

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

举报

0/150
提交
取消