Spring Web项目spring配置文件随服务器启动时自动加

前言:其实配置文件不随服务器启动时加载也是可以的,但是这样操作的话,每次获取相应对象,就会去读取一次配置文件,从而降低程序的效率,而Spring中已经为我们提供了监听器,可监听服务器是否启动,然后在启动时,加载spring的配置文件,并且只加载一次,从而提高程序效率。

实现:其配置需要在web.xml中进行,具体实现如下:

<!--配置监听器 --> <!--以便在服务器启动的时候,加载spring配置文件--> <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener> <!--配置spring配置文件--> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring/spring-context.xml</param-value> </context-param>

注:这里注意<context-param>标签中<param-name>中的内容为固定值,<param-value>中内容为固定格式:classpath:spring配置文件(如有路径请加上)

通过以上配置就可以让spring配置文件随服务器启动而加载了。

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/f7172b658aee506d85597b2bced52509.html