com.mizhousoft.commons.context.support.ApplicationContextRegistration Maven / Gradle / Ivy
The newest version!
package com.mizhousoft.commons.context.support;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import org.springframework.context.ApplicationContext;
import com.mizhousoft.commons.context.ServiceNotFoundException;
/**
* ApplicationContext Registration
*
* @version
*/
public abstract class ApplicationContextRegistration
{
private static final List applicationContexts = new CopyOnWriteArrayList();
/**
* 注册应用上下文
*
* @param applicationContext
*/
public static void registerApplicationContext(ApplicationContext applicationContext)
{
applicationContexts.add(applicationContext);
}
/**
* 注销应用上下文
*
* @param applicationContext
*/
public static void unregisterApplicationContext(ApplicationContext applicationContext)
{
applicationContexts.remove(applicationContext);
}
/**
* 获取Application Context Bean
*
* @param name
* @return
*/
public static Object getBean(String name)
{
Iterator iter = applicationContexts.iterator();
while (iter.hasNext())
{
ApplicationContext applicationContext = iter.next();
try
{
return applicationContext.getBean(name);
}
catch (Throwable e)
{
// ignore
}
}
throw new ServiceNotFoundException(name + " service not found.");
}
/**
* 获取Application Context Bean
*
* @param
*
* @param clazz
* @return
*/
public static T getBean(Class clazz)
{
Iterator iter = applicationContexts.iterator();
while (iter.hasNext())
{
ApplicationContext applicationContext = iter.next();
try
{
return applicationContext.getBean(clazz);
}
catch (Throwable e)
{
// ignore
}
}
throw new ServiceNotFoundException(clazz.getName() + " service not found.");
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy