Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/379.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 弹簧不';添加全局方法安全命名空间时,无法看到@Transactional_Java_Spring_Hibernate_Spring Mvc_Spring Security - Fatal编程技术网

Java 弹簧不';添加全局方法安全命名空间时,无法看到@Transactional

Java 弹簧不';添加全局方法安全命名空间时,无法看到@Transactional,java,spring,hibernate,spring-mvc,spring-security,Java,Spring,Hibernate,Spring Mvc,Spring Security,我创建了一个服务,负责通过dao与数据库联系。我使用了@Transactional注释来处理事务 @Service("aclService") public class HibernateAclServiceImpl implements HibernateAclService{ private final Log logger = LogFactory.getLog(HibernateAclServiceImpl.class); @Autowired private AclObjectIde

我创建了一个服务,负责通过dao与数据库联系。我使用了
@Transactional
注释来处理事务

@Service("aclService")
public class HibernateAclServiceImpl implements HibernateAclService{

private final Log logger = LogFactory.getLog(HibernateAclServiceImpl.class);
@Autowired
private AclObjectIdentityDao objectIdentityDao ;
private PermissionFactory permissionFactory = new DefaultPermissionFactory();
@Autowired
private AclCache aclCache;
@Autowired
private PermissionGrantingStrategy grantingStrategy;
@Autowired
private AclAuthorizationStrategy aclAuthorizationStrategy;

private final Field fieldAces = FieldUtils.getField(AclImpl.class, "aces");

@Override
@Transactional
public List<ObjectIdentity> findChildren(ObjectIdentity parentIdentity) {
    AclObjectIdentity aclObjectIdentity = objectIdentityDao
            .get((Long) parentIdentity.getIdentifier());
    List<ObjectIdentity> list = new ArrayList<ObjectIdentity>(
            aclObjectIdentity.getChildren().size());
    for (AclObjectIdentity aoid : aclObjectIdentity.getChildren()) {
        final ObjectIdentity oid = new ObjectIdentityImpl(aoid.getObjectClass().getClazz());
        list.add(oid);
    }
    return list;
}

@Override
@Transactional
public Acl readAclById(ObjectIdentity object) throws NotFoundException {
    final Map<ObjectIdentity, Acl> objects = readAclsById(Arrays.asList(object), null);
    return objects.get(object);
}

@Override
@Transactional
public Acl readAclById(ObjectIdentity object, List<Sid> sids)
        throws NotFoundException {
    Map<ObjectIdentity, Acl> objects = readAclsById(Arrays.asList(object), sids);
    return objects.get(object);
}


@Override
@Transactional
public Map<ObjectIdentity, Acl> readAclsById(List<ObjectIdentity> objects)
        throws NotFoundException {
    return readAclsById(objects, null);
}

@Override
public Map<ObjectIdentity, Acl> readAclsById(List<ObjectIdentity> objects,
        List<Sid> sids) throws NotFoundException {
    Map<ObjectIdentity, Acl> result = new HashMap<ObjectIdentity, Acl>();
    Set<Long> objectsToLoad = new HashSet<Long>();

    for (int i = 0; i < objects.size(); i++) {
        final ObjectIdentity oid = objects.get(i);
        boolean aclFound = false;

        if (result.containsKey(oid)) {
            aclFound = true;
        }

        if (!aclFound) {
            Acl acl = aclCache.getFromCache(oid);

            if (acl != null) {
                if (acl.isSidLoaded(sids)) {
                    result.put(acl.getObjectIdentity(), acl);
                    aclFound = true;
                } else {
                    throw new IllegalStateException(
                            "Error: SID-filtered element detected when implementation does not perform SID filtering "
                                    + "- have you added something to the cache manually?");
                }
            }
        }
        if (!aclFound) {
            objectsToLoad.add((Long) oid.getIdentifier());
        }
    }

    if (objectsToLoad.size() > 0) {
        lookupAcl(result, objectsToLoad);
    }
    return result;
}
public void lookupAcl(Map<ObjectIdentity, Acl> map, Set<Long> objects){
    final List<AclObjectIdentity> aoids = objectIdentityDao.getList(objects);
    final Map<Long, Long> parents = new HashMap<Long, Long>();
    for(AclObjectIdentity aoid : aoids){
        if(aoid.isEntriesInheriting()){
            parents.put(aoid.getId(), aoid.getParent().getId());
        }
    }
    if(parents.size() > 0){
        lookupAcl(map, (Set<Long>)parents.values());
    }
    for(AclObjectIdentity aoid : aoids){
        if(map.containsKey(aoid.getId()))
            continue;
        final Acl parentAcl = map.get(parents.get(aoid.getId()));
        final Acl acl = new AclImpl(new ObjectIdentityImpl(aoid.getObjectClass().getClazz(), aoid.getId()), aoid.getId(), aclAuthorizationStrategy, grantingStrategy, parentAcl, null, aoid.isEntriesInheriting(), new PrincipalSid(aoid.getOwnerSid().getSid()));



        List<AccessControlEntryImpl> aces = new ArrayList<AccessControlEntryImpl>(aoid.getAclEntries().size());
        for(AclEntry aclEntry : aoid.getAclEntries()){
            final Permission permission = permissionFactory.buildFromMask(aclEntry.getMask());
            aces.add(new AccessControlEntryImpl(aclEntry.getId(), acl, new PrincipalSid(aclEntry.getSid().getSid()), permission, aclEntry.isGranting(), aclEntry.isAuditSuccess(), aclEntry.isAuditFailure()));
        }
        setAces((AclImpl) acl, aces);
        aclCache.putInCache((AclImpl) acl);
    }
}

private void setAces(AclImpl acl, List<AccessControlEntryImpl> aces) {
    try {
        fieldAces.set(acl, aces);
    } catch (IllegalAccessException e) {
        throw new IllegalStateException("Could not set AclImpl entries", e);
    }
}
@Service(“aclService”)
公共类HibernateAclServiceImpl实现HibernateAclService{
私有最终日志记录器=LogFactory.getLog(HibernateAclServiceImpl.class);
@自动连线
私有AclObjectIdentityDao objectIdentityDao;
private PermissionFactory PermissionFactory=新的DefaultPermissionFactory();
@自动连线
私有AclCache-AclCache;
@自动连线
私人许可授予策略授予策略;
@自动连线
私有AclAuthorizationStrategy AclAuthorizationStrategy;
私有最终字段fieldAces=FieldUtils.getField(AclImpl.class,“aces”);
@凌驾
@交易的
公共列表查找的孩子(ObjectIdentity parentIdentity){
AclObjectIdentity AclObjectIdentity=objectIdentityDao
.get((长)parentIdentity.getIdentifier());
列表=新的ArrayList(
aclObjectIdentity.getChildren().size());
对于(AclObjectIdentity aoid:AclObjectIdentity.getChildren()){
final ObjectIdentity oid=新ObjectIdentity Impl(aoid.getObjectClass().getClazz());
列表。添加(oid);
}
退货清单;
}
@凌驾
@交易的
公共Acl readAclById(ObjectIdentity对象)引发NotFoundException{
最终映射对象=readaclsbyd(Arrays.asList(object),null);
返回objects.get(object);
}
@凌驾
@交易的
公共Acl readAclById(ObjectIdentity对象,列表SID)
抛出NotFoundException{
Map objects=readAclsById(Arrays.asList(object),SID);
返回objects.get(object);
}
@凌驾
@交易的
公共地图readAclsById(列出对象)
抛出NotFoundException{
返回readaclsbyd(objects,null);
}
@凌驾
公共地图readAclsById(列出对象,
列表SID)引发NotFoundException{
映射结果=新的HashMap();
Set objectsToLoad=new HashSet();
对于(int i=0;i0){
lookupAcl(结果,objectsToLoad);
}
返回结果;
}
公共void lookupAcl(映射、设置对象){
最终列表AOID=objectIdentityDao.getList(对象);
最终映射父对象=新HashMap();
用于(AclObjectIdentity aoid:aoid){
if(aoid.isEntriesInheriting()){
parents.put(aoid.getId(),aoid.getParent().getId());
}
}
如果(parents.size()>0){
lookupAcl(map,(Set)parents.values());
}
用于(AclObjectIdentity aoid:aoid){
if(map.containsKey(aoid.getId()))
继续;
final Acl parentAcl=map.get(parents.get(aoid.getId());
最终Acl Acl=new AclImpl(new ObjectIdentityImpl(aoid.getObjectClass().getClazz(),aoid.getId()),aoid.getId(),aclAuthorizationStrategy,grantingStrategy,parentAcl,null,aoid.isEntriesInheriting(),new PrincipalId(aoid.getOwnerSid().getSid());
List aces=newarraylist(aoid.getacentries().size());
for(acentry acentry:aoid.getacentries()){
最终权限=permissionFactory.buildFromMask(acentry.getMask());
添加(新的AccessControlEntryImpl(acentry.getId(),acl,新的PrincipalId(acentry.getSid().getSid()),权限,acentry.isgrating(),acentry.isAuditAccess(),acentry.isAuditFailure());
}
setAces((AclImpl)acl,aces);
aclCache.putInCache((AclImpl)acl);
}
}
私有void setAces(AclImpl acl,List aces){
试一试{
fieldAces.set(acl,aces);
}捕获(非法访问例外e){
抛出新的IllegalStateException(“无法设置AclImpl条目”,e);
}
}
}

这是我的“app context.xml”文件的一部分

<security:global-method-security  pre-post-annotations="enabled">
    <security:expression-handler ref="expressionHandler" />
</security:global-method-security>
<bean id="expressionHandler"
    class="org.springframework.security.access.expression.method.DefaultMethodSecurityExpressionHandler">
    <property name="permissionEvaluator" ref="permissionEvaluator" />
    <property name="roleHierarchy" ref="roleHierarchy" />
</bean>

<bean class="org.springframework.security.acls.AclPermissionEvaluator"
    id="permissionEvaluator">
    <constructor-arg ref="aclService" />
</bean>

现在,当我从控制器调用服务的函数时,它抛出一个错误
org.hibernate.hibernate异常:找不到当前线程的会话
。但当我发表评论时,一切都很好(交易没有问题)



我检查了所有内容,并将有问题的代码缩小到上面的部分。有人知道为什么会发生这种情况吗?

我不确定
全局方法安全性是如何在幕后实现的,但
BeanPostProcessor
有一个鲜为人知的副作用-任何BeanPostProcessor直接引用的bean,或者被BPP引用的东西引用的bean,不符合AOP自动代理的条件:

BeanPostProcessors和AOP自动代理

实现BeanPostProcessor接口的类是特殊的,容器会对它们进行不同的处理。它们直接引用的所有BeanPostProcessor和Bean都在启动时实例化,作为ApplicationContext特殊启动阶段的一部分。接下来,所有BeanPostProcessor都以排序方式注册,并应用于容器中的所有其他Bean。因为AOP自动代理是作为BeanPostProcessor本身实现的,所以无论是BeanPostProcessor还是它们直接引用的bean都不符合自动代理的条件,因此也不符合自动代理的条件