【问题标题】:@Service instantiated twice@Service 实例化了两次
【发布时间】:2012-05-29 14:45:59
【问题描述】:

尝试用 Spring MVC 和 Spring Security 做一些实验:

@Controller
@RequestMapping("/auth")
public class AuthController {   
    @Autowired
    // @Qualifier("userDetailsService") - tried adding this
    private MyUserDetailsService userDetailsService;
    ...
}

// @Scope("singleton") - tried adding this
@Service("userDetailsService")
public class MyUserDetailsService implements UserDetailsService {
    ...
}

我拥有的完整 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" 
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    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.0.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
        http://www.springframework.org/schema/security
        http://www.springframework.org/schema/security/spring-security-3.0.xsd">

    <!-- ORIGINAL springmvc-servlet.xml -->
    <mvc:annotation-driven />
    <mvc:resources mapping="/static/**" location="/static/" />

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
        <property name="prefix" value="/WEB-INF/jsp/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>

    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
    <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />

    <context:annotation-config />
    <context:component-scan base-package="com.xxxxxxxxx" />
    <!-- end ORIGINAL springmvc-servlet.xml -->

    <!-- FROM springmvc-security.xml -->
    <security:global-method-security secured-annotations="enabled">
    </security:global-method-security>

    <security:http auto-config="true" access-denied-page="/auth/denied">
        <security:intercept-url pattern="/admin/*" access="ROLE_ADMIN"/>        
        <security:intercept-url pattern="/user/*" access="ROLE_USER"/>
        <security:intercept-url pattern="/auth/login" access="IS_AUTHENTICATED_ANONYMOUSLY"/>
        <security:intercept-url pattern="/auth/register" access="IS_AUTHENTICATED_ANONYMOUSLY"/>
        <security:intercept-url pattern="/**" access="IS_AUTHENTICATED_ANONYMOUSLY"/>
        <security:form-login login-page="/auth/login" authentication-failure-url="/auth/login?login_error=true" default-target-url="/user"/>
        <security:logout logout-url="/auth/logout" logout-success-url="/" invalidate-session="true"/>
        <security:openid-login authentication-failure-url="/login?login_error=t" user-service-ref="openIdUserDetailsService" />
    </security:http>

    <security:authentication-manager>
        <security:authentication-provider user-service-ref="userDetailsService" />
    </security:authentication-manager>
    <!-- end FROM springmvc-security.xml -->    
</beans>

由于某种原因,创建了 2 个 MyUserDetailsService 实例。第一个由 Spring Security 使用,第二个注入到AuthController。如果我想要MyUserDetailsService 的单个实例,正确的方法是什么?

【问题讨论】:

  • 有趣,我一直想知道如何让 Spring Security 与其他 bean 在单个应用程序上下文中运行 - 我想它们在不同的 bean 中运行。
  • 顺便说一句,在我当前的配置中,UserDetailsS​​ervice 仅在 SecurityContext 中声明,不在 ApplicationContext 中(自动扫描已禁用)。

标签: spring spring-mvc spring-security


【解决方案1】:

您没有显示足够的配置来确定,但我敢打赌,您对如何在 Spring MVC 应用程序中管理 Spring ApplicationContexts 感到困惑。我对关于同一问题的另一个问题的回答几乎肯定是您需要阅读的内容:

Declaring Spring Bean in Parent Context vs Child Context

您很可能已经在应用程序的根上下文和子上下文中声明了您的服务 bean(显式或使用组件扫描)。作为一个服务 bean,它应该只存在于根上下文中。您也可以从阅读此答案中受益:

Spring XML file configuration hierarchy help/explanation

【讨论】:

  • 我已经用我完整的上下文配置更新了这个问题。你能检查一下吗?
  • 这是属于您的 DispatcherServlet 的上下文文件。您的 web.xml 中应该有一个名为“springmvc”的文件。还要显示你的根上下文是如何初始化的。这也可能在您的 web.xml 中由 ContextLoaderListener 和 contextConfigLocation 参数完成。
  • 这花了我大约一个小时,但现在我明白了。谢谢你的回答:-)
【解决方案2】:

这个配置对我有用:

<security:authentication-manager>
<security:authentication-provider user-service-ref="userService">

<bean id="userService" class="com.mydomain.service.UserDetailsServiceImpl" />

A tutorial here.

【讨论】:

  • 这不会阻止他们中的两个被创建。它只是要更改一个或两个已创建 bean 的名称。
  • @NimChimpsky:重命名为theUserDetailsService,还是一样。
  • @lok​​i2302 哦,您也在应用程序 cotnext 中更改了它吗?你在其他地方实施 userdetailsservice 吗?
  • @NimChimpsky:全部 3 - @Qualifier@Service 和上下文。现在所有 3 个都是 theUserDetailsS​​ervice,我仍然有 2 个实例。还有其他想法吗?
猜你喜欢
  • 2020-03-03
  • 1970-01-01
  • 2023-04-05
  • 1970-01-01
  • 2023-03-21
  • 2013-05-29
  • 1970-01-01
  • 2011-05-19
  • 2013-06-08
相关资源
最近更新 更多