Spring aop方面没有在注释上执行

时间:2016-07-12 22:39:20

标签: java spring aop spring-aop

我正在使用spring开发WebSocket服务器应用程序。 类PlayerHandler

import org.springframework.web.socket.WebSocketSession;
import org.springframework.web.socket.handler.TextWebSocketHandler;

import java.io.IOException;

/**
 * Created by kris on 11.07.16.
 */
public class PlayerHandler extends TextWebSocketHandler{
    public PlayerHandler(){}

    @Override
    @AuthorizationRequired
    public void handleTextMessage(WebSocketSession session, TextMessage tm) throws IOException {
        session.sendMessage(tm);
    }
}

我希望用户通过令牌获得每个传入请求的授权,因此我创建了一个Aspect UserAuthorization

package com.berrigan.axevor.authorization;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;


@Aspect
@Component
public class UserAuthorization {
    @Around("@annotation(com.berrigan.axevor.authorization.AuthorizationRequired)")
    public void authorize(ProceedingJoinPoint jp) throws Throwable{
         System.out.println("\n\n\n\n\Works\n\n\n\n\n\n");
        jp.proceed();
    }
}

我添加了@AuthorizationRequired注释,它指示了用户将被授权的方法。不幸的是方法授权永远不会被调用。我已将以下代码添加到我的主类中,以检查bean是否已创建。

UserAuthorization ua = ctx.getBean(UserAuthorization.class); // ApplicationContext
if(au == null) System.out.println("is null")

但我没有得到这样的日志。 我的春季配置

@EnableAutoConfiguration
@Configuration
@EnableAspectJAutoProxy
@Import({com.berrigan.axevor.websocket.WebSocketConfig.class})
@ComponentScan(basePackages = {"com.berrigan.axevor"})
public class Config {}

注释码:

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface AuthorizationRequired{}

@Configuration @EnableWebSocket

public class WebSocketConfig implements WebSocketConfigurer{
    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry){
        registry.addHandler(playerHandler(), "/game").setAllowedOrigins("*");
    }

    @Bean
    public WebSocketHandler playerHandler(){
        return new PlayerHandler();
    }
}

1 个答案:

答案 0 :(得分:0)

找到解决方案,损坏了pom.xml文件。重新生成后,一切都像魅力