【问题标题】:Is it possible to reference a bean created with @Service from applicationContext?是否可以从 applicationContext 引用使用 @Service 创建的 bean?
【发布时间】:2014-07-02 08:43:24
【问题描述】:

我有以下情况:

appContext.xml:包含 DAO 映射器 bean(UserMapper、RoleMapper...)

appContext-security.xml:包含需要引用我的一项服务 (UserDetailsS​​ervice) 的 http 标记

app-servlet.xml:包含用于查找注释的标签

<context:component-scan base-package="com.example.myapp"/> 
<mvc:annotation-driven/>

我的服务有 @Service("serviceName") 注释。这意味着 bean 是由 app-servlet.xml 创建的。

OpenID 登录需要 UserDetailsS​​ervice 类才能工作,并且 UserDetailsS​​ervice 有一个自动装配字段 (UserService) 这是我的 appContext-security.xml:

<security:http auto-config="true">
    <security:intercept-url pattern="/welcome*" access="ROLE_USER, ROLE_ADMIN" />
    <security:intercept-url pattern="/user/*" access="ROLE_USER, ROLE_ADMIN" />
    <security:intercept-url pattern="/rest/*" access="ROLE_USER, ROLE_ADMIN" />
    <security:intercept-url pattern="/admin/*" access="ROLE_ADMIN" />
    <security:logout logout-success-url="/" />
    <security:openid-login default-target-url="/welcome" authentication-failure-url="/loginfailed" user-service-ref="userDetailsService"/>
    <security:form-login login-page="/login" default-target-url="/welcome" authentication-failure-url="/loginfailed" />  
    <security:access-denied-handler ref="openIdAuthFailureHandler"/>
</security:http> 

我想我不能引用 UserDetailsS​​ervice,因为它是由 app-servlet.xml 创建的,它是根配置文件(appContext.xml 和 appContext-security.xml)的子项

如果我尝试在 appContext-security.xml 中声明 UserDetailsS​​ervice,其自动装配字段 UserService 在调试中为空:

<bean id="userDetailsService" class="com.example.myapp.service.impl.UserDetailsServiceImpl"/>

是否可以拆分扫描?也许,在 appContext.xml 中扫描服务并仅在 app-servlet 中扫描控制器可能是个好主意,但我不知道这是否有意义。

我想让 UserDetailsS​​ervice 由 @Service 注释并获取它对 appContext-security.xml 的引用。这是我的主要问题。我可以从 appContext-security.xml 引用使用 @Service 创建的 UserDetailservice 吗?如果不是……我需要进行哪些更改?

正确答案将被投票。

编辑:

我做了以下事情:

appContext.xml:

<context:component-scan base-package="com.example.myapp">
    <context:exclude-filter type="regex" expression="com\.example\.myapp\.controller..*"/> 
</context:component-scan>

app-servlet.xml:

<context:component-scan base-package="com.example.myapp.controller" />

<!-- Enabling Spring MVC configuration through annotations -->
<mvc:annotation-driven />

现在可以了

【问题讨论】:

  • 为什么不只扫描控制器app-servlet.xml 中的包和appContext.xml 中的其余包?这可能会解决你的问题
  • 现在我做到了。谢谢@geonand
  • 我会将其添加为答案,以供将来的读者轻松找到。如果不麻烦请采纳,以帮助以后的读者

标签: spring spring-mvc spring-security


【解决方案1】:

您需要做的就是更改组件扫描。

appContext.xml(根应用程序上下文的配置)中

    <context:component-scan base-package="com.example.myapp">
        <context:exclude-filter type="regex" expression="com\.example\.myapp\.controller..*"/> 
        <!--or use this instead 
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/> 
        -->
    </context:component-scan>

app-servlet.xml(Web 应用程序上下文的配置)中

<context:component-scan base-package="com.example.myapp.controller" />

这样,除了控制器之外的所有 bean 都将出现在根应用程序上下文中,因此可以在您的安全代码中访问。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-07-24
    • 2020-06-05
    • 2014-06-17
    • 2012-12-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多