Spring Framework ClassPathXmlApplicationContext打包JAR后启动挂起问题
Spring加载Bean定义时卡死(打包JAR后异常)
之前遇到过类似的Spring加载Bean定义卡死的问题,但对应的解决方案并不适用于当前场景。
环境信息
- Spring Framework 5.3.29
- OpenJDK 8
核心代码
System.out.println("Loading beans.xml from classpath"); ApplicationContext context = new ClassPathXmlApplicationContext("classpath:beans.xml"); System.out.println("Loaded beans.xml from classpath.\nRunning helloWorld.");
beans.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" xsi:schemaLocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id = "helloWorld" class = "net.application.spring.HelloWorld"> <property name = "message" value = "Hello World!"/> </bean> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.microsoft.sqlserver.jdbc.SQLServerDriver" /> <property name="url" value="jdbc:sqlserver://blahblahblah" /> <property name="username" value="some_user" /> <property name="password" value="some_password" /> </bean> <bean id = "exceptionDataDAO" class = "net.application.spring.ExceptionDataDAOImpl"> <property name="dataSource" ref="dataSource" /> </bean> </beans>
问题现象
- 在VSCode中直接运行或调试时,程序运行完全正常
- 使用Maven命令
mvn clean compile assembly:single打包成可执行JAR后,执行该JAR文件时,程序会卡在初始化ApplicationContext的代码行ApplicationContext context = new ClassPathXmlApplicationContext("classpath:beans.xml");,无法继续执行后续逻辑
内容的提问来源于stack exchange,提问作者jwh20
相关产品推荐
相关产品推荐

