我得到“没有为当前线程找到会话”。我想问题在于混合声明性 xml bean 和带注释的 bean。接下来,我将恢复我的配置。
我的图书馆项目
Spring 3.1.4 休眠 4
应用程序上下文.xml
<tx:annotation-driven transaction-manager="transactionManager" />
<context:component-scan
base-package="com.mycompany.dao.core, com.mycompany.services.core,
com.mycompany.permissionEvaluator" />
<import resource="model-core-security.xml" />
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
... (sessionFactory ecc...)
模型核心安全.xml
<bean id="expressionHandler"
class="org.springframework.security.access.expression.method.DefaultMethodSecurityExpressionHandler">
<property name="permissionEvaluator" ref="permissionEvaluator" />
</bean>
<security:global-method-security
pre-post-annotations="enabled">
<security:expression-handler ref="expressionHandler" />
</security:global-method-security>
通过组件扫描,我创建了 bean:AccountService、AccountDAO 和 PermissionEvaluator。
AccountService.java (com.mycompany.services)
@Service("accountService")
@Transactional
public class AccountServiceImpl implements AccountService {
@Resource
private AccountDAO accountDAO;
...
}
AccountDAO.java (com.mycompany.dao)
@Repository
@Transactional
public class HibernateAccountDAOImpl implements AccountDAO {
...query...
}
(AccountService e AccountDAO 是事务性的)
现在,在AccountController.java中,我调用accountService.listAccounts()就可以了!
但是,如果我将 AccountService 注入 PermissionEvaluator 类(以下代码段),AccountController 会在调用accountService.listAccounts()时为当前线程找到 No Session
PermissionEvaluator.java (com.mycompany.permissionEvaluator)
Component("permissionEvaluator")
public class PermissionEvaluatorImpl implements PermissionEvaluator {
@Resource
private AccountService accountService;
...
}
我在 model-core-security.xml 中声明的 expressionHandler bean 中使用由组件扫描创建的 PermissionEvaluator(带有 AccountService、AccountDAO)。
可能是“找不到当前线程的会话”的原因吗?