1

私は休止状態+春のアプリケーションを書いています。

アプリケーションの起動後 (ただし、最初に使用する前) にデータベースの状態を確認したい。

状態が良好な場合 - ロジックをスキップ

状態が悪い場合 - 状態を良好にするロジックを実行します。

この問題のベスト プラクティスを推測できますか?

デボジット・サイキア のアップデート

このコードを書くと:

@Configuration
public class UiConfig {

    @Bean
    public AppListener getMyListener(){
        AppListener appListener = new AppListener();
        System.out.println();
        System.out.println();
        System.out.println(" appListener method ");
        System.out.println();
        System.out.println();
        return appListener;
    }
}

class AppListener implements ApplicationListener<ContextRefreshedEvent> {

        @Override
        public void onApplicationEvent(ContextRefreshedEvent event) {
            System.out.println();
            System.out.println();
            System.out.println(" hello AppListener");
            System.out.println();
            System.out.println();
        }

    }

コンソールに次のログインが表示されます。

21.10.2013 18:46:05 org.apache.catalina.core.AprLifecycleListener init
INFO: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: C:\Program Files\Java\jdk1.6.0_37\bin;C:\WINDOWS\Sun\Java\bin;C:\WINDOWS\system32;C:\WINDOWS;C:\Program Files\Java\jdk1.6.0_37\bin;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\QuickTime\QTSystem\;C:\Program Files\TortoiseSVN\bin;C:\Program Files\Microsoft SQL Server\110\Tools\Binn\;C:\Program Files (x86)\Microsoft SQL Server\110\Tools\Binn\;C:\Program Files\Microsoft SQL Server\110\DTS\Binn\;C:\Program Files (x86)\Microsoft SQL Server\110\Tools\Binn\ManagementStudio\;C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\PrivateAssemblies\;C:\Program Files (x86)\Microsoft SQL Server\110\DTS\Binn\;%ANT_HOME%/bin;C:\Program Files\Apache Software Foundation\apache-maven-3.0.5\\bin;C:\Program Files\Apache Software Foundation\apache-ant-1.9.0/bin;.
21.10.2013 18:46:06 org.apache.tomcat.util.digester.SetPropertiesRule begin
WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:ui' did not find a matching property.
21.10.2013 18:46:06 org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-bio-8080"]
21.10.2013 18:46:06 org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["ajp-bio-8009"]
21.10.2013 18:46:06 org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 762 ms
...
INFO : org.springframework.beans.factory.xml.XmlBeanDefinitionReader - Loading XML bean definitions from ServletContext resource [/WEB-INF/spring/security_config.xml]
INFO : org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor - JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
INFO : org.springframework.beans.factory.support.DefaultListableBeanFactory - Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@77be91c8: defining beans [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.internalPersistenceAnnotationProcessor,candidateDao,eventDaoImpl,eventStatusDao,eventTypeDao,noteDao,skillDao,userDao,vacancyDao,candidateService,eventService,utilService,vacancyService,baseMenuController,candidateMenuController,provider,eventMenuController,homeController,uiConfig,vacancyMenuController,org.springframework.aop.config.internalAutoProxyCreator,org.springframework.transaction.annotation.AnnotationTransactionAttributeSource#0,org.springframework.transaction.interceptor.TransactionInterceptor#0,org.springframework.transaction.config.internalTransactionAdvisor,transactionManager,dataSource,sessionFactory,entryPoint,org.springframework.security.filterChains,org.springframework.security.filterChainProxy,org.springframework.security.web.PortMapperImpl#0,org.springframework.security.web.PortResolverImpl#0,org.springframework.security.config.authentication.AuthenticationManagerFactoryBean#0,org.springframework.security.authentication.ProviderManager#0,org.springframework.security.web.context.HttpSessionSecurityContextRepository#0,org.springframework.security.web.authentication.session.SessionFixationProtectionStrategy#0,org.springframework.security.web.savedrequest.HttpSessionRequestCache#0,org.springframework.security.web.access.expression.DefaultWebSecurityExpressionHandler#0,org.springframework.security.access.vote.AffirmativeBased#0,org.springframework.security.web.access.intercept.FilterSecurityInterceptor#0,org.springframework.security.web.access.DefaultWebInvocationPrivilegeEvaluator#0,org.springframework.security.authentication.AnonymousAuthenticationProvider#0,org.springframework.security.web.authentication.www.BasicAuthenticationEntryPoint#0,org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter#0,org.springframework.security.userDetailsServiceFactory,org.springframework.security.web.DefaultSecurityFilterChain#0,org.springframework.security.authentication.DefaultAuthenticationEventPublisher#0,org.springframework.security.authenticationManager,org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor,messageSource,getMyListener]; root of factory hierarchy


 appListener method 




 hello AppListener


INFO : org.springframework.web.context.ContextLoader - Root WebApplicationContext: initialization completed in 4544 ms
21.10.2013 18:46:21 org.apache.catalina.core.ApplicationContext log
INFO: Initializing Spring FrameworkServlet 'appServlet'
INFO : org.springframework.web.servlet.DispatcherServlet - FrameworkServlet 'appServlet': initialization started
INFO : org.springframework.web.context.support.XmlWebApplicationContext - Refreshing WebApplicationContext for namespace 'appServlet-servlet': startup date [Mon Oct 21 18:46:21 MSK 2013]; parent: Root WebApplicationContext
...
INFO : org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Mapped "{[/addVacancy],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public org.springframework.web.servlet.ModelAndView com.epam.hhsystem.web.controllers.VacancyMenuController.addVacancy(com.epam.hhsystem.model.vacancy.Vacancy,org.springframework.validation.BindingResult,org.springframework.ui.Model,org.springframework.web.servlet.mvc.support.RedirectAttributes)
INFO : org.springframework.web.servlet.handler.SimpleUrlHandlerMapping - Mapped URL path [/resources/**] onto handler 'org.springframework.web.servlet.resource.ResourceHttpRequestHandler#0'


 appListener method 




 hello AppListener




 hello AppListener


INFO : org.springframework.web.servlet.DispatcherServlet - FrameworkServlet 'appServlet': initialization completed in 770 ms
21.10.2013 18:46:22 org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-bio-8080"]
21.10.2013 18:46:22 org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["ajp-bio-8009"]
21.10.2013 18:46:22 org.apache.catalina.startup.Catalina start
INFO: Server startup in 16053 ms

このリスナーは、Tomcat を実行した後、さらに 1 回呼び出します。

一度だけ呼び出してほしい

4

3 に答える 3

2

私は(サーブレットコンテキスト環境であなたを想定して)リスナーを使用します。実装してリスナーを作成し、javax.servlet.ServletContextListenerそれらを最後のリスナーとして配置します。最後のリスナーとして呼び出されます(サーバーにも依存します。サーバーのドキュメントを確認してください)。リスナーでは、状態が良好であることを確認します。良いことは、サーバーへのリクエストの前に呼び出されることです。Web サーバー/Web コンテナーがこれを保証します。

例:

public class MyContextListener implements ServletContextListener {
      public void contextInitialized(ServletContextEvent sce){
          //do your work here
      }     
}

ウェブxmlで:

<listener>
        <listener-class>com.my.MyContextListener</listener-class>
</listener>
于 2013-10-21T14:00:36.640 に答える