0
我使用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;
}
}
;記錄儀類piontcut爲MessageReceiverImpl
@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>
但它不執行之前收到。 () 方法。其他日誌類工作良好。我在哪裏遇到問題?
更多信息。創建instatnce
@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;
...
}
}
你有'aop:aspectj-autoproxy'應該被刪除的重複條目。還要保留'proxy-target-class =「true」' –
你如何創建'MessageRecieverImpl'類的實例?這個配置如何加載?這裏的信息太少了......請提供一個可以複製此信息的樣本,而不僅僅是片段。 –
@M。 Deinum我添加了一些更多的信息 –