开发者社区> 问答> 正文

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>

JSP里报错说:org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'systemService' is defined

或者说,

使用注解的时候,jsp如何获得bean对象

展开
收起
爱吃鱼的程序员 2020-06-14 19:55:44 585 0
1 条回答
写回答
取消 提交回答
  • https://developer.aliyun.com/profile/5yerqm5bn5yqg?spm=a2c6h.12873639.0.0.6eae304abcjaIB

    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:加上了还是不行,我修改了问题,麻烦再帮我看看回复 @正厅级二奶:你的web.xml中有没有这个Listener<listener><listener-class>org.springframework.web.context.request.RequestContextListener</listener-class></listener>回复 @ForEleven:我后来@Service("sysservice")这么写了还是获取不到回复 @正厅级二奶:比如有一个Bean的类名是DemoService,使用注解他的默认名是demoService回复 @正厅级二奶:用了注解不是说没有名字了,比如你@Service注解里面可以定义这个bean的名字,如果不定义,默认是Bean的首字母小写@Autowired啊

    引用来自“CrazyHarry”的评论

    @Autowired啊 @正厅级二奶噢回复 @CrazyHarry:JSP里不能有注解的jsp里难道就不可以吗,同样的是在写Javacode啊Web.xml中加上这个Listener <listener><listener-class>org.springframework.web.context.request.RequestContextListener</listener-class></listener>

    <listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener>



    或者下面这个,额,忘记具体是哪个了,你都加上吧

    回复 @ForEleven:改了下代码,解决了回复 @正厅级二奶:用我给你的那个SpringUtils试试看回复 @ForEleven:都加过了回复 @正厅级二奶:两个都加上呢?ContextLoaderListener我默认就有的WebApplicationContextUtils.getWebApplicationContext(application).getBean("studentService");还是没搞出来郁闷你的结构上正在走入歧途~~

    引用来自“平安北京”的评论

    WebApplicationContextUtils.getWebApplicationContext(application).getBean("studentService");

    引用来自“sxgkwei”的评论

    你的结构上正在走入歧途~~我只能呵呵的祝福你旅途愉快了。

    看到jsp里这么多坑爹的代码真是醉倒了

    你正在走向歧途~祝您旅途愉快~

    试试:ctx.getBean(SystemService.class);
    公共数据可以考虑用过滤器获取后放入request
    2020-06-15 10:55:20
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

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

相关实验场景

更多