亚洲城官方网站Spring怎么知道HttpServletRequest类的存在的?

( 亚洲城官方网站 )

package test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

public class Test {
    /**
     * @param args
     */
    public static void mainString[] args {
        ApplicationContext ac=new FileSystemXmlApplicationContext"D:\spring.xml";
    }
}
   


Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name hqController: Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private javax.servlet.http.HttpServletRequest gnnt.hq.site.controller.HqController.request; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [javax.servlet.http.HttpServletRequest] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowiredrequired=true}
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValuesAutowiredAnnotationBeanPostProcessor.java:292
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBeanAbstractAutowireCapableBeanFactory.java:1185
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBeanAbstractAutowireCapableBeanFactory.java:537
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanAbstractAutowireCapableBeanFactory.java:475
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObjectAbstractBeanFactory.java:304
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingletonDefaultSingletonBeanRegistry.java:228
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBeanAbstractBeanFactory.java:300
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBeanAbstractBeanFactory.java:195
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletonsDefaultListableBeanFactory.java:700
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitializationAbstractApplicationContext.java:760
    at org.springframework.context.support.AbstractApplicationContext.refreshAbstractApplicationContext.java:482
    at org.springframework.context.support.FileSystemXmlApplicationContext.<init>FileSystemXmlApplicationContext.java:140
    at org.springframework.context.support.FileSystemXmlApplicationContext.<init>FileSystemXmlApplicationContext.java:84
    at test.Test.mainTest.java:14
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private javax.servlet.http.HttpServletRequest gnnt.hq.site.controller.HqController.request; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [javax.servlet.http.HttpServletRequest] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowiredrequired=true}
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.injectAutowiredAnnotationBeanPostProcessor.java:508
    at org.springframework.beans.factory.annotation.InjectionMetadata.injectInjectionMetadata.java:87
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValuesAutowiredAnnotationBeanPostProcessor.java:289
    ... 13 more
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [javax.servlet.http.HttpServletRequest] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowiredrequired=true}
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionExceptionDefaultListableBeanFactory.java:1100
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependencyDefaultListableBeanFactory.java:960
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependencyDefaultListableBeanFactory.java:855
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.injectAutowiredAnnotationBeanPostProcessor.java:480
@Controller
public class HqController
{
    @Autowired
    @Qualifier"hqService"
    private IGetHqService service;
    @Autowired  
    private  HttpServletRequest request;

以web项目在tomcat中运行没有问题,直接获取spring容器的时候就报错了。原因是在给HqController注入HttpServletRequest类的时候失败了。那么为什么部署在tomcat中启动就能注入成功呢?
以<bean>和注解的形式让spring创造bean,为什么部署在tomcat中启动时,Spring就会HttpServletRequest用来注入了呢?

因为tomcat作为一个Servlet的容器,是有HttpServletRequestHttpServletResponse的实现的,叫org.apache.catalina.connector.RequestFacadeorg.apache.catalina.connector.ResponseFacade。你要想在本地运行,就得加上内嵌的tomcat或jetty之类的容器依赖了。

Servlet相关的对象是由容器创建

(看完/读完)这篇文章有何感想! 亚洲城官方网站的分享…

发表评论

电子邮件地址不会被公开。 必填项已用*标注