Spring AOP不起作用

时间:2017-02-01 11:26:43

标签: java spring spring-aop

我正在使用Spring AOP来记录我的应用程序系统。但我有一些麻烦。

我有简单的课程

@Component
@Scope(BeanDefinition.SCOPE_PROTOTYPE)
public class MessageReceiverImpl implements MessageReceiver {
    final private BufferedReader reader;

    public MessageReceiverImpl(BufferedReader reader) {
        this.reader = reader;
    }
    public String receive(){
        String msg = null;
        try {
            msg = reader.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return msg;
    }
}

带有MessageReceiverImpl的piontcut的Logger类

@Aspect
public class LoggerMessage {

    private static final Logger log = Logger.getLogger("listenerLogger");

    @Before("execution(* server.logic.listener.message.MessageReceiverImpl.receive(..))")
    public void logMessageReceiver() {
        log.info("INFO : LoggerMessage -> new Client");
    }
}

我的bean.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"     xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop-3.0.xsd ">

    <aop:aspectj-autoproxy>
        <aop:include name="loggerMessage"/>
    </aop:aspectj-autoproxy>
    <aop:aspectj-autoproxy proxy-target-class="true"/>

    <!-- Aspect -->
    <bean id="loggerMessage" class="service.logger.listener.LoggerMessage" />

</beans>

但是在执行receive()方法之前它不起作用。其他Log类工作得很好。哪里有问题?

更多信息。创造不稳定

@Bean
@Scope(BeanDefinition.SCOPE_PROTOTYPE)
 UploadSession getSession(Socket socket) {
    UploadSessionImpl uploadSession = null;
    try {
        uploadSession = new UploadSessionImpl(socket);

        BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        MessageReceiver receiver = new MessageReceiverImpl(reader);
        uploadSession.setReceiver(receiver);

        PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
        MessageSender sender = new MessageSenderImpl(out);
        uploadSession.setSender(sender);

        uploadSession.setBaseDir(context.getEnvironment().getProperty("listener.test-runs-folder"));
    } catch (IOException e) {
        e.printStackTrace();
    }
    return uploadSession;
}

使用

public class UploadSessionImpl implements UploadSession,     ApplicationContextAware {

private MessageReceiver receiver;

public UploadSessionImpl(Socket socket) throws IOException {
    this.socket = socket;
}

public void process() throws IOException {
    String msg;

    while (true) {
        msg = receiver.receive();
        if (msg.equals("close")) break;
        ...
    }
}

1 个答案:

答案 0 :(得分:0)

到目前为止,您的代码看起来像是在搞乱bean创建配置。试试这些:

    @Bean
    @Scope(BeanDefinition.SCOPE_PROTOTYPE)
    public BufferedReader bufferedReader(Socket socket) throws Exception {
        return new BufferedReader(new InputStreamReader(socket.getInputStream()));
    }

    @Bean
    @Scope(BeanDefinition.SCOPE_PROTOTYPE)
    public MessageReceiver receiver(BufferedReader bufferedReader) {
        return new MessageReceiverImpl(bufferedReader);
    }

    @Bean
    @Scope(BeanDefinition.SCOPE_PROTOTYPE)
    UploadSession getSession(Socket socket, MessageReceiver receiver) {
        UploadSessionImpl uploadSession = null;
        try {
            uploadSession = new UploadSessionImpl(socket);

            BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            uploadSession.setReceiver(receiver);

            PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
            MessageSender sender = new MessageSenderImpl(out);
            uploadSession.setSender(sender);

            uploadSession.setBaseDir(context.getEnvironment().getProperty("listener.test-runs-folder"));
        } catch (IOException e) {
            e.printStackTrace();
        }
        return uploadSession;
    }