相关疑难解决方法(0)

追踪Spring的"不符合自动代理条件"的原因

当你开始搞乱Spring的自动代理东西时,你经常遇到记录的这种行为:

实现BeanPostProcessor接口的类是特殊的,因此容器对它们的处理方式不同.所有BeanPostProcessors及其直接引用的bean将在启动时实例化,作为ApplicationContext的特殊启动阶段的一部分,然后所有这些BeanPostProcessors将以排序方式注册 - 并应用于所有其他bean.由于AOP自动代理是作为BeanPostProcessor本身实现的,因此没有BeanPostProcessors或直接引用的bean可以进行自动代理(因此不会将方面'编织'到它们中.

对于任何此类bean,您应该看到一条信息日志消息:"Bean'foo'不适合所有BeanPostProcessors处理(例如:不符合自动代理条件)".

换句话说,如果我编写自己的BeanPostProcessor,并且该类直接引用上下文中的其他bean,那么这些引用的bean将不符合自动代理的条件,并且会记录一条消息.

我的问题是跟踪直接引用的位置可能非常困难,因为"直接引用"实际上可以是一系列传递依赖,最终占用应用程序上下文中的一半bean.Spring提供的只是单个信息消息,并且除了告诉您何时在这个引用网中捕获了bean之外,它并没有太多帮助.

我正在开发的BeanPostProcessor确实有对其他bean的直接引用,但它是一组非常有限的引用.尽管如此,根据日志消息,我的上下文中的几乎每个bean都被排除在自动代理之外,但我无法看到依赖性发生在哪里.

有没有人找到更好的方法来跟踪这个?

java aop spring

48
推荐指数
3
解决办法
5万
查看次数

Spring在Apache Shiro Realm类中忽略了@Transactional注释

我正在使用Spring进行IOC和事务管理,并计划使用Apache Shiro作为安全库.

每当我想检查用户的权限时,我都会打电话subject.isPermitted("right"),然后Shiro使用数据存储区检查权限.在这些调用中,建立了数据库连接,并且我已使用注释方法@Transactional.但是,每当我执行权限检查时,总是会收到一个错误,即没有Hibernate会话绑定到该线程.

该方法在Realm类中.我定义了一个自定义的Shiro Realm类:

@Component
public class MainRealm extends AuthorizingRealm {

@Autowired
protected SysUserDao userDao;

@Transactional
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token)
        throws AuthenticationException {
    ...
    final SysUser user = this.userDao.findByUsername(un);
    ...
    return authInfo;
}

@Transactional
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
    ...
    permissions = this.userDao.getAccessRights(un);
    ...
    return authInfo;
}
}
Run Code Online (Sandbox Code Playgroud)

Apache Shiro使用Servlet过滤器,因此我在web.xml中定义了以下内容:

<filter>
    <filter-name>shiroFilter</filter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
    <init-param>
        <param-name>targetFilterLifecycle</param-name>
        <param-value>true</param-value>
    </init-param>
</filter>
Run Code Online (Sandbox Code Playgroud)

我正在使用Spring的编程配置.这是我的App Config类:

@Configuration //Replaces Spring XML configuration
@ComponentScan(basePackages = "com.mycompany")
@EnableTransactionManagement //Enables declarative Transaction annotations
public class …
Run Code Online (Sandbox Code Playgroud)

spring hibernate shiro

6
推荐指数
1
解决办法
1482
查看次数

标签 统计

spring ×2

aop ×1

hibernate ×1

java ×1

shiro ×1