Matthew
Matthew

Reputation: 867

Spring can't resolve property placeholder but still supplies the value?

Currently working on a Spring application; I'm very new to Spring.

For some reason, Spring picks up my .properties file and injects the literal into my object. In the output, I see the appropriate line: com.test.Baseball@345e5a17 with the name "Chuck Norris". However, later in the stacktrace, it says that this value cannot be resolved:

com.test.Baseball@345e5a17 with the name "Chuck Norris"
14:58:39.618 [main] DEBUG org.springframework.context.support.ClassPathXmlApplicationContext - 
Closing org.springframework.context.support.ClassPathXmlApplicationContext@e2d56bf, started on Mon 
Dec 16 14:58:36 CST 2019

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.2.2.RELEASE)

2019-12-16 14:58:39.976  INFO 98376 --- [           main] .c.t.SpringAnnotationsProjectApplication : 
Starting SpringAnnotationsProjectApplication on C001823506 with PID 98376 (C:\Users\\eclipse- 
workspace\Spring-Annotations-project\target\classes started by in C:\Users\\eclipse- 
workspace\Spring-Annotations-project)
2019-12-16 14:58:39.981  INFO 98376 --- [           main] .c.t.SpringAnnotationsProjectApplication : 
No active profile set, falling back to default profiles: default
2019-12-16 14:58:40.878  INFO 98376 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : 
Tomcat initialized with port(s): 8080 (http)
2019-12-16 14:58:40.897  INFO 98376 --- [           main] o.apache.catalina.core.StandardService   : 
Starting service [Tomcat]
2019-12-16 14:58:40.898  INFO 98376 --- [           main] org.apache.catalina.core.StandardEngine  : 
Starting Servlet engine: [Apache Tomcat/9.0.29]
2019-12-16 14:58:41.126  INFO 98376 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : 
Initializing Spring embedded WebApplicationContext
2019-12-16 14:58:41.134  INFO 98376 --- [           main] o.s.web.context.ContextLoader            : 
Root WebApplicationContext: initialization completed in 1136 ms
2019-12-16 14:58:41.217  WARN 98376 --- [           main] ConfigServletWebServerApplicationContext : 
Exception encountered during context initialization - cancelling refresh attempt: 
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'coach': 
Injection of autowired dependencies failed; nested exception is java.lang.IllegalArgumentException: 
Could not resolve placeholder 'coach.name' in value "${coach.name}"
2019-12-16 14:58:41.225  INFO 98376 --- [           main] o.apache.catalina.core.StandardService   : 
Stopping service [Tomcat]
2019-12-16 14:58:41.237  INFO 98376 --- [           main] ConditionEvaluationReportLoggingListener : 

Error starting ApplicationContext. To display the conditions report re-run your application with 
'debug' enabled.
2019-12-16 14:58:41.247 ERROR 98376 --- [           main] o.s.boot.SpringApplication               : 
Application run failed

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'coach': 
Injection of autowired dependencies failed; nested exception is java.lang.IllegalArgumentException: 
Could not resolve placeholder 'coach.name' in value "${coach.name}"
     at 


org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:405) ~[spring-beans-5.2.2.RELEASE.jar:5.2.2.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1422) ~[spring-beans-5.2.2.RELEASE.jar:5.2.2.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:594) ~[spring-beans-5.2.2.RELEASE.jar:5.2.2.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:517) ~[spring-beans-5.2.2.RELEASE.jar:5.2.2.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:323) ~[spring-beans-5.2.2.RELEASE.jar:5.2.2.RELEASE]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans-5.2.2.RELEASE.jar:5.2.2.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:321) ~[spring-beans-5.2.2.RELEASE.jar:5.2.2.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) ~[spring-beans-5.2.2.RELEASE.jar:5.2.2.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:879) ~[spring-beans-5.2.2.RELEASE.jar:5.2.2.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:878) ~[spring-context-5.2.2.RELEASE.jar:5.2.2.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:550) ~[spring-context-5.2.2.RELEASE.jar:5.2.2.RELEASE]
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:141) ~[spring-boot-2.2.2.RELEASE.jar:2.2.2.RELEASE]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:747) [spring-boot-2.2.2.RELEASE.jar:2.2.2.RELEASE]
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397) [spring-boot-2.2.2.RELEASE.jar:2.2.2.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:315) [spring-boot-2.2.2.RELEASE.jar:2.2.2.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226) [spring-boot-2.2.2.RELEASE.jar:2.2.2.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1215) [spring-boot-2.2.2.RELEASE.jar:2.2.2.RELEASE]
    at com.cat.test.SpringAnnotationsProjectApplication.main(SpringAnnotationsProjectApplication.java:20) [classes/:na]
Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'coach.name' in value "${coach.name}"

The .properties file:

coach.name="Chuck Norris"
coach.item="basketball"

The applicationContext.xml file:

<?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.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd">

   <context:component-scan base-package="com.test"></context:component-scan>

    <context:property-placeholder location="classpath:PropertyValues.properties"/>

</beans>

The Coach class (the problem class):

package com.test;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class Coach {

    private SportsItem item;
    @Value("${coach.name}")
    private String name;

    @Autowired
    public Coach(@Qualifier("baseball") SportsItem item) {
        this.item = item;
    }


    public void setItem(SportsItem item) {
        this.item = item;
    }

    public SportsItem getItem() {
        return item;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getName() {

        return name;
    }

}

my main class:

@SpringBootApplication
public class SpringAnnotationsProjectApplication {

    public static void main(String[] args) {

        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");

        Coach coachBean = context.getBean("coach", Coach.class);

        System.out.println(coachBean.getItem() + " with the name " + coachBean.getName());

        context.close();
        SpringApplication.run(SpringAnnotationsProjectApplication.class, args);
    }

}

What makes this strange is that in the very first line, we get the proper output. Despite this, Spring still detects a problem and shuts down.

What is going on here?

Upvotes: 0

Views: 2484

Answers (1)

cmullins
cmullins

Reputation: 36

The reason you are seeing these errors is because you need to import the applicationContext.xml. You can do this by creating a configuration class that imports the resource. This is explained in this document: https://www.springboottutorial.com/spring-boot-java-xml-context-configuration. I have also included an example of what it would look like below.

@Configuration
@ImportResource({"classpath*:applicationContext.xml"})
public class XmlConfiguration 
{

}

Upvotes: 2

Related Questions