这是问题所在。
我有 spring 3.0.5,使用它的新 DATA JPA 存储库模块(接口扩展
CrudRepository<T, ID extends Serializable>
)。
我有 Apache Shiro 1.1.0 作为我的应用程序的安全解决方案。Apache shiro在spring bean定义xml文件中配置如下:
<bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor"/>
<!-- Enable Shiro Annotations for Spring-configured beans. Only run after -->
<!-- the lifecycleBeanProcessor has run: -->
<bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator" depends-on="lifecycleBeanPostProcessor"/>
<bean class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor">
<property name="securityManager" ref="securityManager"/>
</bean>
<!-- Define the realm you want to use to connect to your back-end security datasource: -->
<bean id="securityDAORealm" class="com.bilto.archiweb.security.SecurityDAORealm" />
<bean id="securityManager" class="org.apache.shiro.mgt.DefaultSecurityManager">
<!-- Single realm app. If you have multiple realms, use the 'realms' property instead. -->
<property name="realm" ref="securityDAORealm"/>
</bean>
<!-- For simplest integration, so that all SecurityUtils.* methods work in all cases, -->
<!-- make the securityManager bean a static singleton. DO NOT do this in web -->
<!-- applications - see the 'Web Applications' section below instead. -->
<bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
<property name="staticMethod" value="org.apache.shiro.SecurityUtils.setSecurityManager"/>
<property name="arguments" ref="securityManager"/>
</bean>
请注意,我的应用程序是一个独立的应用程序,这个 Apachoe Shiro 配置反映了它。
spring jpa 存储库的配置以及标准 spring 配置(注释扫描)在其他文件中配置,它们似乎与这个问题无关,所以我将跳过打印它们。
我的SecurityDAORealm
类CredentialsRepository
作为其 jpa 存储库控制器接口 ( CredentialsRepository extends CrudRepository<T, ID extends Serializable>
) 自动装配,用于访问存储凭据的数据库。
@Component
public class SecurityDAORealm extends AuthorizingRealm {
@Autowired
CredentialRepository credentialRepository;
...
}
现在解决问题。
配置 Apache Shiro 注释扫描时,CredentialsRepository
找不到类型的自动装配 bean,因此未装配。关闭注释扫描后,CredentialsRepository 变量会自动关联并且一切正常。
启用 Apache Shiro 注释处理的部分是这个
<bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor"/>
<!-- Enable Shiro Annotations for Spring-configured beans. Only run after -->
<!-- the lifecycleBeanProcessor has run: -->
<bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator" depends-on="lifecycleBeanPostProcessor"/>
<bean class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor">
<property name="securityManager" ref="securityManager"/>
</bean>
通过注释掉它的中心和平
<!-- <bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator" depends-on="lifecycleBeanPostProcessor"/> -->
注释已关闭,取消注释将再次打开它们。
作为测试,我尝试自动连接一个简单的 pojo 而不是 my CredentialsRepository
,这在两种情况下都很好用(注释开/关)。
我对弹簧内部的了解不多。这里可能发生的情况是CredentialsRepository
变量没有自动连接,因为 Spring 没有机会SimpleJpaRepository
在其后端创建其适当的实现 ()。
解决方法只是通过自动连接一些“全类”JPA 控制器而不是 spring 管理接口实现来实现。
但是我很好奇这是否是一个需要修复的错误,或者这里是否存在一些额外的弹簧魔法可以使其也适用于弹簧数据接口。