使用组件扫描(弹簧)的问题 - 不注入依赖项

时间:2014-07-08 07:00:33

标签: spring dependency-injection component-scan

我正在开始一个新的应用程序开发,我收到了一些代码(原型)。我注意到spring-servlet.xml正在使用标记:

<context:component-scan...

之前我从未使用过那个标签,我很困惑。我想一些配置丢失了,因为我的映射不起作用。

如果我像

一样使用它

<context:component-scan base-package="my.app.mainpackage">,该应用无法部署

如果我使用它:

<context:component-scan base-package="my.app.mainpackage.controller" />

应用程序部署,只要我不在我的控制器上使用@Autowired(否则它也不会部署),但随后,控制器包中的所有其他软件包赢了&#39 ; t注入了依赖项。

最后,我无法注入依赖项!

服务器日志在启动时显示错误:

      "Calling "C:\jboss-eap-6.0\bin\standalone.conf.bat"
    ===============================================================================

      JBoss Bootstrap Environment

      JBOSS_HOME: C:\jboss-eap-6.0

      JAVA: C:\jdk1.7.0_07\bin\java

      JAVA_OPTS: -XX:+TieredCompilation -XX:+UseCompressedOops -Dprogram.name=standalone.bat -Xms1303M -Xmx1303M -XX:MaxPerm
    Size=256M -Dsun.rmi.dgc.client.gcInterval=3600000 -Dsun.rmi.dgc.server.gcInterval=3600000 -Djava.net.preferIPv4Stack=tru
    e -Dorg.jboss.resolver.warning=true -Djboss.modules.system.pkgs=org.jboss.byteman -Djboss.server.default.config=standalo
    ne.xml

    ===============================================================================

    17:37:30,602 INFO  [org.jboss.modules] JBoss Modules version 1.1.2.GA-redhat-1
    17:37:31,296 INFO  [org.jboss.msc] JBoss MSC version 1.0.2.GA-redhat-1
    17:37:31,338 INFO  [org.jboss.as] JBAS015899: JBoss EAP 6.0.0.GA (AS 7.1.2.Final-redhat-1) starting
    17:37:33,865 INFO  [org.xnio] XNIO Version 3.0.4.GA-redhat-1
    17:37:33,884 INFO  [org.jboss.as.server] JBAS015888: Creating http management service using socket-binding (management-h
    ttp)
    17:37:33,888 INFO  [org.xnio.nio] XNIO NIO Implementation Version 3.0.4.GA-redhat-1
    17:37:33,941 INFO  [org.jboss.remoting] JBoss Remoting version 3.2.8.GA-redhat-1
    17:37:33,984 INFO  [org.jboss.as.clustering.infinispan] JBAS010280: Activating Infinispan subsystem.
    17:37:33,984 INFO  [org.jboss.as.configadmin] JBAS016200: Activating ConfigAdmin Subsystem
    17:37:34,009 INFO  [org.jboss.as.naming] JBAS011800: Activating Naming Subsystem
    17:37:34,023 INFO  [org.jboss.as.logging] JBAS011502: Removing bootstrap log handlers
    17:37:34,032 INFO  [org.jboss.as.security] JBAS013101: Activating Security Subsystem
    17:37:34,032 INFO  [org.jboss.as.webservices] JBAS015537: Activating WebServices Extension
    17:37:34,054 INFO  [org.jboss.as.security] (MSC service thread 1-1) JBAS013100: Current PicketBox version=4.0.9.Final-re
    dhat-1
    17:37:34,066 INFO  [org.jboss.as.connector.logging] (MSC service thread 1-8) JBAS010408: Starting JCA Subsystem (JBoss I
    ronJacamar 1.0.11.Final-redhat-1)
    17:37:34,634 INFO  [org.jboss.as.naming] (MSC service thread 1-1) JBAS011802: Starting Naming Service
    17:37:34,637 INFO  [org.jboss.as.mail.extension] (MSC service thread 1-1) JBAS015400: Bound mail session [java:jboss/mai
    l/Default]
    17:37:34,747 INFO  [org.jboss.as.connector.subsystems.datasources] (ServerService Thread Pool -- 27) JBAS010403: Deployi
    ng JDBC-compliant driver class oracle.jdbc.OracleDriver (version 11.2)
    17:37:34,768 INFO  [org.jboss.as.connector.subsystems.datasources] (ServerService Thread Pool -- 27) JBAS010403: Deployi
    ng JDBC-compliant driver class org.h2.Driver (version 1.3)
    17:37:34,951 INFO  [org.jboss.as.connector.subsystems.datasources] (ServerService Thread Pool -- 27) JBAS010403: Deployi
    ng JDBC-compliant driver class oracle.jdbc.driver.OracleDriver (version 11.2)
    17:37:35,040 INFO  [org.jboss.ws.common.management.AbstractServerConfig] (MSC service thread 1-4) JBoss Web Services - S
    tack CXF Server 4.0.4.GA-redhat-1
    17:37:35,095 INFO  [org.apache.coyote.http11.Http11Protocol] (MSC service thread 1-6) Starting Coyote HTTP/1.1 on http-/
    127.0.0.1:8080
    17:37:35,103 INFO  [org.jboss.as.connector.subsystems.datasources] (MSC service thread 1-6) JBAS010400: Bound data sourc
    e [java:jboss/datasources/ExampleDS]
    17:37:35,219 INFO  [org.jboss.as.server.deployment.scanner] (MSC service thread 1-5) JBAS015012: Started FileSystemDeplo
    ymentService for directory C:\jboss-eap-6.0\standalone\deployments
    17:37:35,222 INFO  [org.jboss.as.server.deployment] (MSC service thread 1-2) JBAS015876: Starting deployment of "myApp.war

17:37:35,227 INFO  [org.jboss.as.remoting] (MSC service thread 1-4) JBAS017100: Listening on 127.0.0.1:4447
    17:37:35,230 INFO  [org.jboss.as.remoting] (MSC service thread 1-8) JBAS017100: Listening on 127.0.0.1:9999
    17:37:38,082 ERROR [stderr] (MSC service thread 1-2) log4j:ERROR No appender named [logfile] could be found.
    17:37:38,096 WARN  [org.jboss.as.ee] (MSC service thread 1-2) JBAS011006: Not installing optional component org.springfr
    amework.web.context.request.async.StandardServletAsyncWebRequest due to exception: org.jboss.as.server.deployment.Deploy
    mentUnitProcessingException: JBAS011054: Could not find default constructor for class org.springframework.web.context.re
    quest.async.StandardServletAsyncWebRequest
            at org.jboss.as.ee.component.ComponentDescription$DefaultComponentConfigurator.configure(ComponentDescription.ja
    va:606) [jboss-as-ee-7.1.2.Final-redhat-1.jar:7.1.2.Final-redhat-1]
            at org.jboss.as.ee.component.deployers.EEModuleConfigurationProcessor.deploy(EEModuleConfigurationProcessor.java
    :83) [jboss-as-ee-7.1.2.Final-redhat-1.jar:7.1.2.Final-redhat-1]
            at org.jboss.as.server.deployment.DeploymentUnitPhaseService.start(DeploymentUnitPhaseService.java:116)
            at org.jboss.msc.service.ServiceControllerImpl$StartTask.startService(ServiceControllerImpl.java:1811)
            at org.jboss.msc.service.ServiceControllerImpl$StartTask.run(ServiceControllerImpl.java:1746)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110) [rt.jar:1.7.0_07]
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603) [rt.jar:1.7.0_07]
            at java.lang.Thread.run(Thread.java:722) [rt.jar:1.7.0_07]

    17:37:38,234 INFO  [org.jboss.as.osgi] (MSC service thread 1-1) JBAS011907: Register module: Module "deployment.myApp.war:
    main" from Service Module Loader
    **17:37:38,591 ERROR [org.apache.catalina.core.StandardContext] (MSC service thread 1-8) Error listenerStart
    17:37:38,591 ERROR [org.apache.catalina.core.StandardContext] (MSC service thread 1-8) Context [/myApp] startup failed due
     to previous errors
    17:37:38,603 ERROR [org.jboss.msc.service.fail] (MSC service thread 1-8) MSC000001: Failed to start service jboss.web.de
    ployment.default-host./myApp: org.jboss.msc.service.StartException in service jboss.web.deployment.default-host./myApp: JBAS
    018040: Failed to start context**
            at org.jboss.as.web.deployment.WebDeploymentService.start(WebDeploymentService.java:95)
            at org.jboss.msc.service.ServiceControllerImpl$StartTask.startService(ServiceControllerImpl.java:1811) [jboss-ms
    c-1.0.2.GA-redhat-1.jar:1.0.2.GA-redhat-1]
            at org.jboss.msc.service.ServiceControllerImpl$StartTask.run(ServiceControllerImpl.java:1746) [jboss-msc-1.0.2.G
    A-redhat-1.jar:1.0.2.GA-redhat-1]
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110) [rt.jar:1.7.0_07]
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603) [rt.jar:1.7.0_07]
            at java.lang.Thread.run(Thread.java:722) [rt.jar:1.7.0_07]

    17:37:38,814 INFO  [org.jboss.as.server] (ServerService Thread Pool -- 28) JBAS018559: Deployed "myApp.war"
    17:37:38,817 INFO  [org.jboss.as.controller] (Controller Boot Thread) JBAS014774: Service status report
    JBAS014777:   Services which failed to start:      service jboss.web.deployment.default-host./myApp: org.jboss.msc.service
    .startException in service jboss.web.deployment.default-host./myApp: JBAS018040: Failed to start context

的web.xml

<web-app xmlns="http://java.sun.com/xml/ns/javaee"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
          http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
          version="2.5">    

    <display-name>MyApp</display-name>

    <welcome-file-list>
        <!-- welcome-file>index.jsp</welcome-file>
        <welcome-file>index.html</welcome-file> -->
        <welcome-file>/WEB-INF/jsps/menu.jsp</welcome-file> 
    </welcome-file-list>

    <filter>
        <description>SSOAuthFilter</description>
        <filter-name>SSOAuthFilter</filter-name>
        <filter-class>auth.system.SSOAuthFilter</filter-class>
        <!-- this parameter is added from 2.1.1-SNAPSHOT and is optional -->
        <!-- init-param>
            <param-name>exclude</param-name>
            <param-value>.*index.*</param-value>
        </init-param-->
    </filter>


    <filter-mapping>
        <filter-name>SSOAuthFilter</filter-name>
        <url-pattern>/myApp</url-pattern>
        <dispatcher>REQUEST</dispatcher>
    </filter-mapping>

    <servlet>
        <servlet-name>mvc-dispatcher</servlet-name>
        <servlet-class>
            org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>mvc-dispatcher</servlet-name>
        <url-pattern>/myApp/*</url-pattern>
    </servlet-mapping>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/mvc-dispatcher-servlet.xml</param-value>
    </context-param>

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


    <servlet>
        <servlet-name>SSOLogoutServlet</servlet-name>
        <servlet-class>auth.system.SSOLogoutServlet</servlet-class>    
    </servlet>
    <servlet-mapping>
        <servlet-name>SSOLogoutServlet</servlet-name>
        <url-pattern>/logout</url-pattern>
    </servlet-mapping>

</web-app>

非常感谢任何帮助。

提前谢谢!

2 个答案:

答案 0 :(得分:1)

如果您使用@Autowired注释,请确保bean由spring容器初始化。您可以通过<bean id="test" class="com.demo.Test"></bean>创建bean,或者只是将包名提供给context:component-scan标签,以表示Spring必须扫描包搜索构造型注释并在应用程序上下文中注册bean。

答案 1 :(得分:0)

在帖子上:Error deploying WAR file to Jboss EAP 6.0, which works under JBOSS AS 7.1

我找到了使用以下命令启动的建议:

standalone.sh -Dorg.jboss.as.logging.per-deployment=false

这样做,我可以看到真正的原因:

Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not
 autowire field: private packages.StudentSearchDao packages.studentSearchDao; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException
: No qualifying bean of type [packages.StudentSearchDao] found for dependency: expected at
 least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

所以,我添加了&#34; @ Qualifier&#34;在@Autowired之后,如下所示:

@Autowired @Qualifier("studentSearchDaoImpl") 
private StudentSearchDao studentSearchDao;