001/*
002 * Copyright 2002-2012 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.web.context.support;
018
019import javax.servlet.ServletContext;
020
021import org.apache.commons.logging.Log;
022import org.apache.commons.logging.LogFactory;
023
024import org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor;
025import org.springframework.util.Assert;
026import org.springframework.util.ClassUtils;
027import org.springframework.web.context.ContextLoader;
028import org.springframework.web.context.WebApplicationContext;
029
030/**
031 * Convenient base class for self-autowiring classes that gets constructed
032 * within a Spring-based web application. Resolves {@code @Autowired}
033 * annotations in the endpoint class against beans in the current Spring
034 * root web application context (as determined by the current thread's
035 * context ClassLoader, which needs to be the web application's ClassLoader).
036 * Can alternatively be used as a delegate instead of as a base class.
037 *
038 * <p>A typical usage of this base class is a JAX-WS endpoint class:
039 * Such a Spring-based JAX-WS endpoint implementation will follow the
040 * standard JAX-WS contract for endpoint classes but will be 'thin'
041 * in that it delegates the actual work to one or more Spring-managed
042 * service beans - typically obtained using {@code @Autowired}.
043 * The lifecycle of such an endpoint instance will be managed by the
044 * JAX-WS runtime, hence the need for this base class to provide
045 * {@code @Autowired} processing based on the current Spring context.
046 *
047 * <p><b>NOTE:</b> If there is an explicit way to access the ServletContext,
048 * prefer such a way over using this class. The {@link WebApplicationContextUtils}
049 * class allows for easy access to the Spring root web application context
050 * based on the ServletContext.
051 *
052 * @author Juergen Hoeller
053 * @since 2.5.1
054 * @see WebApplicationObjectSupport
055 */
056public abstract class SpringBeanAutowiringSupport {
057
058        private static final Log logger = LogFactory.getLog(SpringBeanAutowiringSupport.class);
059
060
061        /**
062         * This constructor performs injection on this instance,
063         * based on the current web application context.
064         * <p>Intended for use as a base class.
065         * @see #processInjectionBasedOnCurrentContext
066         */
067        public SpringBeanAutowiringSupport() {
068                processInjectionBasedOnCurrentContext(this);
069        }
070
071
072        /**
073         * Process {@code @Autowired} injection for the given target object,
074         * based on the current web application context.
075         * <p>Intended for use as a delegate.
076         * @param target the target object to process
077         * @see org.springframework.web.context.ContextLoader#getCurrentWebApplicationContext()
078         */
079        public static void processInjectionBasedOnCurrentContext(Object target) {
080                Assert.notNull(target, "Target object must not be null");
081                WebApplicationContext cc = ContextLoader.getCurrentWebApplicationContext();
082                if (cc != null) {
083                        AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor();
084                        bpp.setBeanFactory(cc.getAutowireCapableBeanFactory());
085                        bpp.processInjection(target);
086                }
087                else {
088                        if (logger.isDebugEnabled()) {
089                                logger.debug("Current WebApplicationContext is not available for processing of " +
090                                                ClassUtils.getShortName(target.getClass()) + ": " +
091                                                "Make sure this class gets constructed in a Spring web application. Proceeding without injection.");
092                        }
093                }
094        }
095
096
097        /**
098         * Process {@code @Autowired} injection for the given target object,
099         * based on the current root web application context as stored in the ServletContext.
100         * <p>Intended for use as a delegate.
101         * @param target the target object to process
102         * @param servletContext the ServletContext to find the Spring web application context in
103         * @see WebApplicationContextUtils#getWebApplicationContext(javax.servlet.ServletContext)
104         */
105        public static void processInjectionBasedOnServletContext(Object target, ServletContext servletContext) {
106                Assert.notNull(target, "Target object must not be null");
107                WebApplicationContext cc = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
108                AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor();
109                bpp.setBeanFactory(cc.getAutowireCapableBeanFactory());
110                bpp.processInjection(target);
111        }
112
113}