package com.lzw.util;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionReaderUtils;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
/**
* @Description:运行期向spring加入动态代理类
* @author lzw
* @date 2013年9月28日 下午13:27:13
* @version V1.0
* @Copyright (c)
*/
public class InitServer implements ServletContextListener {
Logger logger = Logger.getLogger(InitServer.class);
private static ServletContext servletContext;
public static ServletContext getServletContext() {
return servletContext;
}
public void contextInitialized(ServletContextEvent contextEvent) {
servletContext = contextEvent.getServletContext();
final ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(servletContext);
SpringContextTool.setApplicationContext(ctx);
logger.info("服务启动!!");
}
/**
* @Description:运行期向spring加入被代理类
* @param className com.lzw.service.impl.HandelUserImpl
* @param beanName handelUser
* @throws ClassNotFoundException
*/
public static void registerBeanToSpringContext(String className, String beanName) throws ClassNotFoundException {
// 1 get BeanFactory
DefaultListableBeanFactory factory = (DefaultListableBeanFactory) WebApplicationContextUtils.getWebApplicationContext(
InitServer.getServletContext()).getAutowireCapableBeanFactory();
// 2 create Bean Definition
AbstractBeanDefinition beanDef = BeanDefinitionReaderUtils.createBeanDefinition(null, className,
WebApplicationContextUtils.getWebApplicationContext(InitServer.getServletContext()).getClassLoader());
//set scope
beanDef.setScope("prototype");
// 3 add Bean Definition to BeanFactory
factory.registerBeanDefinition(beanName, beanDef);
}
public void contextDestroyed(ServletContextEvent contextEvent) {}
}
运行期向spring加入动态代理类
最新推荐文章于 2022-04-19 14:39:02 发布