Java Code Examples for org.springframework.aop.framework.AopContext#currentProxy()

The following examples show how to use org.springframework.aop.framework.AopContext#currentProxy() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: UserServiceImpl.java    From thinking-in-spring with Apache License 2.0 6 votes vote down vote up
/**
     * 使用事务非常容易犯以下错误:
     * 1)在同一个类中,a调用b,在b上面新开了一个事务,以为b上的事务生效了,其实是不生效的,
     * 因为事务基于aop实现,是通过代理对象完成事务包裹的,必须通过代理对象调用,事务才会生效,
     * 同一个类中,a调b,相当于this.a,只是一个普通对象调用,没有事务,很多人容易犯这个错误,
     * 老坑了。
     * 2)事务方法必须为public的,否则事务也不会生效。
     *
     * @author yihonglei
     * @date 2019/1/15 11:53
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public void a(String userName) {
        // 插入user 记录
        jdbcTemplate.update("INSERT INTO `user` (userName) VALUES(?)", userName);

        // 事务未生效:同类中a直接调用b方法,相当于b的代码直接写在a里面,事务要基于代理对象调用才有效,所以b上的事务是无效的。
//         b(userName, 10000);

        // 事务生效:获取代理对象,基于代理对象调用,事务才能生效。
        UserService proxy = (UserService) AopContext.currentProxy();
        proxy.b(userName, 10000);

        // 人为报错
        int i = 1 / 0;
    }
 
Example 2
Source File: RoleClearRelationTask.java    From es with Apache License 2.0 6 votes vote down vote up
/**
 * 清除删除的角色对应的关系
 */
public void clearDeletedRoleRelation() {

    final int PAGE_SIZE = 100;
    int pn = 0;

    Page<Role> rolePage = null;
    do {
        Pageable pageable = new PageRequest(pn++, PAGE_SIZE);
        rolePage = roleService.findAll(pageable);
        //开启新事物清除
        try {
            RoleClearRelationTask roleClearRelationTask = (RoleClearRelationTask) AopContext.currentProxy();
            roleClearRelationTask.doClear(rolePage.getContent());
        } catch (Exception e) {
            //出异常也无所谓
            LogUtils.logError("clear role relation error", e);

        }
        //清空会话
        RepositoryHelper.clear();
    } while (rolePage.hasNextPage());
}
 
Example 3
Source File: AuthRelationClearTask.java    From es with Apache License 2.0 6 votes vote down vote up
/**
 * 清除删除的角色对应的关系
 */
public void clearDeletedAuthRelation() {

    Set<Long> allRoleIds = findAllRoleIds();

    final int PAGE_SIZE = 100;
    int pn = 0;

    Page<Auth> authPage = null;

    do {
        Pageable pageable = new PageRequest(pn++, PAGE_SIZE);
        authPage = authService.findAll(pageable);
        //开启新事物清除
        try {
            AuthRelationClearTask authRelationClearService = (AuthRelationClearTask) AopContext.currentProxy();
            authRelationClearService.doClear(authPage.getContent(), allRoleIds);
        } catch (Exception e) {
            //出异常也无所谓
            LogUtils.logError("clear auth relation error", e);
        }
        //清空会话
        RepositoryHelper.clear();
    } while (authPage.hasNextPage());
}
 
Example 4
Source File: UserService.java    From es with Apache License 2.0 5 votes vote down vote up
public void changePassword(User opUser, Long[] ids, String newPassword) {
    UserService proxyUserService = (UserService) AopContext.currentProxy();
    for (Long id : ids) {
        User user = findOne(id);
        proxyUserService.changePassword(user, newPassword);
        UserLogUtils.log(
                user.getUsername(),
                "changePassword",
                "admin user {} change password!", opUser.getUsername());

    }
}
 
Example 5
Source File: UserService.java    From es with Apache License 2.0 5 votes vote down vote up
public void changeStatus(User opUser, Long[] ids, UserStatus newStatus, String reason) {
    UserService proxyUserService = (UserService) AopContext.currentProxy();
    for (Long id : ids) {
        User user = findOne(id);
        proxyUserService.changeStatus(opUser, user, newStatus, reason);
        UserLogUtils.log(
                user.getUsername(),
                "changeStatus",
                "admin user {} change status!", opUser.getUsername());
    }
}
 
Example 6
Source File: UserClearRelationTask.java    From es with Apache License 2.0 5 votes vote down vote up
/**
 * 清除删除的用户-组织机构/工作职务对应的关系
 */
public void clearDeletedUserRelation() {

    //删除用户不存在的情况的UserOrganizationJob(比如手工从数据库物理删除)。。
    userService.deleteUserOrganizationJobOnNotExistsUser();

    Page<UserOrganizationJob> page = null;

    int pn = 0;
    final int PAGE_SIZE = 100;
    Pageable pageable = null;
    do {
        pageable = new PageRequest(pn++, PAGE_SIZE);
        page = userService.findUserOrganizationJobOnNotExistsOrganizationOrJob(pageable);

        //开启新事物清除
        try {
            UserClearRelationTask userClearRelationTask = (UserClearRelationTask) AopContext.currentProxy();
            userClearRelationTask.doClear(page.getContent());
        } catch (Exception e) {
            //出异常也无所谓
            LogUtils.logError("clear user relation error", e);

        }
        //清空会话
        RepositoryHelper.clear();

    } while (page.hasNextPage());

}
 
Example 7
Source File: MessageClearTask.java    From es with Apache License 2.0 5 votes vote down vote up
public void autoClearExpiredOrDeletedmMessage() {
    MessageClearTask messageClearTask = (MessageClearTask) AopContext.currentProxy();
    //1、收件箱、发件箱状态修改为垃圾箱状态
    messageClearTask.doClearInOrOutBox();
    //2、垃圾箱状态改为已删除状态
    messageClearTask.doClearTrashBox();
    //3、物理删除那些已删除的(即收件人和发件人 同时都删除了的)
    messageClearTask.doClearDeletedMessage();
}
 
Example 8
Source File: GenericService.java    From Aooms with Apache License 2.0 4 votes vote down vote up
public <T> T proxy(Class<T> obj){
	return ((T) AopContext.currentProxy());
}
 
Example 9
Source File: ExcelDataService.java    From es with Apache License 2.0 4 votes vote down vote up
@Async
public void initOneMillionData(final User user) {

    ExcelDataService proxy = (ExcelDataService) AopContext.currentProxy();

    long beginTime = System.currentTimeMillis();

    getExcelDataRepository().truncate();

    final int ONE_MILLION = 1000000; //100w
    for(int i = batchSize; i <= ONE_MILLION; i += batchSize) {
        //不能使用AopContext.currentProxy() 因为task:annotation-driven没有暴露proxy。。
        proxy.doBatchSave(i - batchSize);
    }

    long endTime = System.currentTimeMillis();

    Map<String, Object> context = Maps.newHashMap();
    context.put("seconds", (endTime - beginTime) / 1000);
    notificationApi.notify(user.getId(), "excelInitDataSuccess", context);

}
 
Example 10
Source File: UserService.java    From es with Apache License 2.0 4 votes vote down vote up
public User login(String username, String password) {

        if (StringUtils.isEmpty(username) || StringUtils.isEmpty(password)) {
            UserLogUtils.log(
                    username,
                    "loginError",
                    "username is empty");
            throw new UserNotExistsException();
        }
        //密码如果不在指定范围内 肯定错误
        if (password.length() < User.PASSWORD_MIN_LENGTH || password.length() > User.PASSWORD_MAX_LENGTH) {
            UserLogUtils.log(
                    username,
                    "loginError",
                    "password length error! password is between {} and {}",
                    User.PASSWORD_MIN_LENGTH, User.PASSWORD_MAX_LENGTH);

            throw new UserPasswordNotMatchException();
        }

        User user = null;

        //此处需要走代理对象,目的是能走缓存切面
        UserService proxyUserService = (UserService) AopContext.currentProxy();
        if (maybeUsername(username)) {
            user = proxyUserService.findByUsername(username);
        }

        if (user == null && maybeEmail(username)) {
            user = proxyUserService.findByEmail(username);
        }

        if (user == null && maybeMobilePhoneNumber(username)) {
            user = proxyUserService.findByMobilePhoneNumber(username);
        }

        if (user == null || Boolean.TRUE.equals(user.getDeleted())) {
            UserLogUtils.log(
                    username,
                    "loginError",
                    "user is not exists!");

            throw new UserNotExistsException();
        }

        passwordService.validate(user, password);

        if (user.getStatus() == UserStatus.blocked) {
            UserLogUtils.log(
                    username,
                    "loginError",
                    "user is blocked!");
            throw new UserBlockedException(userStatusHistoryService.getLastReason(user));
        }

        UserLogUtils.log(
                username,
                "loginSuccess",
                "");
        return user;
    }
 
Example 11
Source File: SpringUtils.java    From supplierShop with MIT License 2 votes vote down vote up
/**
 * 获取aop代理对象
 * 
 * @param invoker
 * @return
 */
@SuppressWarnings("unchecked")
public static <T> T getAopProxy(T invoker)
{
    return (T) AopContext.currentProxy();
}
 
Example 12
Source File: AopContextHolder.java    From Milkomeda with MIT License 2 votes vote down vote up
/**
 * 获得当前切面代理对象
 * <br>使用前通过<code>@EnableAspectJAutoProxy(proxyTargetClass = true, exposeProxy = true)</code>开启代理曝露
 *
 * @param clazz 当前类
 * @param <T>   当前类型
 * @return  代理对象
 */
@SuppressWarnings("unchecked")
public static <T> T self(Class<T> clazz) {
    return  (T)AopContext.currentProxy();
}
 
Example 13
Source File: SpringUtils.java    From runscore with Apache License 2.0 2 votes vote down vote up
/**
 * 获取aop代理对象
 * 
 * @param invoker
 * @return
 */
@SuppressWarnings("unchecked")
public static <T> T getAopProxy(T invoker) {
	return (T) AopContext.currentProxy();
}
 
Example 14
Source File: SpringUtils.java    From RuoYi-Vue with MIT License 2 votes vote down vote up
/**
 * 获取aop代理对象
 * 
 * @param invoker
 * @return
 */
@SuppressWarnings("unchecked")
public static <T> T getAopProxy(T invoker)
{
    return (T) AopContext.currentProxy();
}
 
Example 15
Source File: SpringUtils.java    From DimpleBlog with Apache License 2.0 2 votes vote down vote up
/**
 * 获取aop代理对象
 *
 * @param invoker
 * @return
 */
@SuppressWarnings("unchecked")
public static <T> T getAopProxy(T invoker) {
    return (T) AopContext.currentProxy();
}
 
Example 16
Source File: SpringUtils.java    From RuoYi with Apache License 2.0 2 votes vote down vote up
/**
 * 获取aop代理对象
 *
 * @param invoker
 * @return 代理对象
 */
@SuppressWarnings("unchecked")
public static <T> T getAopProxy(T invoker){
    return (T) AopContext.currentProxy();
}