java.lang.IllegalArgumentException Could not resolve placeholder

In this post, we will see how to fix java.lang.IllegalArgumentException Could not resolve placeholder exception in Spring Boot. It may be different reasons for Could not resolve placeholder exception. Let’s see a few reasons and fix this exception.

  • If we forget to define the attribute in application.properties or define the wrong attribute name and use this attribute further in our java we may get java.lang.IllegalArgumentException Could not resolve placeholder.

For example below code will throw java.lang.IllegalArgumentException Could not resolve placeholder.

@Service
public class StudentServiceImpl implements StudentService {

    @Value("${test.flag}")
    private boolean testFlag;

    @Autowired
    private StudentRepository studentRepository;
    

    @Transactional
    public Student save(Student student) {
        Student createResponse = null;
        if(testFlag){
            createResponse = studentRepository.save(student);
        }
        //createResponse = studentRepository.save(student);
        return createResponse;
    }
    
}

services.properties

In the services.properties file, we are not going to define test.flag attribute.

spring.datasource.url=jdbc:mysql://localhost:3306/springbootcrudexample
spring.datasource.username=root
spring.datasource.password=root
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.format_sql=true
#server.port = 9091
#test.flag=true

The above code snippet will throw Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder ‘test.flag’ in value “${test.flag}” exception.

  • If we define a properties file with a different name (for example – appconfig.properties) then make sure we are reading it correctly.

Consider we have defined appconfig.properties as below.

java.lang.IllegalArgumentException Could not resolve placeholder

@Configuration
@EnableJpaRepositories(basePackages = "com.javatute.repository")
@PropertySource(value={"classpath:appconfig.properties"})
public class JpaConfig {

}

or

@Configuration
@EnableJpaRepositories(basePackages = "com.javatute.repository")
@PropertySource(value={"appconfig.properties"})
public class JpaConfig {

}

Stacktrace for this exception.

Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder ‘test.flag’ in value “${test.flag}”
at org.springframework.util.PropertyPlaceholderHelper.parseStringValue(PropertyPlaceholderHelper.java:178) ~[spring-core-5.2.7.RELEASE.jar:5.2.7.RELEASE]
at org.springframework.util.PropertyPlaceholderHelper.replacePlaceholders(PropertyPlaceholderHelper.java:124) ~[spring-core-5.2.7.RELEASE.jar:5.2.7.RELEASE]
at org.springframework.core.env.AbstractPropertyResolver.doResolvePlaceholders(AbstractPropertyResolver.java:239) ~[spring-core-5.2.7.RELEASE.jar:5.2.7.RELEASE]
at org.springframework.core.env.AbstractPropertyResolver.resolveRequiredPlaceholders(AbstractPropertyResolver.java:210) ~[spring-core-5.2.7.RELEASE.jar:5.2.7.RELEASE]
at org.springframework.context.support.PropertySourcesPlaceholderConfigurer.lambda$processProperties$0(PropertySourcesPlaceholderConfigurer.java:175) ~[spring-context-5.2.7.RELEASE.jar:5.2.7.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.resolveEmbeddedValue(AbstractBeanFactory.java:912) ~[spring-beans-5.2.7.RELEASE.jar:5.2.7.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1245) ~[spring-beans-5.2.7.RELEASE.jar:5.2.7.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1224) ~[spring-beans-5.2.7.RELEASE.jar:5.2.7.RELEASE]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:640) ~[spring-beans-5.2.7.RELEASE.jar:5.2.7.RELEASE]
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:130) ~[spring-beans-5.2.7.RELEASE.jar:5.2.7.RELEASE]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:399) ~[spring-beans-5.2.7.RELEASE.jar:5.2.7.RELEASE]
… 31 common frames omitted

Let’s see the complete example that will show how to avoid java.lang.IllegalArgumentException Could not resolve placeholder exception.

Student.java

@Entity
public class Student implements Serializable {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @Column(name = "name")
    private String name;

    @Column(name = "roll_number")
    private String rollNumber;

    @Column(name = "university")
    private String university;
    
    //getter and setter
}

StudentService.java

@Component
public interface StudentService {

    public Student save(Student student);

    public Student update(Student student);

    public Student get(Long id);

    public List<Student> getStudents(String name);

    public void delete(Student student);
}

StudentRepository.java

@Repository
public interface StudentRepository extends JpaRepository<Student, Serializable> {

    List<Student> findByName(String name);
}

StudentServiceImpl.java

@Service
public class StudentServiceImpl implements StudentService {

    @Value("${test.flag}")
    private boolean testFlag;

    @Autowired
    private StudentRepository studentRepository;


    @Transactional
    public Student save(Student student) {
        Student createResponse = null;
        if(testFlag){
            createResponse = studentRepository.save(student);
        }
        //createResponse = studentRepository.save(student);
        return createResponse;
    }

    @Transactional
    public Student update(Student student) {
        Student updateResponse = studentRepository.save(student);
        return updateResponse;
    }

    @Transactional
    public Student get(Long id) {
        Optional<Student> response = studentRepository.findById(id);
        Student getResponse = response.get();
        //String sql = "select name from student";
        //List<String> names =  jdbcTemplate.queryForList(sql,  String.class);
        //return names;
        return getResponse;
    }

    @Transactional
    public List<Student> getStudents(String name) {
        List<Student> response = new ArrayList<>();
       // List<Student> response = studentRepository.findByName1(name);
        //Student getResponse = response.get();
        //String sql = "select name from student";
        //List<String> names =  jdbcTemplate.queryForList(sql,  String.class);
        //return names;
        return response;
    }

    @Transactional
    public void delete(Student student) {
        studentRepository.delete(student);
    }
}

StudentController.java

package com.javatute.controller;

import com.javatute.entity.Student;
import com.javatute.repository.StudentRepository;
import com.javatute.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@RestController
@RequestMapping("/student")
public class StudentController {

    @Autowired
    private StudentService studentService;

    @Autowired
    private StudentRepository studentRepository;

    @PostMapping("/create")
    public Student createStudent1(@RequestBody Student student) {
        Student createResponse = studentService.save(student);
        return createResponse;
    }

	@PutMapping("/update")
	public Student updateStudent(@RequestBody Student student) {
		Student updateResponse = studentService.update(student);
		return updateResponse;
	}

/*	@GetMapping("/{id}")
	public Student getStudent(@PathVariable Long id) {
		Student getReponse = studentService.get(id);
		return getReponse;
	}*/

	@GetMapping("/{name}")
	public List<Student> getStudent(@PathVariable String name) {
		List<Student> getReponse = studentService.getStudents(name);
		return getReponse;
	}

	@DeleteMapping("/delete")
	public String deleteStudent(@RequestBody Student student) {
		studentService.delete(student);
		return "Record deleted succesfully";
	}

	@GetMapping("/getcustomfieldsbasisofuserinput")
	public String deleteStudent(@RequestParam String fields) {
		System.out.println(fields);
		//studentService.delete(student);
		return "Record deleted succesfully";
	}

}

JpaConfig.java

@Configuration
@EnableJpaRepositories(basePackages = "com.javatute.repository")
public class JpaConfig {

}

SpringMain.java

@SpringBootApplication
@ComponentScan(basePackages = "com.javatute.*")
@EntityScan("com.javatute.*")
public class SpringMain {
    public static void main(String[] args) {
        SpringApplication.run(SpringMain.class, args);
    }
}

That’s all about java.lang.IllegalArgumentException Could not resolve placeholder exception.