![JAR search and dependency download from the Maven repository](/logo.png)
com.quartzdesk.api.spring.ApplicationContextAccessor Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of quartzdesk-api Show documentation
Show all versions of quartzdesk-api Show documentation
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.
The newest version!
/*
* Copyright (c) 2013-2024 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.beans.factory.DisposableBean;
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, DisposableBean
{
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 );
}
@Override
public void destroy()
{
ApplicationContextAccessor.applicationContext = null;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy