Java tutorial
/******************************************************************************* * Copyright Kevin Lynx (kevinlynx@gmail.com) 2015 * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. *******************************************************************************/ package com.codemacro.jcm; import java.io.IOException; import org.springframework.context.ApplicationListener; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.ImportResource; import org.springframework.context.event.ContextClosedEvent; import org.springframework.context.event.ContextRefreshedEvent; import org.springframework.stereotype.Component; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; /* in this class, we can import Spring stuff */ @Configuration @ImportResource({ "jcm-core.xml" }) @EnableAutoConfiguration @ComponentScan(basePackages = "com.codemacro.jcm") public class JCMMain implements ApplicationListener<ContextRefreshedEvent> { @Autowired private JCMApplication jcmApp; public static void main(String[] args) { SpringApplication.run(JCMMain.class); } public void onApplicationEvent(ContextRefreshedEvent event) { try { jcmApp.startup(); } catch (IOException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } } } @Component class ShutdownHook implements ApplicationListener<ContextClosedEvent> { @Autowired private JCMApplication jcmApp; public void onApplicationEvent(ContextClosedEvent event) { try { jcmApp.shutdown(); } catch (InterruptedException e) { e.printStackTrace(); } } }