简体   繁体   English

如何在春季初始化应用程序?

[英]How to Initialize the application in spring?

Now come directly to my point, 现在直接来到我的观点,

In JSP I will do the initialization process of my application like, JSP我会做我的应用程序的初始化过程,比如

<%! public void jsp_init(){

      //Initialise the domain server to create protocol
      //Create the logging file
}%>

Now i am going to rebuild my previous application from Servlets to Spring 3.2 . 现在我将重建我之前从ServletsSpring 3.2应用程序。

How I can I do this with Spring 3.2 ? 我怎么能用Spring 3.2做到这一点?

One of my colleague said me to do this initialization with Constructor of the Spring Controller . 我的colleague说我initialization with Constructor of the Spring Controller

Because I have created the bean for controller class in the applicationContext.xml and I am loading the applicationContext.xml file withe ContextLoadListner in web.xml . 因为我在applicationContext.xmlcontroller class创建了bean,所以我在web.xml使用ContextLoadListner加载applicationContext.xml文件。

Is this the right way of Initialization ? 这是正确的初始化方式吗?

What about ApplicationListener in spring ? 春天的ApplicationListener怎么样?

Which is the best way to initialize the application in spring 3.2 ? 哪个是在spring 3.2初始化应用程序的最佳方法?

Hope our stack users will give a good solution. 希望我们的堆栈用户能给出一个好的解决方案

Spring will do a great deal of this for you if configured properly. 如果配置正确,Spring会为您做很多事情。 If you really need to execute code (vs using something that will automatically configure itself like Log4J), I would suggest registering an InitializingBean and overriding afterPropertiesSet . 如果你真的需要执行代码(vs使用像Log4J那样自动配置的东西),我建议注册一个InitializingBean并覆盖afterPropertiesSet You then would add this bean definition to the applicationContext.xml file: 然后,您可以将此bean定义添加到applicationContext.xml文件中:

<bean id="initializer" class="com.myproject.MyInitializer" />

As a result, Spring will invoke the MyInitializer.afterPropertiesSet() method when the application has been fully initialized. 因此,当应用程序完全初始化时,Spring将调用MyInitializer.afterPropertiesSet()方法。 You alternatively could use the @PostConstruct annotation on a bean that has been registered with the application context, but there's no guarantee the rest of the application will be initialized when that method gets invoked. 您也可以在已经向应用程序上下文注册的bean上使用@PostConstruct注释,但是无法保证在调用该方法时将初始化其余的应用程序。 If you want it to run when everything has been set up, the Initializing Bean method is the way to go. 如果您希望在设置完所有内容后运行它,则可以使用Initializing Bean方法。 I've used this strategy to start a server socket, etc that needed to run independently of the web request life cycle. 我已经使用这种策略来启动一个服务器套接字等,它需要独立于Web请求生命周期运行。

Why would u initialize spring application by yourself? 你为什么要自己初始化spring应用程序? Spring will do automatically for you: This is how you tell your server to initialize spring: Spring将自动为您执行:这是您告诉服务器初始化spring的方法:

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/app.xml</param-value>
  </context-param>

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

This will happen during deploymentand all beans are defined there will be initialized(depending on laziness). 这将在部署期间发生,并且定义所有bean将被初始化(取决于懒惰)。 If you really want to do something once bean is initialized, before usage then use InitializingBean Example would be 如果你真的想在初始化bean之后做一些事情,那么在使用之前然后使用InitializingBean Example

MyBean implements InitializingBean{
   afterPropertiesSet() {

      //do here
   }

}

ContextLoaderListener is sort of bootstrapper to start up Spring's WebApplicationContext while ApplicationListener is more at the java application level itself rather than at the web application context. ContextLoaderListener是一种启动Spring的WebApplicationContext的引导程序,而ApplicationListener更多地处于Java应用程序级别本身而不是Web应用程序上下文。

ContextLoaderListener is a great and standard tool for the contextualization of your app if it has multiple DispatcherServlet s or some servlets/servlet filters mapped to different services. ContextLoaderListener是一个很棒的标准工具,用于应用程序的上下文化,如果它有多个DispatcherServlet或一些servlet / servlet过滤器映射到不同的服务。 Basically, it is handy to have listeners for different servlets of such apps so that you can have fine-grained contextualization. 基本上,为这些应用程序的不同servlet提供监听器是很方便的,这样你就可以进行细粒度的语境化。

I do not know the nature of the application you are building but I assume that you are trying something basic for now. 我不知道你正在构建的应用程序的性质,但我认为你现在正在尝试一些基本的东西。 If this is the case, and even in cases where you have a more complex setup, it is better to load on startup a controller that takes care of your main initialization routines including your contextualization. 如果是这种情况,即使在设置更复杂的情况下,最好在启动时加载一个控制器来处理主要的初始化例程,包括上下文化。 You can use the controller bean you have with something like this in your web.xml: 你可以在web.xml中使用你拥有的控制器bean:

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

This servelt can also be mapped and invoked whenever you read a certain url pattern Eg 每当您读取某个url模式时,也可以映射和调用此servelt

@RequestMapping("/welcome")         
    public ModelAndView helloWorld() 

And in the web deployment descriptor, this bit is just like you map your servlets to other Spring services like Spring Security: 在Web部署描述符中,这一点就像您将servlet映射到Spring Security等其他Spring服务:

<servlet-mapping>
    <servlet-name>crunchify</servlet-name>
    <url-pattern>*.html</url-pattern>
</servlet-mapping>

So you don't really need to use the constructor of a controller and neither do you always need to use ContextLoaderListener or other listeners for simple initialization tasks. 因此,您实际上不需要使用控制器的构造函数,也不需要使用ContextLoaderListener或其他侦听器来执行简单的初始化任务。 Nevertheless, it is handy to learn their use cases as you will need it when scaling your app. 不过,在扩展应用程序时,学习它们的用例非常方便。

Read more about dispatcher servlet here: 在这里阅读有关调度程序servlet的更多信息:

http://static.springsource.org/spring/docs/3.2.x/javadoc-api/org/springframework/web/servlet/DispatcherServlet.html http://static.springsource.org/spring/docs/3.2.x/javadoc-api/org/springframework/web/servlet/DispatcherServlet.html

You could do this by having a bean with @PostConstruct and injecting in in your servlet-config in spring. 您可以通过使用带有@PostConstruct的bean并在spring中注入servlet-config来实现此目的。 Take a look at the code here and one of the lines at the bottom. 看看这里的代码和底部的一行。

<beans:bean id="PlayerImportDaoImpl"
        class="com.footieview.app.importer.dao.PlayerImportDaoImpl" />

This will inject this bean at startup a method on this bean has the annotation @PostConstruct - this means at startup this method gets called. 这将在启动时注入此bean,此bean上的方法具有注释@PostConstruct - 这意味着在启动时会调用此方法。

Create a Spring 3 MVC application and you do not need to do this! 创建一个Spring 3 MVC应用程序,您不需要这样做!

Spring MVC supports JSPs and you can do all the configuration via Annotations Spring MVC支持JSP,您可以通过Annotations进行所有配置

See http://fruzenshtein.com/spring-mvc-creation-of-simple-controller-with-java-based-config/ for an example. 有关示例,请参见http://fruzenshtein.com/spring-mvc-creation-of-simple-controller-with-java-based-config/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 Spring Boot 应用程序中初始化 AWS SDK? - How to initialize AWS SDK in spring boot application? 如何在不重新启动应用程序服务器的情况下在春季重新初始化bean - How to re-initialize beans in spring without application server restart 如何使用 Spring 引导应用程序初始化 log4j? - How to initialize log4j with Spring Boot application? 如何在 Selenium TestNG 环境中初始化 Spring 应用程序上下文文件 - How to initialize Spring application context file in Selenium TestNG environment 如何在启动 spring 应用程序之前初始化数据结构? - How to initialize data sturcture before starting spring application? 数据源无法通过Spring Boot应用程序初始化? - Datasource not able to initialize via Spring boot application? 应用程序启动后初始化 spring bean 字段 - Initialize spring bean field after application started 如何将spring boot初始化为项目? - How to initialize spring boot to project? 如何从WEB-INF加载“迷你”弹簧上下文以初始化真实的应用程序上下文 - how to load a 'mini' spring context from WEB-INF in order to initialize the real application context 如何使用附加到Spring中当前应用程序上下文的环境初始化property-placeholder? - How to initialize property-placeholder with environment attached to the current application context in Spring?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM