Java 无法解析字符串值中的占位符
在我的Spring项目中的一项服务中,我有:Java 无法解析字符串值中的占位符,java,spring,Java,Spring,在我的Spring项目中的一项服务中,我有: @Value("${myProject.myVar}") private int myVar; 这意味着引用名为application.properties的文件中的值,该文件位于我的ProjectRoot/src/main/java文件夹中。该文件如下所示: myProject.myVar = 6 我还有一个配置文件ProjectRoot/WebContent/WEB-INF/spring/servlet context.xml,其中包含以下行
@Value("${myProject.myVar}")
private int myVar;
这意味着引用名为application.properties
的文件中的值,该文件位于我的ProjectRoot/src/main/java
文件夹中。该文件如下所示:
myProject.myVar = 6
我还有一个配置文件ProjectRoot/WebContent/WEB-INF/spring/servlet context.xml
,其中包含以下行:
<context:property-placeholder location="classpath:application.properties" />
但是,我的程序随后会崩溃,出现以下嵌套异常:
Could not autowire field: private int
org.myproject.service.MyServiceImpl.myVar; nested
exception is java.lang.IllegalArgumentException: Could not resolve placeholder
'myProject.myVar' in string value "${myProject.myVar}"
有什么好处
编辑:web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID"
version="3.0">
<display-name>Test</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/spring/root-context.xml
/WEB-INF/spring/spring-security.xml
/WEB-INF/spring/servlet-context.xml
</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/servlet-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<filter>
<filter-name>CORS</filter-name>
<filter-class>com.thetransactioncompany.cors.CORSFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>CORS</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>
org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<error-page>
<error-code>404</error-code>
<location>/WEB-INF/views/errors/error.jsp</location>
</error-page>
</web-app>
试验
上下文配置位置
/WEB-INF/spring/root-context.xml
/WEB-INF/spring/spring-security.xml
/WEB-INF/spring/servlet-context.xml
org.springframework.web.context.ContextLoaderListener
appServlet
org.springframework.web.servlet.DispatcherServlet
上下文配置位置
/WEB-INF/spring/servlet-context.xml
1.
appServlet
/
科尔斯
com.thetransactioncompany.cors.CORSFilter
科尔斯
/*
springSecurityFilterChain
org.springframework.web.filter.DelegatingFilterProxy
springSecurityFilterChain
/*
404
/WEB-INF/views/errors/error.jsp
您的application.properties文件位于错误的位置。它应该位于src/main/resources下,应用程序上下文才能加载它。如果您正在运行测试用例,那么它应该位于src/test/resources下
注意,您正在启动spring引导应用程序
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
两条建议
WEB-INF/classes
下,Maven或Gradle等构建工具将自动将src/main/resources
下的所有文件放在WEB-INF/classes
中=
前后的空格<代码>myProject.myVar=6变成myProject.myVar=6
我试过了,但也犯了同样的错误。我还尝试暂时删除
application.properties
,但我发现了另一个错误,即找不到该文件。似乎正在读取文件,但由于某种原因,该值没有被映射。您如何初始化spring引导上下文?在web.xml
中,我将servlet context.xml
列为参数。我猜,您可能已经在xml中注册了PropertyPlaceholderConfigurer bean,也可能使用了注释驱动的方法。同时做这两件事不是一个好主意。我会建议,使用与此问题完全相关的spring boot?谢谢你的建议,不幸的是,实施它们并没有改变任何事情。@Ecliptica那么,我恐怕你必须提供更多关于你的项目的细节。像web.xml
@Ecliptica一样,我怀疑您可能会将该值加载到diff上下文中。您可以尝试仅使用一个上下文来配置web.xml,可以在此处找到信息。我已将我的web.xml
文件添加到原始问题中。@Ecliptica 1。尝试删除侦听器和上下文参数标记并再次运行。2.您能在tomcat下的分解文件夹中找到/WEB-INF/classes
下的应用程序.properties
吗?
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}