您的位置:首页 > 移动开发

applicationContext.getBean() 随处可获取basedao

2015-03-03 17:10 239 查看
在web.xml文件中配置

指定配置文件

<context-param>

    <param-name>contextConfigLocation</param-name>

    <param-value>/WEB-INF/classes/applicationContext.xml</param-value>

  </context-param>

声明web容器监听器

<listener>

    <listener-class>com.hhkj.jike.util.SringContextLOaderListen</listener-class>

  </listener>

SringContextLOaderListen该监听器需要继承监听器ContextLoaderListener

----------------------------------------------------------------------------

SringContextLOaderListen类代码如下

public class SringContextLOaderListen extends ContextLoaderListener {

    @Override

    public void contextDestroyed(ServletContextEvent event) {

        // TODO Auto-generated method stub

        super.contextDestroyed(event);

    }

    @Override

    public void contextInitialized(ServletContextEvent event) {

        // TODO Auto-generated method stub

        super.contextInitialized(event);

        System.out.println("成功");

        ServletContext context = event.getServletContext();  

        ApplicationContext ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(context);  //获取applicationContext

        SpringContentUtil.setApplicationContext(ctx); //将applicationContext  放入到SpringContentUtil类的静态变量中存储起来

        

    }

    @Override

    protected ContextLoader createContextLoader() {

        // TODO Auto-generated method stub

        return super.createContextLoader();

    }

    @Override

    public ContextLoader getContextLoader() {

        // TODO Auto-generated method stub

        return super.getContextLoader();

    }

    

}

-------------------------------------------------------------------------

SpringContentUtil 代码如下

public class SpringContentUtil {

     private static ApplicationContext context;  

       

        public static void setApplicationContext(ApplicationContext acx) {  

            context = acx;  

        }  

      

        public static ApplicationContext getApplicationContext() {  

            return context;  

        }  

      

        public static Object getBean(String name) throws BeansException {  

            return context.getBean(name);  

        }  

      

      

        public static Object getBean(String name, Class requiredType) throws BeansException {  

            return context.getBean(name, requiredType);  

        }  

      

        public static boolean containsBean(String name) {  

            return context.containsBean(name);  

        }  

      

        public static boolean isSingleton(String name) throws NoSuchBeanDefinitionException {  

            return context.isSingleton(name);  

        }  

      

        public static Class getType(String name) throws NoSuchBeanDefinitionException {  

            return context.getType(name);  

        }  

      

        public static String[] getAliases(String name) throws NoSuchBeanDefinitionException {  

            return context.getAliases(name);  

        }  

}

------------------------------------------------------

service中类写法

@Service("ling")

public class ImageUrlServiceImpl implements ImageUrlService {

    @Resource GenericDao  basedao;

     。。。。。。。。。。

}

----------------------------------------

调用获取上述service 

ImageUrlService  imageUrl = (ImageUrlService) SpringContentUtil.getBean("ling"); 

至此imageUrl变可调用该内中的一切执行方法
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  applicationContext