[英]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. 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. Is there a way to achive this?
有没有办法做到这一点? Thanks in advance
提前致谢
This is the default behaviour.这是默认行为。 Spring beans are non-lazy singletons by default.
Spring bean 默认是非惰性单例。
<bean id="Example" class="com.domain.Example" singleton="true" lazy-init="false" />
or或者
<bean id="Example" class="com.domain.Example" />
lazy-init and singleton are set to this value by default. lazy-init 和 singleton 默认设置为此值。
You could write a bootstrap class to load the reference on app start-up.您可以编写引导程序 class 以在应用程序启动时加载参考。
You can load your context by creating a servlet mapping in your web.xml file if you use Spring MCV如果使用 Spring MCV,则可以通过在 web.xml 文件中创建 servlet 映射来加载上下文
<servlet-mapping>
<servlet-name>MyServlet<servlet-name>
<url-pattern>*.jsp</url-pattern>
</servlet-mapping>
Next, load the configuration files.接下来,加载配置文件。 To do this, register the ContextLoaderListener.
为此,请注册 ContextLoaderListener。 The ContextLoaderServlet will load the Spring configuration files when you start the Web application
当您启动 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>
The contextConfigLocation parameter defines the Spring configurations files to load 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.