public AgentServer(Socket c,String confDir) {
this.client = c;
ApplicationContext ac = new FileSystemXmlApplicationContext(confDir);
this.atsMgr = (AtsMgr) ac.getBean("atsMgr");
}
用FileSystemXmlApplicationContext方法准备去容器中取bean,在window环境下没有问题,但是放到Jenkins上面发现总是报错,说文件找不到:
/home/docker/.jenkins/jobs/ats/workspace/target/test-classes/utap-ats.xml org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from file [/home/docker/.jenkins/jobs/ats/workspace/home/docker/.jenkins/jobs/ats/workspace/target/test-classes/utap-ats.xml]; nested exception is java.io.FileNotFoundException: home/docker/.jenkins/jobs/ats/workspace/target/test-classes/utap-ats.xml (No such file or directory) at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:343) at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:303) at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:180) at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:216) at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:187) at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:251) at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:127) at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:93) at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:129) at org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:537) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:452) at org.springframework.context.support.FileSystemXmlApplicationContext.<init>(FileSystemXmlApplicationContext.java:140) at org.springframework.context.support.FileSystemXmlApplicationContext.<init>(FileSystemXmlApplicationContext.java:84) at com.newland.sri.utap.ats.AgentServer.<init>(AgentServer.java:31) at com.newland.sri.utap.ats.AtsTest$T1.run(AtsTest.java:57) at java.lang.Thread.run(Thread.java:745) Caused by: java.io.FileNotFoundException: home/docker/.jenkins/jobs/ats/workspace/target/test-classes/utap-ats.xml (No such file or directory) at java.io.FileInputStream.open0(Native Method) at java.io.FileInputStream.open(FileInputStream.java:195) at java.io.FileInputStream.<init>(FileInputStream.java:138) at org.springframework.core.io.FileSystemResource.getInputStream(FileSystemResource.java:115) at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:329) ... 15 more
查看源码发现,FileSystemXmlApplicationContext方法对路径进行了处理:
protected Resource getResourceByPath(String path) { if(path != null && path.startsWith("/")) { path = path.substring(1); } return new FileSystemResource(path); } }
用绝对路径时,第一个斜杠会被去掉(也是不知道为什么,我勒个去),所以,给在代码前再加个斜杠解决问题,代码通过:
public AgentServer(Socket c,String confDir) { this.client = c; ApplicationContext ac = new FileSystemXmlApplicationContext("/" + confDir); this.atsMgr = (AtsMgr) ac.getBean("atsMgr"); }