[英]spring singleton bean construction
I'm new on spring and I want a spring singleton bean to be created when the web application starts, not when it is referenced. 有没有办法做到这一点? 提前致谢
这是默认行为。 Spring bean 默认是非惰性单例。
<bean id="Example" class="com.domain.Example" singleton="true" lazy-init="false" />
或者
<bean id="Example" class="com.domain.Example" />
lazy-init 和 singleton 默认设置为此值。
您可以编写引导程序 class 以在应用程序启动时加载参考。
如果使用 Spring MCV,则可以通过在 web.xml 文件中创建 servlet 映射来加载上下文
<servlet-mapping>
<servlet-name>MyServlet<servlet-name>
<url-pattern>*.jsp</url-pattern>
</servlet-mapping>
接下来,加载配置文件。 为此,请注册 ContextLoaderListener。 当您启动 Web 应用程序时,ContextLoaderServlet 将加载 Spring 配置文件
<servlet>
<servlet-name>context>servlet-name>
<servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
contextConfigLocation 参数定义要加载的 Spring 配置文件
<context-param>
<param-value>contextConfigLocation</param-value>
<param-value>/WEB-INF/my-spring-config.xml</param-value>
</context-param>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.