【发布时间】:2014-07-02 08:43:24
【问题描述】:
我有以下情况:
appContext.xml:包含 DAO 映射器 bean(UserMapper、RoleMapper...)
appContext-security.xml:包含需要引用我的一项服务 (UserDetailsService) 的 http 标记
app-servlet.xml:包含用于查找注释的标签
<context:component-scan base-package="com.example.myapp"/>
<mvc:annotation-driven/>
我的服务有 @Service("serviceName") 注释。这意味着 bean 是由 app-servlet.xml 创建的。
OpenID 登录需要 UserDetailsService 类才能工作,并且 UserDetailsService 有一个自动装配字段 (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>
我想我不能引用 UserDetailsService,因为它是由 app-servlet.xml 创建的,它是根配置文件(appContext.xml 和 appContext-security.xml)的子项
如果我尝试在 appContext-security.xml 中声明 UserDetailsService,其自动装配字段 UserService 在调试中为空:
<bean id="userDetailsService" class="com.example.myapp.service.impl.UserDetailsServiceImpl"/>
是否可以拆分扫描?也许,在 appContext.xml 中扫描服务并仅在 app-servlet 中扫描控制器可能是个好主意,但我不知道这是否有意义。
我想让 UserDetailsService 由 @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