开发者社区> 问答> 正文

spring mvc里, jsp中怎么获取bean

spring mvc里, jsp中怎么调用service,service是使用注解的方式注入的,用WebApplicationContext弄不出来

service里的代码

@Service("systemService")
public class SystemService extends BaseService
 {
//数据库操作
}
JSP代码

<%@ page language="java" pageEncoding="UTF-8"%>
<%@ page import="com.bada.service.system.SystemService,java.util.*,
com.bada.model.system.SystemParam,org.springframework.web.context.WebApplicationContext
,org.springframework.web.context.support.WebApplicationContextUtils
,org.springframework.context.ApplicationContext,com.bada.service.SpringUtils" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<c:set var="path" value="${pageContext.request.contextPath }" />
<%
ServletContext context = request.getSession().getServletContext();
ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(context);
WebApplicationContextUtils.getWebApplicationContext(application).getBean("systemService");
//SystemService sysservice=(SystemService)ctx.getBean("systemService"); 
SystemService sysservice=(SystemService)SpringUtils.getApplicationContext().getBean("systemService");
%>
web.xml里配置了监听器

listener> 
    <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>

展开
收起
a123456678 2016-03-17 14:48:20 3811 0
1 条回答
写回答
取消 提交回答
  • SprintUtils , 在你的jsp页面上面import 这个类。然后通过

    SpringUtils.getApplicationContext().getBean("dataSource")
    获取指定的bean

    package com.jsits.core;
     
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.beans.BeansException;
    import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
    import org.springframework.beans.factory.config.BeanPostProcessor;
    import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
    import org.springframework.beans.factory.support.BeanDefinitionRegistry;
    import org.springframework.beans.factory.xml.ResourceEntityResolver;
    import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.ApplicationContextAware;
    import org.springframework.context.ConfigurableApplicationContext;
    import org.springframework.core.io.FileSystemResource;
    import org.springframework.stereotype.Component;
     
    @Component
    public class SpringUtils implements BeanFactoryPostProcessor,
            BeanPostProcessor, ApplicationContextAware {
     
        protected static Logger logger = LoggerFactory.getLogger(SpringUtils.class);
     
        private static ConfigurableApplicationContext applicationContext = null;
     
        private static ConfigurableListableBeanFactory beanFactory = null;
     
        @Override
        public void setApplicationContext(ApplicationContext applicationContext)
                throws BeansException {
            SpringUtils.applicationContext = (ConfigurableApplicationContext) applicationContext;
        }
     
        public static ConfigurableApplicationContext getApplicationContext() {
            return applicationContext;
        }
         
        public static ConfigurableListableBeanFactory getConfigurableListableBeanFactory() {
            return beanFactory;
        }
     
        @Override
        public void postProcessBeanFactory(
                ConfigurableListableBeanFactory beanFactory) throws BeansException {
            SpringUtils.beanFactory = beanFactory;
        }
     
        @Override
        public Object postProcessBeforeInitialization(Object bean, String beanName)
                throws BeansException {
            return bean;
        }
     
        @Override
        public Object postProcessAfterInitialization(Object bean, String beanName)
                throws BeansException {
            return bean;
        }
     
     
        public static void registerBean(String resource) {
            BeanDefinitionRegistry beanDefinitionRegistry = ((BeanDefinitionRegistry)getConfigurableListableBeanFactory());
     
            XmlBeanDefinitionReader definitionReader = new XmlBeanDefinitionReader(
                    beanDefinitionRegistry);
     
            definitionReader.setResourceLoader(getApplicationContext());
            definitionReader.setEntityResolver(new ResourceEntityResolver(
                    getApplicationContext()));
     
            definitionReader.setValidating(false);
            definitionReader.setValidationMode(XmlBeanDefinitionReader.VALIDATION_NONE);
             
            definitionReader.loadBeanDefinitions(new FileSystemResource(resource));
        }
     
        public static void unregisterBean(String beanName) {
            if (containsBean(beanName)) {
                BeanDefinitionRegistry beanDefinitionRegistry = ((BeanDefinitionRegistry)getConfigurableListableBeanFactory());
                beanDefinitionRegistry.removeBeanDefinition(beanName);
            }
        }
     
        public static boolean containsBean(String beanName) {
            return getConfigurableListableBeanFactory().containsBeanDefinition(beanName);
        }
    }
    2019-07-17 19:05:23
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
云栖社区特邀专家徐雷Java Spring Boot开发实战系列课程(第20讲):经典面试题与阿里等名企内部招聘求职面试技巧 立即下载
微服务架构模式与原理Spring Cloud开发实战 立即下载
阿里特邀专家徐雷Java Spring Boot开发实战系列课程(第18讲):制作Java Docker镜像与推送到DockerHub和阿里云Docker仓库 立即下载

相关实验场景

更多