001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.spring.util;
016    
017    import com.liferay.portal.bean.BeanLocatorImpl;
018    import com.liferay.portal.kernel.bean.BeanLocator;
019    import com.liferay.portal.kernel.bean.PortalBeanLocatorUtil;
020    import com.liferay.portal.spring.context.ArrayApplicationContext;
021    import com.liferay.portal.util.ClassLoaderUtil;
022    
023    import java.util.List;
024    
025    import org.springframework.context.support.AbstractApplicationContext;
026    
027    /**
028     * <p>
029     * In most cases, SpringUtil.setContext() would have been called by
030     * com.liferay.portal.spring.context.PortalContextLoaderListener, configured in
031     * web.xml for the web application. However, there will be times in which
032     * SpringUtil will be called in a non-web application and, therefore, require
033     * manual instantiation of the application context.
034     * </p>
035     *
036     * @author Michael Young
037     */
038    public class SpringUtil {
039    
040            public static void loadContext(List<String> configLocations) {
041                    AbstractApplicationContext applicationContext =
042                            new ArrayApplicationContext(
043                                    configLocations.toArray(new String[configLocations.size()]));
044    
045                    BeanLocator beanLocator = new BeanLocatorImpl(
046                            ClassLoaderUtil.getPortalClassLoader(), applicationContext);
047    
048                    PortalBeanLocatorUtil.setBeanLocator(beanLocator);
049            }
050    
051    }