Java Code Examples for org.springframework.context.annotation.AnnotationConfigApplicationContext#registerShutdownHook()

The following examples show how to use org.springframework.context.annotation.AnnotationConfigApplicationContext#registerShutdownHook() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BeanDefinitionInheritanceDemoApplication.java    From sfg-blog-posts with GNU General Public License v3.0 6 votes vote down vote up
public static void main(String[] args) {
    //XML based Bean Definition Test\
    System.out.println("XML based Bean Definition Inheritance Test");
    ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
    Book book = (Book) context.getBean("BookBean");
    System.out.println("Book Details: " + book);


    //Annotation based Bean Definition Test
    System.out.println("Annotation based Bean Definition Inheritance Test");
    AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
    ctx.register(AppConfig.class);
    ctx.refresh();
    EPubBook ePubBook = ctx.getBean(EPubBook.class);
    System.out.println("Author Name: " + ePubBook.getAuthorName());
    System.out.println("Book Name: " + ePubBook.getBookName());
    System.out.println("Book Price: " + ePubBook.getBookPrice());
    System.out.println("Download URL: " + ePubBook.getDownloadUrl());
    ctx.registerShutdownHook();
}
 
Example 2
Source File: TestScopes.java    From Spring-5.0-Cookbook with MIT License 6 votes vote down vote up
public static void main(String[] args) {
	 AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
	   context.register(BeanConfig.class);
   
	   System.out.println("application context loaded.");
        
       context.refresh();
       System.out.println("*********The empRec1 bean ***************");
       Employee empRec1A = (Employee) context.getBean("empRec1");
       System.out.println("instance A: " + empRec1A.hashCode());
       Employee empRec1B = (Employee) context.getBean("empRec1");
       System.out.println("instance B: " +empRec1B.hashCode());
       
       System.out.println("*********The empRec2 bean ***************");
       Employee empRec2A = (Employee) context.getBean("empRec2");
       System.out.println("instance A: " + empRec2A.hashCode());
       Employee empRec2B = (Employee) context.getBean("empRec2");
       System.out.println("instance B: " + empRec2B.hashCode());
       
       context.registerShutdownHook();
}
 
Example 3
Source File: TestBeans.java    From Spring-5.0-Cookbook with MIT License 5 votes vote down vote up
public static void main(String[] args) {
	   AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
	   context.register(BeanConfig.class);
   
	   System.out.println("application context loaded.");
        
       context.refresh();
       System.out.println("*********The empRec1 bean ***************");
       Employee empRec1 = (Employee) context.getBean("empRec1");
       
       System.out.println("*********The empRec2 bean ***************");
       Employee empRec2 = (Employee) context.getBean("empRec2");
       Department dept2 = empRec2.getDept();
       System.out.println("First Name: " + empRec2.getFirstName());
       System.out.println("Last Name: " + empRec2.getLastName());
       System.out.println("Birthdate: " + empRec2.getBirthdate());
       System.out.println("Salary: " + empRec2.getSalary());
       System.out.println("Dept. Name: " + dept2.getDeptName());
       
       System.out.println("*********The empRec3 bean ***************");
       Employee empRec3 = (Employee) context.getBean("empRec3");
       Department dept3 = empRec3.getDept();
       System.out.println("First Name: " + empRec3.getFirstName());
       System.out.println("Last Name: " + empRec3.getLastName());
       System.out.println("Birthdate: " + empRec3.getBirthdate());
       System.out.println("Salary: " + empRec3.getSalary());
       System.out.println("Dept. Name: " + dept3.getDeptName());
       
       System.out.println("*********The empRec4 bean ***************");
       Employee empRec4 = (Employee) context.getBean("empRec4");
       Department dept4 = empRec4.getDept();
       System.out.println("First Name: " + empRec4.getFirstName());
       System.out.println("Last Name: " + empRec4.getLastName());
       System.out.println("Birthdate: " + empRec4.getBirthdate());
       System.out.println("Salary: " + empRec4.getSalary());
       System.out.println("Dept. Name: " + dept4.getDeptName());
       
       context.registerShutdownHook();
}
 
Example 4
Source File: VersionedKeyValueBackendIntegrationTests.java    From spring-vault with Apache License 2.0 5 votes vote down vote up
@Test
void shouldRetrieveNonLeasedSecret() {

	AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(
			VaultIntegrationTestConfiguration.class, NonRotatingSecret.class);
	context.registerShutdownHook();

	assertThat(context.getEnvironment().getProperty("my-key")).isEqualTo("my-value");

	context.stop();
}
 
Example 5
Source File: VersionedKeyValueBackendIntegrationTests.java    From spring-vault with Apache License 2.0 5 votes vote down vote up
@Test
void shouldRetrieveRotatingSecret() {

	AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(
			VaultIntegrationTestConfiguration.class, RotatingSecret.class);
	context.registerShutdownHook();

	assertThat(context.getEnvironment().getProperty("my-key")).isEqualTo("my-value");

	context.stop();
}
 
Example 6
Source File: SpringVerticleFactory.java    From spring-vertx-ext with Apache License 2.0 5 votes vote down vote up
private static void addPostprocessorAndUpdateContext(Class<?> currentVerticleClass,
    AnnotationConfigApplicationContext annotationConfigApplicationContext) {
    annotationConfigApplicationContext.addBeanFactoryPostProcessor(new SpringSingleVerticleConfiguration(currentVerticleClass));
    annotationConfigApplicationContext.refresh();
    annotationConfigApplicationContext.start();
    annotationConfigApplicationContext.registerShutdownHook();
}
 
Example 7
Source File: Main.java    From CogStack-Pipeline with Apache License 2.0 4 votes vote down vote up
public static void setUpApplicationContext(ConfigurableEnvironment environment, Properties properties) {
    @SuppressWarnings("resource")
    AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
    ctx.registerShutdownHook();
    ctx.setEnvironment(environment);

    // TODO: need a proper way to validate input properties specified by user
    String executionMode = environment.getProperty("execution.mode", "local").toLowerCase();
    if (executionMode != "local" && executionMode != "remote")
        throw new RuntimeException("Invalid execution mode specified. Must be `local` (default) or `remote`.");

    String instanceType = "";
    if (executionMode == "remote")
    {
        if (!environment.containsProperty("execution.instanceType")) {
            throw new RuntimeException("Instance type in remote execution not specified. Must be `master` or `slave`.");
        }

        instanceType = environment.getRequiredProperty("execution.instanceType").toLowerCase();
        if (instanceType != "master" && instanceType != "slave")
            throw new RuntimeException("Invalid instance type in remote execution mode specified. Must be `master` or `slave`.");
    }

    boolean useScheduling;
    try {
        useScheduling = Boolean.parseBoolean(environment.getProperty("scheduler.useScheduling", "false"));
    } catch (Exception e) {
        throw new RuntimeException("Invalid scheduling option specified. Must be `true` or `false` (default).");
    }

    // set appropriate job configuration
    if (executionMode == "remote" && instanceType == "slave") {
        ctx.register(JobConfiguration.class);
        ctx.refresh();
    } else { // execution mode local or remote with master
        if (useScheduling) {
            ctx.register(ScheduledJobLauncher.class);
            ctx.refresh();
        } else {
            ctx.register(SingleJobLauncher.class);
            ctx.refresh();
            SingleJobLauncher launcher = ctx.getBean(SingleJobLauncher.class);
            launcher.launchJob();
        }
    }
}
 
Example 8
Source File: App.java    From minitwit with MIT License 4 votes vote down vote up
public static void main(String[] args) {
	AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(App.class);
	new WebConfig(ctx.getBean(MiniTwitService.class));
    ctx.registerShutdownHook();
}
 
Example 9
Source File: JsonCacheDataImporterExporterIntegrationTests.java    From spring-boot-data-geode with Apache License 2.0 3 votes vote down vote up
private ConfigurableApplicationContext newApplicationContext(Class<?>... componentClasses) {

		AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();

		applicationContext.register(componentClasses);
		applicationContext.registerShutdownHook();
		applicationContext.refresh();

		this.applicationContext = applicationContext;

		return applicationContext;
	}
 
Example 10
Source File: JsonClientCacheDataImporterExporterIntegrationTests.java    From spring-boot-data-geode with Apache License 2.0 3 votes vote down vote up
public static void main(String[] args) {

			AnnotationConfigApplicationContext applicationContext =
				new AnnotationConfigApplicationContext(TestGeodeServerConfiguration.class);

			applicationContext.registerShutdownHook();
		}
 
Example 11
Source File: ClusterAvailableConfigurationIntegrationTests.java    From spring-boot-data-geode with Apache License 2.0 3 votes vote down vote up
public static void main(String[] args) {

			AnnotationConfigApplicationContext applicationContext =
				new AnnotationConfigApplicationContext(GeodeServerApplication.class);

			applicationContext.registerShutdownHook();
		}
 
Example 12
Source File: ClientCacheDataImportExportAutoConfigurationIntegrationTests.java    From spring-boot-data-geode with Apache License 2.0 3 votes vote down vote up
public static void main(String[] args) {

			AnnotationConfigApplicationContext applicationContext =
				new AnnotationConfigApplicationContext(TestGeodeServerConfiguration.class);

			applicationContext.registerShutdownHook();
		}
 
Example 13
Source File: ServerDefinedRegionTemplateAutoConfigurationIntegrationTests.java    From spring-boot-data-geode with Apache License 2.0 3 votes vote down vote up
public static void main(String[] args) {

			AnnotationConfigApplicationContext applicationContext =
				new AnnotationConfigApplicationContext(GemFireServerConfiguration.class);

			applicationContext.registerShutdownHook();
		}
 
Example 14
Source File: AutoConfiguredContinuousQueryIntegrationTests.java    From spring-boot-data-geode with Apache License 2.0 3 votes vote down vote up
public static void main(String[] args) {

			AnnotationConfigApplicationContext applicationContext =
				new AnnotationConfigApplicationContext(GemFireServerConfiguration.class);

			applicationContext.registerShutdownHook();
		}
 
Example 15
Source File: PetClinicApplicationSmokeTests.java    From spring-boot-data-geode with Apache License 2.0 3 votes vote down vote up
public static void main(String[] args) {

			AnnotationConfigApplicationContext applicationContext =
				new AnnotationConfigApplicationContext(GeodeServerTestConfiguration.class);

			applicationContext.registerShutdownHook();
		}