[SPRING] 문자열 값의 자리 표시자를 해결할 수 없습니다.
SPRING문자열 값의 자리 표시자를 해결할 수 없습니다.
.properties 파일에서 속성을 사용하려고하는데 작동하지 않는 것 같습니다.
여기 내 코드가 있습니다 :
@Service("ServiceFTP")
@Transactional
public class ServiceFTPImpl implements ServiceFTP {
@Value("${project.ftp.adresse}")
private String adresse;
@Value("${project.ftp.login}")
private String compte;
@Value("${project.ftp.password}")
private String motDePasse;
@Value("${project.ftp.root}")
private String ROOT;
[...]
}
이 클래스는 @Value 주석을 사용하여 속성을 가져옵니다. 또한 스프링 서비스로 선언되고 내 infraContext.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"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">
<context:property-placeholder location="classpath:context-core.properties"/>
[...]
</beans>
context : property-placeholder를 사용하여이 파일을 context-core.properties 파일에 연결합니다.
project.ftp.adresse = localhost
project.ftp.login = anonymous
project.ftp.password =
project.ftp.root = /anonymous/
이 말이 맞습니까?
그러나 프로젝트를 시작하려고 할 때 Tomcat은이 예외를 throw합니다.
ERROR [context.ContextLoader.initWebApplicationContext()] Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'ServiceFTP': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private java.lang.String project.sins.service.impl.ServiceFTPImpl.adresse; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'project.ftp.adresse' in string value "${project.ftp.adresse}"
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:287)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1106)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:607)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:925)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:472)
at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:388)
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:293)
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:111)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4887)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5381)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:901)
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:877)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:633)
at org.apache.catalina.startup.HostConfig.deployDescriptor(HostConfig.java:657)
at org.apache.catalina.startup.HostConfig$DeployDescriptor.run(HostConfig.java:1636)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334)
at java.util.concurrent.FutureTask.run(FutureTask.java:166)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:722)
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private java.lang.String project.sins.service.impl.ServiceFTPImpl.adresse; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'project.ftp.adresse' in string value "${project.ftp.adresse}"
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:513)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:92)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:284)
... 27 more
Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'project.ftp.adresse' in string value "${project.ftp.adresse}"
at org.springframework.util.PropertyPlaceholderHelper.parseStringValue(PropertyPlaceholderHelper.java:173)
at org.springframework.util.PropertyPlaceholderHelper.replacePlaceholders(PropertyPlaceholderHelper.java:125)
at org.springframework.core.env.AbstractPropertyResolver.doResolvePlaceholders(AbstractPropertyResolver.java:151)
at org.springframework.core.env.AbstractPropertyResolver.resolveRequiredPlaceholders(AbstractPropertyResolver.java:142)
at org.springframework.context.support.PropertySourcesPlaceholderConfigurer$2.resolveStringValue(PropertySourcesPlaceholderConfigurer.java:169)
at org.springframework.beans.factory.support.AbstractBeanFactory.resolveEmbeddedValue(AbstractBeanFactory.java:748)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:740)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:730)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:485)
... 29 more
또는 간단히 말해서 : java.lang.IllegalArgumentException : 문자열 값 "$ {project.ftp.adresse}"에서 'project.ftp.adresse'라는 자리 표시자를 확인할 수 없습니다.
편집하다 :
다음은 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"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="sins" version="2.5">
<display-name>Project</display-name>
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
<context-param>
<param-name>log4jExposeWebAppRoot</param-name>
<param-value>false</param-value>
</context-param>
<filter>
<filter-name>ExpiresFilter</filter-name>
<filter-class>org.apache.catalina.filters.ExpiresFilter</filter-class>
<init-param>
<param-name>ExpiresByType text/html</param-name>
<param-value>now plus 0 seconds</param-value>
</init-param>
<init-param>
<param-name>ExpiresByType application/json</param-name>
<param-value>now plus 0 seconds</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>ExpiresFilter</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>REQUEST</dispatcher>
</filter-mapping>
<filter>
<filter-name>EncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>EncodingFilter</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>
<filter>
<filter-name>Spring OpenEntityManagerInViewFilter</filter-name>
<filter-class>
org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>Spring OpenEntityManagerInViewFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>org.apache.struts2.tiles.StrutsTilesListener</listener-class>
</listener>
<context-param>
<param-name>
org.apache.tiles.impl.BasicTilesContainer.DEFINITIONS_CONFIG
</param-name>
<param-value>
/WEB-INF/tiles/user.xml
</param-value>
</context-param>
<resource-ref>
<res-ref-name>jdbc/si_nsg</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
<session-config>
<session-timeout>60</session-timeout>
</session-config>
</web-app>
내 infraContext.xml을 applicationContext.xml이라는 다른 .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"
xmlns:jee="http://www.springframework.org/schema/jee"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.1.xsd">
<bean class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer">
<property name="environment">
<bean class="org.springframework.web.context.support.StandardServletEnvironment"/>
</property>
</bean>
<import resource="classpath:securityContext.xml"/>
[...]
<import resource="classpath:project/sins/persistenceContext.xml"/>
<import resource="classpath:project/sins/infraContext.xml"/>
</beans>
분명히 뭔가 빠졌지 만, 나는 무엇을 알아낼 수 없다.
자세한 내용이 필요하면 알려주세요. 첫 번째 질문이므로 최대한 빨리 답변 드리겠습니다. :)
해결법
-
==============================
1.구성에 2 개의 PropertySourcesPlaceholderConfigurer 인스턴스가 있습니다.
구성에 2 개의 PropertySourcesPlaceholderConfigurer 인스턴스가 있습니다.
applicationContext.xml
<bean class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer"> <property name="environment"> <bean class="org.springframework.web.context.support.StandardServletEnvironment"/> </property> </bean>
infraContext.xml
<context:property-placeholder location="classpath:context-core.properties"/>
기본적으로 PlaceholderConfigurer는 빠른 속도로 진행되므로 placeholder를 확인할 수 없으면 예외가 발생합니다. applicationContext.xml 파일의 인스턴스에는 속성이 없으므로 모든 자리 표시 자에서 실패합니다.
해결책 : 아무것도 추가하지 않으면 applicationContext.xml에서 제거합니다.
-
==============================
2.나는 같은 문제를 겪고 있었다.
나는 같은 문제를 겪고 있었다.
<filtering>true</filtering>
pom.xml :
이전 (작동하지 않음) :
<build> <resources> <resource> <directory>src/main/resources</directory> </resource> </resources> </build>
후에 (효과가 있었다) :
<build> <resources> <resource> <directory>src/main/resources</directory> <filtering>true</filtering> </resource> </resources> </build>
그 다음에는 mvn을 새로 설치하고 응용 프로그램을 배포하기 만하면됩니다.
-
==============================
3.이 문제는 응용 프로그램이 some_file_name.properties 파일에 액세스 할 수없는 경우 발생합니다. 속성 파일이 spring의 resources 폴더 아래에 있는지 확인하십시오.
이 문제는 응용 프로그램이 some_file_name.properties 파일에 액세스 할 수없는 경우 발생합니다. 속성 파일이 spring의 resources 폴더 아래에 있는지 확인하십시오.
문제 해결 단계
1 : 리소스 폴더 아래에 등록 정보 파일을 추가하십시오.
2 : 리소스 폴더가없는 경우. new> Source Folder를 마우스 오른쪽 버튼으로 클릭하고 자원으로 이름을 지정한 다음 속성 파일을 그 아래에 놓습니다.
주석 기반 구현
@PropertySource를 추가합니다 (ignoreResourceNotFound = true, value = "classpath : some_file_name.properties") // 자리 표시자를 사용하기 전에 추가하십시오
예:
Assignment1Controller.Java
@PropertySource(ignoreResourceNotFound = true, value = "classpath:assignment1.properties") @RestController public class Assignment1Controller { // @Autowired // Assignment1Services assignment1Services; @Value("${app.title}") private String appTitle; @RequestMapping(value = "/hello") public String getValues() { return appTitle; } }
assignment1.properties
app.title=Learning Spring
from https://stackoverflow.com/questions/20244696/could-not-resolve-placeholder-in-string-value by cc-by-sa and MIT license