/**
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.soybeanMilk.example.spring;
import java.io.Serializable;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import org.soybeanMilk.core.exe.support.ResolverObjectFactory;
import org.soybeanMilk.example.spring.org.springframework.web.context.WebApplicationContext;
import org.soybeanMilk.example.spring.org.springframework.web.context.support.WebApplicationContextUtils;
/**
* SoybeanMilkʾ��
* @author earthangry@gmail.com
* @date 2012-6-2
*/
public class SpringResolverObjectFactoryListener implements ServletContextListener
{
public static final String SPRING_RESOLVER_FACTORY_KEY="springResolverObjectFactory";
public void contextDestroyed(ServletContextEvent event)
{
event.getServletContext().removeAttribute(SPRING_RESOLVER_FACTORY_KEY);
}
public void contextInitialized(ServletContextEvent event)
{
ResolverObjectFactory rof=null;
ServletContext servletContext=event.getServletContext();
final WebApplicationContext springContext=WebApplicationContextUtils.getWebApplicationContext(servletContext);
if(springContext != null)
{
rof=new ResolverObjectFactory()
{
public Object getResolverObject(Serializable resolverObjectId)
{
String name=(resolverObjectId instanceof String ? (String)resolverObjectId : resolverObjectId.toString());
if(springContext.containsBean(name))
return springContext.getBeans(name);
else
return null;
}
public void addResolverObject(Serializable resolverObjectId, Object resolverObject){}
};
}
servletContext.setAttribute(SPRING_RESOLVER_FACTORY_KEY, rof);
}
}