All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.quartzdesk.api.spring.ApplicationContextAccessor Maven / Gradle / Ivy

Go to download

QuartzDesk Public API library required for QuartzDesk Standard and Enterprise edition installations. This library must be placed on the classpath of the Quartz scheduler based application that is managed by QuartzDesk. It is important that this library is loaded by the same classloader that loads the Quartz scheduler API used by the application.

There is a newer version: 5.0.3
Show newest version
/*
 * Copyright (c) 2013-2019 QuartzDesk.com. All Rights Reserved.
 * QuartzDesk.com PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */

package com.quartzdesk.api.spring;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ApplicationContextException;
import org.springframework.stereotype.Component;

/**
 * A Spring utility class that provides access to the current Spring application context and all beans registered in
 * that context to all non-Spring components.
 * 

* In order to use this utility class, the class needs to be registered with Spring like so: *

*
 *   <bean id="applicationContextAccessor" class="com.quartzdesk.api.spring.ApplicationContextAccessor"/>
 * 
*/ @Component public class ApplicationContextAccessor implements ApplicationContextAware { private static ApplicationContext applicationContext; /** * Returns the Spring application context. * * @return the Spring application context. */ private static ApplicationContext getApplicationContext() { if ( applicationContext == null ) throw new ApplicationContextException( "Spring application context cannot be accessed because " + ApplicationContextAccessor.class.getName() + " is not registered with Spring." ); return applicationContext; } /** * Sets the Spring application context. * * @param applicationContext an application context. */ @Override public void setApplicationContext( ApplicationContext applicationContext ) { /* * A bit ugly assignment to a static field from a non-static context, but there is no other way... */ ApplicationContextAccessor.applicationContext = applicationContext; } /** * Returns the Spring bean with the specified type. * * @param beanType a bean class. * @param a bean type. * @return the Spring bean with the specified type. * @throws BeansException if an error occurs. */ public static T getBean( Class beanType ) throws BeansException { ApplicationContext appCtx = getApplicationContext(); return appCtx.getBean( beanType ); } /** * Returns the Spring bean with the specified name. * * @param name a bean name. * @param beanType a bean class. * @param a bean type. * @return the Spring bean with the specified type. * @throws BeansException if an error occurs. */ public static T getBean( String name, Class beanType ) throws BeansException { ApplicationContext appCtx = getApplicationContext(); return appCtx.getBean( name, beanType ); } /** * Returns the Spring bean with the specified name. * * @param name a bean name. * @param a bean type. * @return the Spring bean with the specified name. * @throws BeansException if an error occurs. */ @SuppressWarnings( "unchecked" ) public static T getBean( String name ) throws BeansException { ApplicationContext appCtx = getApplicationContext(); return (T) appCtx.getBean( name ); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy