001/* 002 * Copyright 2002-2007 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * https://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.springframework.beans.factory.serviceloader; 018 019import java.util.ServiceLoader; 020 021import org.springframework.beans.factory.BeanClassLoaderAware; 022import org.springframework.beans.factory.config.AbstractFactoryBean; 023import org.springframework.util.Assert; 024import org.springframework.util.ClassUtils; 025 026/** 027 * Abstract base class for FactoryBeans operating on the 028 * JDK 1.6 {@link java.util.ServiceLoader} facility. 029 * 030 * @author Juergen Hoeller 031 * @since 2.5 032 * @see java.util.ServiceLoader 033 */ 034public abstract class AbstractServiceLoaderBasedFactoryBean extends AbstractFactoryBean<Object> 035 implements BeanClassLoaderAware { 036 037 private Class<?> serviceType; 038 039 private ClassLoader beanClassLoader = ClassUtils.getDefaultClassLoader(); 040 041 042 /** 043 * Specify the desired service type (typically the service's public API). 044 */ 045 public void setServiceType(Class<?> serviceType) { 046 this.serviceType = serviceType; 047 } 048 049 /** 050 * Return the desired service type. 051 */ 052 public Class<?> getServiceType() { 053 return this.serviceType; 054 } 055 056 @Override 057 public void setBeanClassLoader(ClassLoader beanClassLoader) { 058 this.beanClassLoader = beanClassLoader; 059 } 060 061 062 /** 063 * Delegates to {@link #getObjectToExpose(java.util.ServiceLoader)}. 064 * @return the object to expose 065 */ 066 @Override 067 protected Object createInstance() { 068 Assert.notNull(getServiceType(), "Property 'serviceType' is required"); 069 return getObjectToExpose(ServiceLoader.load(getServiceType(), this.beanClassLoader)); 070 } 071 072 /** 073 * Determine the actual object to expose for the given ServiceLoader. 074 * <p>Left to concrete subclasses. 075 * @param serviceLoader the ServiceLoader for the configured service class 076 * @return the object to expose 077 */ 078 protected abstract Object getObjectToExpose(ServiceLoader<?> serviceLoader); 079 080}