tomcat 9 中sessionCreated 这个方法存在的的接口源码如下
public interface HttpSessionListener extends EventListener {
/**
* Notification that a session was created.
* The default implementation is a NO-OP.
*
* @param se
* the notification event
*/
public default void sessionCreated(HttpSessionEvent se) {
}
/**
* Notification that a session is about to be invalidated.
* The default implementation is a NO-OP.
*
* @param se
* the notification event
*/
public default void sessionDestroyed(HttpSessionEvent se) {
}
}
可以看到是个默认方法 所以它的实现类也没有实现该方法 源码如下
public class WsSessionListener implements HttpSessionListener{
private final WsServerContainer wsServerContainer;
public WsSessionListener(WsServerContainer wsServerContainer) {
this.wsServerContainer = wsServerContainer;
}
@Override
public void sessionDestroyed(HttpSessionEvent se) {
wsServerContainer.closeAuthenticatedSession(se.getSession().getId());
}
}
问题的原因就在于我的代码中引用的包引入了另一个版本的接口 代码如下
public interface HttpSessionListener extends EventListener {
void sessionCreated(HttpSessionEvent var1);
void sessionDestroyed(HttpSessionEvent var1);
}
可以看到不是一个默认方法必须要求实现
再来看看报错的地方
可以看到运行时候因为存在两个api的冲突 所以会去找它的实现类 把它默认为是另一个版本的实现类然后因为并没有对该方法implement 所以进行了报错
解决办法 将引入这个api的包在dependency中exclude即可
网上有许多exclude的方法 但是由于大多是引入第三方的包造成的 所以需要找一下自己的情况是哪个包引入的才能解决问题