在Heroku配置Spring XML数据源时遭遇非法字符错误求助
Looks like the root cause of your error is pretty straightforward—you're using HTML-escaped single quotes (') in your Spring EL expression, which Spring doesn't recognize as valid syntax.
Spring Expression Language expects plain single quotes (') or double quotes (") to wrap the key name when accessing system environment variables. The ' is an HTML entity for a single quote, which gets treated as an illegal character in the expression fragment.
How to Fix It
Replace the escaped quotes with regular single quotes in your XML configuration:
#{systemEnvironment['DATABASE_URL']}
Alternatively, you can use double quotes instead (just make sure they don't conflict with the XML attribute's quotes):
#{systemEnvironment["DATABASE_URL"]}
Quick Configuration Check
While you're fixing that, let's make sure you haven't missed other critical bits for the Heroku datasource setup:
- Context Namespace: Ensure your Spring XML file includes the
contextnamespace, which enables support for SpEL expressions and property placeholders. Here's a sample root beans element with the correct namespaces:<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> - Property Placeholder: Add the
<context:property-placeholder/>element to your configuration to enable access to system environment variables via SpEL. - Database Connection Pool: Make sure you've included a valid connection pool dependency (like HikariCP, Heroku's recommended option) in your build file. For Maven, that would look like:
<dependency> <groupId>com.zaxxer</groupId> <artifactId>HikariCP</artifactId> <version>5.0.1</version> </dependency>
Once you swap out those escaped quotes, the illegal character error should disappear. If you run into other issues, double-check that the DATABASE_URL environment variable is properly set on Heroku (this is usually auto-configured when you add a database add-on).
内容的提问来源于stack exchange,提问作者fsakiyama

