caimengyuan
蔡梦缘
采纳率66.7%
2017-02-07 12:43 阅读 3.3k
已采纳

spring中Cglib代理某个类不起效

5

不知道怎么的事务不起效,我就挣扎了良久,尝试过多种解决方案,发现是没有被动态代理,所以@Transactional 就没有起效了

这里写图片描述

这里写图片描述

然后做一份完全的拷贝UserService2

这里写图片描述

这里写图片描述
这个逆天的问题呈现在一个经验欠缺的菜鸟前面,跪了!求救

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

7条回答 默认 最新

  • 已采纳
    caimengyuan 蔡梦缘 2017-02-07 15:45

    图片说明
    真的是其它的注入地方影响到了它的代理
    我去除了ShiroRealm的注入就可以代理了

     @Slf4j
    @Component
    public class MShiroRealm extends AuthorizingRealm {
    
    //    @Autowired
    //    private UserService userService;
    
    .............................................
    }
    
    
    点赞 1 评论 复制链接分享
  • JE_GE JE_GE 2017-02-07 14:26

    下面扫描这个对比?

     <context:component-scan base-package="com.jege.ssh" />
    

    去对比一下
    http://blog.csdn.net/je_ge/article/details/54633287

    点赞 评论 复制链接分享
  • JE_GE JE_GE 2017-02-07 14:36

    试试这个

     <aop:aspectj-autoproxy proxy-target-class="true"/>
    
    点赞 评论 复制链接分享
  • sheldon178 shadon178 2017-02-07 14:54

    UserService和UserService2是一模一样的么?发出来看看,还有你的事务注解是怎么写的?

    点赞 评论 复制链接分享
  • JE_GE JE_GE 2017-02-07 15:02

    如果是用spring boot的话,同一个包下面的UserService,UserService2有不同效果就诡异了
    必须看全部代码才行了哦

    点赞 评论 复制链接分享
  • caimengyuan 蔡梦缘 2017-02-07 15:07
     package cn.cherish.mboot.service;
    
    import cn.cherish.mboot.dal.dto.UserDTO;
    import cn.cherish.mboot.dal.entity.User;
    import cn.cherish.mboot.dal.vo.BasicSearchVO;
    import cn.cherish.mboot.dal.vo.user.UserSaveVO;
    import cn.cherish.mboot.dal.vo.user.UserSearchVO;
    import cn.cherish.mboot.dal.vo.user.UserUpdateVO;
    import cn.cherish.mboot.extra.shiro.CryptographyUtil;
    import cn.cherish.mboot.repository.IBaseDAO;
    import cn.cherish.mboot.repository.UserDAO;
    import cn.cherish.mboot.util.ObjectConvertUtil;
    import lombok.extern.slf4j.Slf4j;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.cache.annotation.CacheConfig;
    import org.springframework.cache.annotation.CacheEvict;
    import org.springframework.cache.annotation.Cacheable;
    import org.springframework.data.domain.Page;
    import org.springframework.data.domain.PageImpl;
    import org.springframework.data.domain.PageRequest;
    import org.springframework.stereotype.Service;
    import org.springframework.transaction.annotation.Transactional;
    
    import java.util.Date;
    import java.util.List;
    import java.util.stream.Collectors;
    
    @Slf4j
    @Service
    @CacheConfig(cacheNames = "users")
    @Transactional(readOnly = true)
    public class UserService extends ABaseService<User, Long> {
    
        @Autowired
        private UserDAO userDAO;
    
        private static final String UNKNOW = "未知";
        private static final String AC = "激活/在职";
        private static final String UN = "冻结/离职";
    
        @Override
        protected IBaseDAO<User, Long> getEntityDAO() {
            return userDAO;
        }
    
        @Cacheable(key = "'username_' + #username", unless = "#result==null")
        public User findByUsername(String username) {
            log.debug("username_{}没有缓存", username);
            return userDAO.findByUsername(username);
        }
    
        public boolean exist(String username) {
            return userDAO.findByUsername(username) != null;
        }
    
        @Cacheable(key = "'countAllUser'")
        public Long getCount() {
            log.debug("countAllUser没有缓存");
            return userDAO.count();
        }
    
        @Transactional(readOnly = false)
        @CacheEvict(allEntries = true)
        public void delete(Long id) {
            // 并不是真正的删除,只是冻结账户
            User user = findById(id);
            user.setActive(0);
            update(user);
        }
    
        @Transactional
        @CacheEvict(allEntries = true)
        public void updateByVO(UserUpdateVO userUpdateVO) {
            User user = findById(userUpdateVO.getId());
            ObjectConvertUtil.objectCopy(user, userUpdateVO);
            user.setModifiedTime(new Date());
            update(user);
        }
    
        @Transactional
        public void saveByVO(UserSaveVO userSaveVO) {
    
            if (exist(userSaveVO.getUsername())) {
                return;
            }
    
            User user = new User();
            ObjectConvertUtil.objectCopy(user, userSaveVO);
            user.setCreatedTime(new Date());
            user.setModifiedTime(new Date());
            user.setPassword(CryptographyUtil.cherishSha1(user.getPassword()));
            save(user);
        }
    
        public Page<UserDTO> findAll(UserSearchVO userSearchVO, BasicSearchVO basicSearchVO) {
    
            int pageNumber = basicSearchVO.getStartIndex() / basicSearchVO.getPageSize() + 1;
            PageRequest pageRequest = super.buildPageRequest(pageNumber, basicSearchVO.getPageSize());
    
            //除了分页条件没有特定搜索条件的,为了缓存count
            if (ObjectConvertUtil.objectFieldIsBlank(userSearchVO)){
                log.debug("没有特定搜索条件的");
                List<User> userList = userDAO.listAllPaged(pageRequest);
                List<UserDTO> userDTOList = userList.stream().map(source -> {
                    UserDTO userDTO = new UserDTO();
                    ObjectConvertUtil.objectCopy(userDTO, source);
                    userDTO.setActiveStr(source.getActive() == null ? UNKNOW : source.getActive() == 1 ? AC : UN);
                    return userDTO;
                }).collect(Collectors.toList());
    
                //为了计算总数使用缓存,加快搜索速度
                Long count = getCount();
                return new PageImpl<>(userDTOList, pageRequest, count);
            }
    
            //有了其它搜索条件
            Page<User> userPage = super.findAllBySearchParams(
                    buildSearchParams(userSearchVO), pageNumber, basicSearchVO.getPageSize());
    
            return userPage.map(source -> {
                UserDTO userDTO = new UserDTO();
                ObjectConvertUtil.objectCopy(userDTO, source);
                userDTO.setActiveStr(source.getActive() == null ? UNKNOW : source.getActive() == 1 ? AC : UN);
                return userDTO;
            });
    
        }
    
    }
    
    
     package cn.cherish.mboot.service;
    
    import cn.cherish.mboot.dal.dto.RoleDTO;
    import cn.cherish.mboot.dal.entity.Role;
    import cn.cherish.mboot.dal.vo.BasicSearchVO;
    import cn.cherish.mboot.dal.vo.role.RoleSaveVO;
    import cn.cherish.mboot.dal.vo.role.RoleUpdateVO;
    import cn.cherish.mboot.repository.IBaseDAO;
    import cn.cherish.mboot.repository.RoleDAO;
    import cn.cherish.mboot.util.ObjectConvertUtil;
    import lombok.extern.slf4j.Slf4j;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.cache.annotation.CacheConfig;
    import org.springframework.data.domain.Page;
    import org.springframework.stereotype.Service;
    import org.springframework.transaction.annotation.Transactional;
    
    import java.util.List;
    
    @Slf4j
    @Service
    @Transactional(readOnly = true)
    @CacheConfig(cacheNames = "roles")
    public class RoleService extends ABaseService<Role, Long> {
    
        @Autowired
        private RoleDAO roleDAO;
    
        @Override
        protected IBaseDAO<Role, Long> getEntityDAO() {
            return roleDAO;
        }
    
        public Page<RoleDTO> findAll(BasicSearchVO basicSearchVO) {
    
            int pageNumber = basicSearchVO.getStartIndex() / basicSearchVO.getPageSize() + 1;
            Page<Role> rolePage = this.findAll(pageNumber, basicSearchVO.getPageSize());
    
            return rolePage.map(source -> {
                RoleDTO roleDTO = new RoleDTO();
                ObjectConvertUtil.objectCopy(roleDTO, source);
                return roleDTO;
            });
        }
    
        @Transactional
        public void updateByVO(RoleUpdateVO roleUpdateVO) {
            Role role = this.findById(roleUpdateVO.getId());
            ObjectConvertUtil.objectCopy(role, roleUpdateVO);
            this.update(role);
        }
    
        public boolean exist(String name) {
            return roleDAO.findByName(name) != null;
        }
    
        @Transactional
        public void saveByVO(RoleSaveVO roleSaveVO) {
            Role role = new Role();
            ObjectConvertUtil.objectCopy(role, roleSaveVO);
            this.save(role);
        }
    
        public List<Role> listByUserId(Long userId){
            return roleDAO.findByUserId(userId);
        }
    
        /*不建议如此强硬,该手动去除关联再删除
        @Transactional(readOnly = false)
        public void delete(Long roleId){
            //先删除t_user_role表的外键关联
            customizedDAO.deleteUserRoleRelation(roleId);
            //再删除role
            roleDAO.delete(roleId);
        }*/
    
    }
    
    
    点赞 评论 复制链接分享
  • JE_GE JE_GE 2017-02-08 04:53
     @Service("userService001")添加一个名字试试
    
    点赞 评论 复制链接分享

相关推荐