将上下文初始化事件发送给侦听器

时间:2015-04-24 06:33:17

标签: spring security

我正在使用spring mvc来创建restful webservices。当我将Spring安全性集成到代码中时,我得到了以下错误

StackTrace:

SEVERE: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'appointmentController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.nutansTplus.service.AppointmentService com.nutansTplus.controller.AppointmentController.appointmentService; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [com.nutansTplus.service.AppointmentService] 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), @org.springframework.beans.factory.annotation.Qualifier(value=appointmentService)}
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:285)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1074)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:291)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:288)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:190)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:580)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:895)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:425)
    at org.springframework.web.context.ContextLoader.createWebApplicationContext(ContextLoader.java:276)
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:197)
    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:47)
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4939)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5434)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549)
    at java.util.concurrent.FutureTask.run(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.nutansTplus.service.AppointmentService com.nutansTplus.controller.AppointmentController.appointmentService; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [com.nutansTplus.service.AppointmentService] 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), @org.springframework.beans.factory.annotation.Qualifier(value=appointmentService)}
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:502)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:84)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:282)
    ... 22 more
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [com.nutansTplus.service.AppointmentService] 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), @org.springframework.beans.factory.annotation.Qualifier(value=appointmentService)}
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:920)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:789)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:703)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:474)

我的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" id="WebApp_ID" version="3.0">
  <display-name>NutansTplus-BackEnd</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
  </welcome-file-list>
  <servlet>
    <servlet-name>spring</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>spring</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
  <filter>
  <filter-name>CorsFilter</filter-name>
  <filter-class>org.apache.catalina.filters.CorsFilter</filter-class>
</filter>
<filter-mapping>
  <filter-name>CorsFilter</filter-name>
  <url-pattern>/*</url-pattern>
</filter-mapping>
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/application-context.xml</param-value>
    </context-param>
    <filter>
        <filter-name>springSecurityFilterChain</filter-name>
        <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>springSecurityFilterChain</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

<error-page>
    <error-code>404</error-code>
    <location>/service/version_1.0/object/account/resourcenotfound</location>
</error-page>
<error-page>
    <error-code>403</error-code>
    <location>/service/version_1.0/object/account/unauthorised</location>
</error-page>
<error-page>
    <error-code>401</error-code>
    <location>/service/version_1.0/object/account/unauthorised</location>
</error-page>
</web-app>

我的 Security-beans.xml 如下所示   WEB-INF /安全-beans.xml文件:

<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:security="http://www.springframework.org/schema/security"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.2.xsd">

    <context:annotation-config />
    <context:component-scan base-package="com.nutansTplus.controller" />
    <!-- Security configuration for REST services. -->
    <security:http 
        authentication-manager-ref="authenticationManager" entry-point-ref="restAuthenticationEntryPoint" auto-config="true">
        <security:custom-filter ref="restServicesFilter"
            before="PRE_AUTH_FILTER" />
              <security:intercept-url pattern="/services/data/v1.0/Restaurant/GetRestaurantsDetails" access="ROLE_ADMIN"/>
          <security:intercept-url pattern="/services/data/v1.0/Restaurant/PostFeedbackDetail" access="ROLE_USER"/>
    </security:http>

    <!-- Entry point for REST service. -->
<!--    <bean id="restAuthenticationEntryPoint" class="com.nutansR.controller.RestAuthenticationEntryPoint" />
 -->    <!-- Filter for REST services. -->
    <bean id="restServicesFilter"
        class="com.nutansTplus.controller.RestUsernamePasswordAuthenticationFilter">
        <property name="postOnly" value="false" />
        <property name="authenticationManager" ref="authenticationManager" />
        <property name="authenticationSuccessHandler" ref="restServicesSuccessHandler" />
    </bean>
    <!-- A handler for successful REST service authentication. -->
    <bean id="restServicesSuccessHandler" class="com.nutansTplus.controller.RestAuthenticationSuccessHandler" />
    <!-- Security configuration for other resources. Use login form. -->
    <!-- <security:http use-expressions="true" create-session="always">
        <security:intercept-url pattern="/**"
            access="isAuthenticated()" />
    </security:http> -->
    <security:authentication-manager alias="authenticationManager">
        <security:authentication-provider>
            <security:user-service>
                <security:user name="admin_user" password="password"
                    authorities="ROLE_ADMIN" />
                <security:user name="karthi" password="karthi"
                    authorities="ROLE_USER" />
            </security:user-service>
        </security:authentication-provider>
    </security:authentication-manager>
    <security:global-method-security
        secured-annotations="enabled"
    proxy-target-class="true">
    </security:global-method-security>
</beans>

我的application-context.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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <import resource="spring/security-beans.xml" />

</beans>

我的spring-servlet.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:cache="http://www.springframework.org/schema/cache"
 xmlns:context="http://www.springframework.org/schema/context"
 xmlns:jdbc="http://www.springframework.org/schema/jdbc"
 xmlns:jee="http://www.springframework.org/schema/jee"
 xmlns:lang="http://www.springframework.org/schema/lang"
 xmlns:mvc="http://www.springframework.org/schema/mvc"
 xmlns:p="http://www.springframework.org/schema/p"
 xmlns:task="http://www.springframework.org/schema/task"
 xmlns:tx="http://www.springframework.org/schema/tx"
 xmlns:util="http://www.springframework.org/schema/util"
 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-3.2.xsd
  http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
  http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
  http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
  http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-3.2.xsd
  http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
  http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.2.xsd
  http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
  http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">
 <mvc:annotation-driven />

    <context:component-scan base-package="com.nutansTplus"/>

       <context:annotation-config />
        <context:component-scan base-package="com.nutansTplus.pushService" />
        <task:annotation-driven />

    <bean id="viewResolver"
  class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  <property name="prefix" value="/WEB-INF/" />
  <property name="suffix" value=".jsp"/>
 </bean>
 <bean
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="location">
        <value>classpath:Application.properties</value>
    </property>
</bean>
 <bean id="nutansRegisterDao" class="com.nutansTplus.dao.NutansRegisterDao"/>
 <bean id="appointmentDAO" class="com.nutansTplus.dao.AppointmentDAO"/>
 <bean id="commonDAO" class="com.nutansTplus.dao.CommonDAO"/>

<bean id="dataSource1" destroy-method="close" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driverClassName}"/>
<property name="jdbcUrl" value="${jdbc.url}"/>
<property name="user" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
<property name="minPoolSize" value="10"/>
<property name="maxPoolSize" value="150"/>
<property name="breakAfterAcquireFailure" value="false"/>
<property name="acquireRetryAttempts" value="3"/>
<property name="maxStatements" value="100"/>
<property name="maxIdleTime" value="3600"/>
<property name="acquireIncrement" value="10"/>
<property name="preferredTestQuery" value="select 1 from DUAL" />
<property name="idleConnectionTestPeriod" value="180" />
<property name="testConnectionOnCheckin" value="true" />
</bean>
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> 
    <property name="dataSource"><ref bean="dataSource1"/></property> 
</bean> 
<bean id="ndPjdbcTemplate"
        class="org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate">
        <constructor-arg ref="dataSource1"></constructor-arg>
    </bean>
<bean id="MyServlet" class="com.nutansTplus.dao.NutansRegisterDao"> 
    <property name="jdbcTemplate"><ref bean="jdbcTemplate"/></property> 
</bean> 
<bean id="commonDao" class="com.nutansTplus.dao.CommonDAO"> 
    <property name="jdbcTemplate"><ref bean="jdbcTemplate"/></property> 
</bean> 
<bean id="dynamicFormDao" class="com.nutansTplus.dao.DynamicFormDAO">
        <property name="jdbcTemplate" ref="jdbcTemplate" />
        <property name="namedParamJdbcTemplate" ref="ndPjdbcTemplate" />
    </bean> 
  </beans>

1 个答案:

答案 0 :(得分:0)

错误说Spring无法创建bean appointmentController,因为类AppointmentController包含类似的内容:

Autowired(required=true)
Qualifier(value=appointmentService)
AppointmentService appointmentService;

我认为AppointmentService是一个类,而不是一个接口。 Spring大量使用AOP代理,默认情况下使用JDK代理=&gt;意味着您必须通过接口进行自动装配,因为JDK代理确实实现了接口,但没有扩展该类。

我的建议是尽可能使用接口,因为它提供了高度可维护的代码,因为您可以轻松地更改实现。

例如,您的服务可能变为:

class AppointmentServiceImpl implements AppointmentService {
    ...
}

如果你不能,你可以尝试使用proxy-target-class=true,但一定要在Spring将使用AOP代理的所有地方一致地使用它(我的建议是坚持使用Spring默认的JDK代理......)