找不到Autowired bean的候选人

时间:2017-01-04 14:48:02

标签: java-8 spring-4

在客户处,我正在处理需要审核的应用程序。这些审核记录应该可以在网页中查看,我可以修改当前存在的数据来做到这一点。 Maven编译代码时没有错误,但是当调用网页显示审计记录时,我收到以下异常

04-Jan-2017 15:22:25.133 WARNING [http-nio-7443-exec-7] org.glassfish.jersey.server.spring.AutowiredInjectResolver.getBeanFromSpringContext No qualifying bean of type 'my.company.app.audit.service.IAuditService' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
04-Jan-2017 15:22:25.167 WARNING [http-nio-7443-exec-7] org.glassfish.jersey.internal.Errors.logErrors The following warnings have been detected: WARNING: Unknown HK2 failure detected:
MultiException stack 1 of 3
org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'my.company.app.audit.service.IAuditService' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

MultiException stack 2 of 3
java.lang.IllegalArgumentException: While attempting to resolve the dependencies of my.company.app.audit.rest.resources.AuditResource errors were found

MultiException stack 3 of 3
java.lang.IllegalStateException: Unable to perform operation: resolve on my.company.app.audit.rest.resources.AuditResource

应用程序类

package my.company.app.audit;

public class AuditApplication extends ResourceConfig {
    public AuditApplication() {
        register(AuditResource.class);
    }
}

资源类

package my.company.app.audit.rest.resources;

@Path(AUDIT_RESOURCE_LINK)
@Produces({ MediaType.APPLICATION_JSON })
public class AuditResource extends AbstractResource {
    @Autowired
    private IAuditService auditService;  // Commenting out this line results in the exception no longer popping up
}

服务类

package my.company.app.audit.service;

import org.springframework.stereotype.Service;

@Service
public class AuditService implements IAuditService {
}

审核应用程序特定的配置:audit-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:context="http://www.springframework.org/schema/context"
   xmlns:jpa="http://www.springframework.org/schema/data/jpa"
   xmlns:aop="http://www.springframework.org/schema/aop"
   xsi:schemaLocation="http://www.springframework.org/schema/beans
   http://www.springframework.org/schema/beans/spring-beans.xsd
   http://www.springframework.org/schema/context
   http://www.springframework.org/schema/context/spring-context.xsd
   http://www.springframework.org/schema/data/jpa
   http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
   http://www.springframework.org/schema/aop
   http://www.springframework.org/schema/aop/spring-aop.xsd">

    <context:annotation-config/>

    <context:component-scan base-package="my.company.app.audit"/>

    <jpa:repositories base-package="my.company.app.audit" transaction-manager-ref="txManager"/>

    <aop:aspectj-autoproxy/>

</beans>

的web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0" metadata-complete="true">

    <display-name>foo web_audit</display-name>

    <!--  SESSION -->
    <session-config>
        <session-timeout>30</session-timeout>
        <cookie-config>
            <name>WEB_AUDIT</name>
            <path>/</path>
            <http-only>true</http-only>
            <secure>true</secure>
        </cookie-config>
    </session-config>

    <!-- SPRING -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            classpath:applicationContext.xml,
            classpath:foo-json-serialization-beans.xml,
            classpath:foo-dao-jpa-beans.xml,
            classpath:foo-dao-jpa-vendor-adapter-beans.xml,
            classpath:spring/foo-jpa-services-common.xml,
            classpath:audit-config.xml,
            classpath:iam-config.xml
        </param-value>
    </context-param>
    <listener>
        <listener-class>
            org.springframework.web.context.ContextLoaderListener
        </listener-class>
    </listener>

    <!-- FILTER -->
    <filter>
        <filter-name>securityHeadersFilter</filter-name>
        <filter-class>my.company.fooshared.web.filter.SecurityHeadersFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>securityHeadersFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <filter>
        <filter-name>SSOFilter</filter-name>
        <filter-class>my.company.fooshared.webaccess.filter.WebAccessFilter</filter-class>
        <init-param>
            <param-name>acsUrl</param-name>
            <param-value>/acs</param-value>
        </init-param>
        <init-param>
            <param-name>logoutUrl</param-name>
            <param-value>/logout</param-value>
        </init-param>
        <init-param>
            <param-name>authRequestUrl</param-name>
            <param-value>/authRequest</param-value>
        </init-param>       
        <init-param>
            <param-name>spProviderId</param-name>
            <param-value>screeningutility-ci.browse.companynet.sipn.company.com</param-value>
        </init-param>
        <init-param>
            <param-name>idProviderSSOUrl</param-name>
            <param-value>https://idp.companynet.sipn.company.com/idp/profile/SAML2/POST/SSO</param-value>
        </init-param>
        <init-param>
            <param-name>cacertPath</param-name>
            <param-value>/var/lib/tomcat/cacert/</param-value>
        </init-param>
        <init-param>
            <param-name>keystoreFile</param-name>
            <param-value>/var/lib/tomcat/jks/suchannel.jks</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>SSOFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <!-- JERSEY -->
    <servlet>
        <servlet-name>Jersey2Dispatcher</servlet-name>
        <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
        <init-param>
            <param-name>javax.ws.rs.Application</param-name>
            <param-value>my.company.app.audit.AuditApplication</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>Jersey2Dispatcher</servlet-name>
        <url-pattern>/rest/*</url-pattern>
    </servlet-mapping>

    <error-page>
        <location>/error.html</location>
    </error-page>

</web-app>

我以一个类似的模块为例来构建我的代码。其他模块工作正常,所以我必须遗漏一些东西...... 欢迎提出任何建议。

当添加默认构造函数并在其中设置断点时,我注意到从不调用AuditService类的构造函数。

1 个答案:

答案 0 :(得分:0)

自动装配类本身而不是实现的接口解决了这个问题。我没有解释为什么界面没有用。

修改

我可能已经找到了问题的原因。

在修改代码时,我在maven中引入了一些新模块。这些新模块是最初包含代码的模块的子模块。

select {
    pidInList = ###pageUid###
    #where  - already set by styles.content.get
    #orderBy - already set by styles.content.get
    markers {
      pageUid.field = uid
    }
  }

但依赖旧审计模块的其他模块未更新,因此引用了新模块的父模块。这可能是Spring

未正确加载AuditService类的原因