Jetty多项目启动时的冲突问题

通过main启动多项目Jetty时碰到了lib冲突的问题,如下面这个异常

Invocation of init method failed; nested exception is java.lang.SecurityException: sealing violation: package oracle.jdbc.driver is sealed

下面是我启动用的代码

Server server = new Server(HTTPPORT);

WebAppContext webContext = new WebAppContext("src/main/webapp", CONTEXT);
        webContext.setClassLoader(Thread.currentThread().getContextClassLoader());

WebAppContext other = new WebAppContext();
other.setWar("../other.war");
other.setContextPath("/other");

server.addHandler(webContext);
server.addHandler(other);


server.setStopAtShutdown(true);
server.start();
server.join();

因为当前工程和other是用同一套lib,打包时被打进了war,又不想全部拆出来处理,结果就得到了上面的异常。这种冲突的异常表现形式不唯一,大体都能猜出来是同一个原因。查了各种方案也没明确说明怎么解决这个问题,难道要去看源码了么?懒蛋的我灵光一现!

server.addHandler(other);
server.addHandler(webContext);

把顺序调换一下,成功!

万万没想到这篇博客还有二周目

原因是这样的,在成功调整了Jetty的启动顺序后,两个项目已经可以和谐共存了,controller,service,dao看起来是那么的美好。然而这美好并没有持续太久,在一个aop的午后,平静被打破了。

[org.springframework.web.context.support.XmlWebApplicationContext] Exception encountered during context initialization - cancelling refresh attempt
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'tableDao': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private xxxxxxxxxx.Dialect yyyyyyy.dialect; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [xxxxxxxxxx.Dialect] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:301)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1186)

这么个奇怪的报错,不由得虎躯一震。看前面的日志信息,是jetty在解包war工程,并且加载完xml以后,扫描了当前工程的class文件。

卧槽!这不科学!
翻看Jetty的API没找到怎么设置独立上下文的办法,耗时2days+

不能够,一定有解决办法,于是开debug看jetty加载流程。终于debug到spring的加载层,发现war工程spring在加载xml时,会根据classpath*: 这个参数在每个工程里找,也就是除了当前要处理的war工程,也会查找当前工程。而我要加载的这两个工程的结构是差不多的,spring配置都写在了一个叫spring的文件夹下。

虽然没找到怎么分隔上下文,但是我可以把文件夹变更为独立的,然后在各自工程下将component-scan限制到精确路径。再次加载果然解决了这个问题。

别再来三周目了,伤不起

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值