001 // Copyright 2007, 2008 The Apache Software Foundation
002 //
003 // Licensed under the Apache License, Version 2.0 (the "License");
004 // you may not use this file except in compliance with the License.
005 // You may obtain a copy of the License at
006 //
007 // http://www.apache.org/licenses/LICENSE-2.0
008 //
009 // Unless required by applicable law or agreed to in writing, software
010 // distributed under the License is distributed on an "AS IS" BASIS,
011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012 // See the License for the specific language governing permissions and
013 // limitations under the License.
014
015 package org.apache.tapestry5.ioc;
016
017 import org.apache.tapestry5.ioc.annotations.Scope;
018 import org.apache.tapestry5.ioc.def.ServiceDef;
019
020 /**
021 * Allows a module to bind service interfaces to service implementation classes in support of autobuilding services. A
022 * ServiceBinder is passed to to a method with the following signature: <code>public static void bind(ServiceBinder
023 * binder)</code>. This is an adaptation of ideas from <a href="http://code.google.com/p/google-guice/">Guice</a>.
024 */
025 public interface ServiceBinder
026 {
027 /**
028 * Defines a service in terms of an implementation class, without a service interface. In this case, the service
029 * will not be proxiable (proxying requires a service interface) and {@link ServiceDef#getServiceInterface()} will
030 * return the implementation class. In this situation, the service will not be proxied; it will be instantiated
031 * fully on first reference (ignoring its scope, if any) and will not be decorated.
032 *
033 * @param <T>
034 * @param implementationClass class to instantiate as the service
035 * @return binding options, used to specify additional details about the service
036 */
037 <T> ServiceBindingOptions bind(Class<T> implementationClass);
038
039 /**
040 * Alternative implementation that supports a callback to build the service, rather than instantiating a particular
041 * class.
042 *
043 * @param serviceInterface interface implemented by the service
044 * @param builder constructs the core service implementation
045 * @return binding options, used to specify additional details about the service
046 */
047 <T> ServiceBindingOptions bind(Class<T> serviceInterface, ServiceBuilder<T> builder);
048
049 /**
050 * Binds the service interface to a service implementation class. The default service name is the unqualified name
051 * of the service interface. The default service scope is "singleton", unless the service implementation class
052 * includes the {@link Scope} annotation.
053 *
054 * @param <T>
055 * @param serviceInterface service interface (used when locating services, and when building proxies)
056 * @param serviceImplementation implementation class that implements the service interface
057 * @return binding options, used to specify additional details about the service
058 */
059 <T> ServiceBindingOptions bind(Class<T> serviceInterface,
060 Class<? extends T> serviceImplementation);
061 }