- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我对 Spring 还很陌生,这段代码基于不同的教程,因此请在继续阅读时记住这一点。
这就是任务:
我正在使用 SpringMVC 在现有 Web 服务之上创建一个 Web 前端,该服务在 Websphere Application Server 8.5.5 上运行。
前端只是显示一些数据库表,没什么花哨的。
我尝试使用 WebapplicationContext.getBean()
方法,结果 Controller 出现 org.springframework.beans.factory.BeanCreationException 。其原因是我尝试 Autowiring bean 的行中出现 java.lang.NullPointerException。
因此我在 Stackoverflow 和 google 上搜索有类似问题的人,但找不到任何合适的帖子。
这是我的类(class)。
@ComponenScan 类
@Configuration
@ComponentScan(basePackages= {"com.xyz.webservice.services", "com.xyz.webservice.web"})
public class AppConfig {
}
Controller
@Controller
public class IndexController {
@Autowired //this works fine
private WebApplicationContext ctx;
//--> the error
/* this doesn't work
@Autowired
private GridService gs;
*/
//neither does this work
private GridService gs = (GridService) ctx.getBean("gridService");
//<--
@RequestMapping(value= {"", "/", "/index"}, method=RequestMethod.GET)
public ModelAndView showIndex(ModelAndView mv,
HttpSession session) {
Grid grid = getGrid();
...
return mv;
}
private Grid getGrid() {
return (Grid) ctx.getBean("grid"); //this works totally fine
}
...
}
服务
package com.xyz.webservice.services;
@Service
@Scope(value = WebApplicationContext.SCOPE_SESSION, proxyMode = ScopedProxyMode.TARGET_CLASS)
public class GridService {
...
}
POJO
package com.xyz.webservice.web;
@Component
@Scope(value = WebApplicationContext.SCOPE_SESSION, proxyMode = ScopedProxyMode.TARGET_CLASS)
public class Grid {
private List<String> columnNames;
private List<Page> pages;
private int currentPageNumber;
private CrmEmsHistoryProperties sortedBy;
private boolean isSortedDescending;
//Getters and Setters
GridService 提供了使用网格的方法。因此, Controller 注入(inject)了网格(通过使用 ctx.getBean("grid");)(它可以正常工作)和 GridService(它根本不起作用)。
预期结果: Controller 应该能够从 Spring 的 IoC-Container 注入(inject) GridService。
实际结果: Controller 无法从 Spring 的 IoC-Container 注入(inject) GridService。
WebApplicationContext.getBean() 抛出此异常:
[8/13/19 12:23:17:785 CEST] 0000008b ServletWrappe E com.ibm.ws.webcontainer.servlet.ServletWrapper init SRVE0271E: Uncaught init() exception created by servlet [dispatcher] in application [WebserviceEAR]: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'indexController': Unsatisfied dependency expressed through field 'gs'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.xyz.webservice.services.GridService' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:588)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:366)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1264)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:553)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:761)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:867)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:543)
at org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:668)
at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:540)
at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:494)
at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:171)
at javax.servlet.GenericServlet.init(GenericServlet.java:161)
at com.ibm.ws.webcontainer.servlet.ServletWrapper.init(ServletWrapper.java:345)
at com.ibm.ws.webcontainer.servlet.ServletWrapperImpl.init(ServletWrapperImpl.java:168)
at com.ibm.ws.webcontainer.servlet.ServletWrapper.handleRequest(ServletWrapper.java:637)
at com.ibm.ws.webcontainer.servlet.ServletWrapper.handleRequest(ServletWrapper.java:481)
at com.ibm.ws.webcontainer.servlet.ServletWrapperImpl.handleRequest(ServletWrapperImpl.java:178)
at com.ibm.ws.webcontainer.filter.WebAppFilterManager.invokeFilters(WebAppFilterManager.java:1114)
at com.ibm.ws.webcontainer.webapp.WebApp.handleRequest(WebApp.java:4047)
at com.ibm.ws.webcontainer.webapp.WebGroup.handleRequest(WebGroup.java:304)
at com.ibm.ws.webcontainer.WebContainer.handleRequest(WebContainer.java:1016)
at com.ibm.ws.webcontainer.WSWebContainer.handleRequest(WSWebContainer.java:1817)
at com.ibm.ws.webcontainer.channel.WCChannelLink.ready(WCChannelLink.java:213)
at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.handleDiscrimination(HttpInboundLink.java:463)
at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.handleNewRequest(HttpInboundLink.java:530)
at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.processRequest(HttpInboundLink.java:316)
at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.ready(HttpInboundLink.java:287)
at com.ibm.ws.tcp.channel.impl.NewConnectionInitialReadCallback.sendToDiscriminators(NewConnectionInitialReadCallback.java:214)
at com.ibm.ws.tcp.channel.impl.NewConnectionInitialReadCallback.complete(NewConnectionInitialReadCallback.java:113)
at com.ibm.ws.tcp.channel.impl.AioReadCompletionListener.futureCompleted(AioReadCompletionListener.java:175)
at com.ibm.io.async.AbstractAsyncFuture.invokeCallback(AbstractAsyncFuture.java:217)
at com.ibm.io.async.AsyncChannelFuture.fireCompletionActions(AsyncChannelFuture.java:161)
at com.ibm.io.async.AsyncFuture.completed(AsyncFuture.java:138)
at com.ibm.io.async.ResultHandler.complete(ResultHandler.java:204)
at com.ibm.io.async.ResultHandler.runEventProcessingLoop(ResultHandler.java:775)
at com.ibm.io.async.ResultHandler$2.run(ResultHandler.java:905)
at com.ibm.ws.util.ThreadPool$Worker.run(ThreadPool.java:1892)
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.xyz.webservice.services.GridService' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1493)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1104)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1066)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:585)
... 42 more
@Autowire 抛出此异常:
[8/13/19 12:14:35:106 CEST] 00000086 ServletWrappe E com.ibm.ws.webcontainer.servlet.ServletWrapper init SRVE0271E: Uncaught init() exception created by servlet [dispatcher] in application [WebserviceEAR]: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'indexController' defined in file [/home/abc/Repositories/JEE/.metadata/.plugins/org.eclipse.wst.server.core/tmp1/Webservice/WEB-INF/classes/com/xyz/webservice/controller/IndexController.class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.xyz.webservice.controller.IndexController]: Constructor threw exception; nested exception is java.lang.NullPointerException
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1155)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1099)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:513)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:761)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:867)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:543)
at org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:668)
at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:540)
at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:494)
at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:171)
at javax.servlet.GenericServlet.init(GenericServlet.java:161)
at com.ibm.ws.webcontainer.servlet.ServletWrapper.init(ServletWrapper.java:345)
at com.ibm.ws.webcontainer.servlet.ServletWrapperImpl.init(ServletWrapperImpl.java:168)
at com.ibm.ws.webcontainer.servlet.ServletWrapper.load(ServletWrapper.java:1378)
at com.ibm.ws.webcontainer.filter.WebAppFilterManager.invokeFilters(WebAppFilterManager.java:1027)
at com.ibm.ws.webcontainer.servlet.CacheServletWrapper.handleRequest(CacheServletWrapper.java:87)
at com.ibm.ws.webcontainer.WebContainer.handleRequest(WebContainer.java:949)
at com.ibm.ws.webcontainer.WSWebContainer.handleRequest(WSWebContainer.java:1817)
at com.ibm.ws.webcontainer.channel.WCChannelLink.ready(WCChannelLink.java:213)
at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.handleDiscrimination(HttpInboundLink.java:463)
at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.handleNewRequest(HttpInboundLink.java:530)
at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.processRequest(HttpInboundLink.java:316)
at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.ready(HttpInboundLink.java:287)
at com.ibm.ws.tcp.channel.impl.NewConnectionInitialReadCallback.sendToDiscriminators(NewConnectionInitialReadCallback.java:214)
at com.ibm.ws.tcp.channel.impl.NewConnectionInitialReadCallback.complete(NewConnectionInitialReadCallback.java:113)
at com.ibm.ws.tcp.channel.impl.AioReadCompletionListener.futureCompleted(AioReadCompletionListener.java:175)
at com.ibm.io.async.AbstractAsyncFuture.invokeCallback(AbstractAsyncFuture.java:217)
at com.ibm.io.async.AsyncChannelFuture.fireCompletionActions(AsyncChannelFuture.java:161)
at com.ibm.io.async.AsyncFuture.completed(AsyncFuture.java:138)
at com.ibm.io.async.ResultHandler.complete(ResultHandler.java:204)
at com.ibm.io.async.ResultHandler.runEventProcessingLoop(ResultHandler.java:775)
at com.ibm.io.async.ResultHandler$2.run(ResultHandler.java:905)
at com.ibm.ws.util.ThreadPool$Worker.run(ThreadPool.java:1892)
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.xyz.webservice.controller.IndexController]: Constructor threw exception; nested exception is java.lang.NullPointerException
at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:154)
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:89)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1147)
... 37 more
Caused by: java.lang.NullPointerException
at com.xyz.webservice.controller.IndexController.<init>(IndexController.java:24)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:83)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:57)
at java.lang.reflect.Constructor.newInstance(Constructor.java:437)
at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:142)
... 39 more
希望有人能帮我解决这个问题。
最佳答案
这不起作用,因为在 Bean 生命周期构造中发生在依赖项注入(inject)之前,因此在 gs
初始化时 ctx
尚未注入(inject)并且等于空
private GridService gs = (GridService) ctx.getBean("gridService");
相反,你应该按照以下方式进行
private GridService gs;
@PostConstruct
private void init() {
gs = (GridService) ctx.getBean("gridService");
}
方法将在所有注入(inject)后调用,因此 gs
的初始化将正确发生。
关于java - 无法使用 @Autowired/WebApplicationContext.getBean() 获取 Bean,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57476276/
我需要 ApplicationContext.xml 的上下文,我在 web.xml 中将其提供为 org.springframework.web.context.Cont
上一篇解析了 DispatcherServlet 与 ContextLoaderListener 这两个类,解析了应用初始化与请求处理的流程,但还有一些组件需要解析: ConfigurableWebA
我有这个测试类的代码: @ContextConfiguration(locations = { "classpath:/test/BeanConfig.xml" }) @WebAppConfigura
我想从桌面应用程序启动嵌入式 Tomcat 服务器。在 Tomcat 容器内运行的 web 应用程序是通过 Spring 的 WebApplicationInitializer 配置的(现在我只是扩展
我是 spring MVC 的新手。所以我从非常简单的 MVC 应用程序开始。在此应用程序中,我正在尝试提交登录表单,但出现以下提到的错误: SEVERE: Servlet.service() for
在 Spring 框架引用中我发现了这个: The ApplicationContext interface has a few other methods for retrieving beans,
我知道配置 OSIVF 是一个常见的痛点。我已经阅读了过去几天能找到的所有页面,但似乎没有什么能让我解决这个问题。我已经成功地让自己感到困惑了。我试图保持这个配置非常简单,因为网络应用程序非常简单。嗯
我写了这个测试类: @ContextConfiguration(locations = { "classpath:/test/BeanConfig.xml" }) public class Candi
我在我的应用程序中声明了两个 Spring 上下文 - 一个用于 Spring-MVC 请求,另一个用于 Flex/BlazeDS messagebroker 请求,映射到不同的 url-patter
我是 Spring Security 新手 我有 web.xml contextConfigLocation /WEB-INF/applicationContext-s
我有一个 Spring 应用程序。出于某些原因,我有一个标准的 servlet,我需要该 servlet 才能访问一些 spring bean(我知道这并不理想,将来我会寻找更好的东西)。 在 Web
这个问题已经有答案了: No WebApplicationContext found: no ContextLoaderListener registered? (2 个回答) 已关闭 6 年前。 我
我注意到org.springframework.web.servlet.mvc.AbstractController中有getWebApplicationContext。这意味着spring程序员可以
这个问题已经有答案了: Difference between applicationContext.xml and spring-servlet.xml in Spring Framework (6
我遇到过这样的情况:我使用 ServletRegistrationBean 向 Spring 父 Web 应用程序上下文注册两个子 Web 应用程序上下文,并为每个子 Web 应用程序上下文提供不同的
在Tomcat(或其他服务器)中部署了两个WAR,foo1.war 和foo2.war。所有的spring bean都定义在foo1.war中,但是有没有可能在foo2.war中部署的servlet中
这个问题在这里已经有了答案: No WebApplicationContext found: no ContextLoaderListener registered? (2 个答案) 关闭 5 年前
我有一个带有两个 web 应用程序(foo 和 bar)的 tomcat 服务器,它们具有相同的部署 war 。部署使用标准的 Spring/Hibernate 设置。我假设这两个 webapps 将
当我关闭 Tomcat 时,我观察到 Spring WebApplicationContext 的正确关闭和清理。但是,当我重新部署基于 Spring 的 WAR(通过将新 WAR 复制到 webap
我正在尝试创建一个简单的 Spring 3 应用程序并拥有以下文件。请告诉我这个错误的原因 下面是我的 web.xml Spring2 index.jsp
我是一名优秀的程序员,十分优秀!