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>
JSP里报错说:org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'systemService' is defined
或者说,
SprintUtils,在你的jsp页面上面import这个类。然后通过
SpringUtils. getApplicationContext ().getBean( "dataSource" )获取指定的bean packagecom.jsits.core;importorg.slf4j.Logger;importorg.slf4j.LoggerFactory;importorg.springframework.beans.BeansException;importorg.springframework.beans.factory.config.BeanFactoryPostProcessor;importorg.springframework.beans.factory.config.BeanPostProcessor;importorg.springframework.beans.factory.config.ConfigurableListableBeanFactory;importorg.springframework.beans.factory.support.BeanDefinitionRegistry;importorg.springframework.beans.factory.xml.ResourceEntityResolver;importorg.springframework.beans.factory.xml.XmlBeanDefinitionReader;importorg.springframework.context.ApplicationContext;importorg.springframework.context.ApplicationContextAware;importorg.springframework.context.ConfigurableApplicationContext;importorg.springframework.core.io.FileSystemResource;importorg.springframework.stereotype.Component;@ComponentpublicclassSpringUtilsimplementsBeanFactoryPostProcessor,BeanPostProcessor,ApplicationContextAware{protectedstaticLoggerlogger=LoggerFactory.getLogger(SpringUtils.class);privatestaticConfigurableApplicationContextapplicationContext=null;privatestaticConfigurableListableBeanFactorybeanFactory=null;@OverridepublicvoidsetApplicationContext(ApplicationContextapplicationContext)throwsBeansException{SpringUtils.applicationContext=(ConfigurableApplicationContext)applicationContext;}publicstaticConfigurableApplicationContextgetApplicationContext(){returnapplicationContext;}publicstaticConfigurableListableBeanFactorygetConfigurableListableBeanFactory(){returnbeanFactory;}@OverridepublicvoidpostProcessBeanFactory(ConfigurableListableBeanFactorybeanFactory)throwsBeansException{SpringUtils.beanFactory=beanFactory;}@OverridepublicObjectpostProcessBeforeInitialization(Objectbean,StringbeanName)throwsBeansException{returnbean;}@OverridepublicObjectpostProcessAfterInitialization(Objectbean,StringbeanName)throwsBeansException{returnbean;}publicstaticvoidregisterBean(Stringresource){BeanDefinitionRegistrybeanDefinitionRegistry=((BeanDefinitionRegistry)getConfigurableListableBeanFactory());XmlBeanDefinitionReaderdefinitionReader=newXmlBeanDefinitionReader(beanDefinitionRegistry);definitionReader.setResourceLoader(getApplicationContext());definitionReader.setEntityResolver(newResourceEntityResolver(getApplicationContext()));definitionReader.setValidating(false);definitionReader.setValidationMode(XmlBeanDefinitionReader.VALIDATION_NONE);definitionReader.loadBeanDefinitions(newFileSystemResource(resource));}publicstaticvoidunregisterBean(StringbeanName){if(containsBean(beanName)){BeanDefinitionRegistrybeanDefinitionRegistry=((BeanDefinitionRegistry)getConfigurableListableBeanFactory());beanDefinitionRegistry.removeBeanDefinition(beanName);}}publicstaticbooleancontainsBean(StringbeanName){returngetConfigurableListableBeanFactory().containsBeanDefinition(beanName);}}
或者下面这个,额,忘记具体是哪个了,你都加上吧
回复 @ForEleven:改了下代码,解决了回复 @正厅级二奶:用我给你的那个SpringUtils试试看回复 @ForEleven:都加过了回复 @正厅级二奶:两个都加上呢?ContextLoaderListener我默认就有的WebApplicationContextUtils.getWebApplicationContext(application).getBean("studentService");还是没搞出来郁闷你的结构上正在走入歧途~~看到jsp里这么多坑爹的代码真是醉倒了
你正在走向歧途~祝您旅途愉快~
试试:ctx.getBean(SystemService.class);版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。